[d565449] | 1 | argparse
|
---|
| 2 | ========
|
---|
| 3 |
|
---|
| 4 | [![Build Status](https://secure.travis-ci.org/nodeca/argparse.svg?branch=master)](http://travis-ci.org/nodeca/argparse)
|
---|
| 5 | [![NPM version](https://img.shields.io/npm/v/argparse.svg)](https://www.npmjs.org/package/argparse)
|
---|
| 6 |
|
---|
| 7 | CLI arguments parser for node.js, with [sub-commands](https://docs.python.org/3.9/library/argparse.html#sub-commands) support. Port of python's [argparse](http://docs.python.org/dev/library/argparse.html) (version [3.9.0](https://github.com/python/cpython/blob/v3.9.0rc1/Lib/argparse.py)).
|
---|
| 8 |
|
---|
| 9 | **Difference with original.**
|
---|
| 10 |
|
---|
| 11 | - JS has no keyword arguments support.
|
---|
| 12 | - Pass options instead: `new ArgumentParser({ description: 'example', add_help: true })`.
|
---|
| 13 | - JS has no python's types `int`, `float`, ...
|
---|
| 14 | - Use string-typed names: `.add_argument('-b', { type: 'int', help: 'help' })`.
|
---|
| 15 | - `%r` format specifier uses `require('util').inspect()`.
|
---|
| 16 |
|
---|
| 17 | More details in [doc](./doc).
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | Example
|
---|
| 21 | -------
|
---|
| 22 |
|
---|
| 23 | `test.js` file:
|
---|
| 24 |
|
---|
| 25 | ```javascript
|
---|
| 26 | #!/usr/bin/env node
|
---|
| 27 | 'use strict';
|
---|
| 28 |
|
---|
| 29 | const { ArgumentParser } = require('argparse');
|
---|
| 30 | const { version } = require('./package.json');
|
---|
| 31 |
|
---|
| 32 | const parser = new ArgumentParser({
|
---|
| 33 | description: 'Argparse example'
|
---|
| 34 | });
|
---|
| 35 |
|
---|
| 36 | parser.add_argument('-v', '--version', { action: 'version', version });
|
---|
| 37 | parser.add_argument('-f', '--foo', { help: 'foo bar' });
|
---|
| 38 | parser.add_argument('-b', '--bar', { help: 'bar foo' });
|
---|
| 39 | parser.add_argument('--baz', { help: 'baz bar' });
|
---|
| 40 |
|
---|
| 41 | console.dir(parser.parse_args());
|
---|
| 42 | ```
|
---|
| 43 |
|
---|
| 44 | Display help:
|
---|
| 45 |
|
---|
| 46 | ```
|
---|
| 47 | $ ./test.js -h
|
---|
| 48 | usage: test.js [-h] [-v] [-f FOO] [-b BAR] [--baz BAZ]
|
---|
| 49 |
|
---|
| 50 | Argparse example
|
---|
| 51 |
|
---|
| 52 | optional arguments:
|
---|
| 53 | -h, --help show this help message and exit
|
---|
| 54 | -v, --version show program's version number and exit
|
---|
| 55 | -f FOO, --foo FOO foo bar
|
---|
| 56 | -b BAR, --bar BAR bar foo
|
---|
| 57 | --baz BAZ baz bar
|
---|
| 58 | ```
|
---|
| 59 |
|
---|
| 60 | Parse arguments:
|
---|
| 61 |
|
---|
| 62 | ```
|
---|
| 63 | $ ./test.js -f=3 --bar=4 --baz 5
|
---|
| 64 | { foo: '3', bar: '4', baz: '5' }
|
---|
| 65 | ```
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | API docs
|
---|
| 69 | --------
|
---|
| 70 |
|
---|
| 71 | Since this is a port with minimal divergence, there's no separate documentation.
|
---|
| 72 | Use original one instead, with notes about difference.
|
---|
| 73 |
|
---|
| 74 | 1. [Original doc](https://docs.python.org/3.9/library/argparse.html).
|
---|
| 75 | 2. [Original tutorial](https://docs.python.org/3.9/howto/argparse.html).
|
---|
| 76 | 3. [Difference with python](./doc).
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | argparse for enterprise
|
---|
| 80 | -----------------------
|
---|
| 81 |
|
---|
| 82 | Available as part of the Tidelift Subscription
|
---|
| 83 |
|
---|
| 84 | The maintainers of argparse and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-argparse?utm_source=npm-argparse&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|
---|