UNPKG

2.77 kBMarkdownView Raw
1argparse
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
7CLI 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
17More details in [doc](./doc).
18
19
20Example
21-------
22
23`test.js` file:
24
25```javascript
26#!/usr/bin/env node
27'use strict';
28
29const { ArgumentParser } = require('argparse');
30const { version } = require('./package.json');
31
32const parser = new ArgumentParser({
33 description: 'Argparse example'
34});
35
36parser.add_argument('-v', '--version', { action: 'version', version });
37parser.add_argument('-f', '--foo', { help: 'foo bar' });
38parser.add_argument('-b', '--bar', { help: 'bar foo' });
39parser.add_argument('--baz', { help: 'baz bar' });
40
41console.dir(parser.parse_args());
42```
43
44Display help:
45
46```
47$ ./test.js -h
48usage: test.js [-h] [-v] [-f FOO] [-b BAR] [--baz BAZ]
49
50Argparse example
51
52optional 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
60Parse arguments:
61
62```
63$ ./test.js -f=3 --bar=4 --baz 5
64{ foo: '3', bar: '4', baz: '5' }
65```
66
67
68API docs
69--------
70
71Since this is a port with minimal divergence, there's no separate documentation.
72Use original one instead, with notes about difference.
73
741. [Original doc](https://docs.python.org/3.9/library/argparse.html).
752. [Original tutorial](https://docs.python.org/3.9/howto/argparse.html).
763. [Difference with python](./doc).
77
78
79argparse for enterprise
80-----------------------
81
82Available as part of the Tidelift Subscription
83
84The 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)