Debian Codegraph @

node/argparse/README.md

raw
1 argparse
2 ========
3
4 [![CI](https://github.com/nodeca/argparse/actions/workflows/ci.yml/badge.svg)](https://github.com/nodeca/argparse/actions/workflows/ci.yml)
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.14/library/argparse.html#sub-commands) support. Port of python's [argparse](https://docs.python.org/3.14/library/argparse.html) (version [3.14.6](https://github.com/python/cpython/blob/v3.14.6/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 See the complete list of [differences from Python](./doc/port_difference.md).
18 Users upgrading from v2 should read the [migration guide](./doc/migrate_v2_to_v3.md).
19
20
21 Example
22 -------
23
24 Following code is a JS program that takes a list of integers and produces either the sum or the max:
25
26 ```js
27 const { ArgumentParser } = require('argparse')
28
29 const parser = new ArgumentParser({ description: 'Process some integers.' })
30
31 let sum = ints => ints.reduce((a, b) => a + b)
32 let max = ints => ints.reduce((a, b) => a > b ? a : b)
33
34 parser.add_argument('integers', { metavar: 'N', type: 'int', nargs: '+',
35 help: 'an integer for the accumulator' })
36 parser.add_argument('--sum', { dest: 'accumulate', action: 'store_const',
37 const: sum, default: max,
38 help: 'sum the integers (default: find the max)' });
39
40 let args = parser.parse_args()
41 console.log(args.accumulate(args.integers))
42 ```
43
44 Assuming the JS code above is saved into a file called prog.js, it can be run at the command line and provides useful help messages:
45
46 ```
47 $ node prog.js -h
48 usage: prog.js [-h] [--sum] N [N ...]
49
50 Process some integers.
51
52 positional arguments:
53 N an integer for the accumulator
54
55 options:
56 -h, --help show this help message and exit
57 --sum sum the integers (default: find the max)
58 ```
59
60 When run with the appropriate arguments, it prints either the sum or the max of the command-line integers:
61
62 ```
63 $ node prog.js 1 2 3 4
64 4
65 $ node prog.js 1 2 3 4 --sum
66 10
67 ```
68
69 If invalid arguments are passed in, it will issue an error:
70
71 ```
72 $ node prog.js a b c
73 usage: prog.js [-h] [--sum] N [N ...]
74 prog.js: error: argument N: invalid 'int' value: 'a'
75 ```
76
77 This is an example ported from Python. You can find detailed explanation [here](https://docs.python.org/3.14/library/argparse.html).
78
79
80 API docs
81 --------
82
83 Since this is a port with minimal divergence, there's no separate documentation.
84 Use original one instead, with notes about difference.
85
86 1. [Original doc](https://docs.python.org/3.14/library/argparse.html).
87 2. [Original tutorial](https://docs.python.org/3.14/howto/argparse.html).
88 3. [Differences from Python](./doc/port_difference.md).
89 4. [Migration from v2 to v3](./doc/migrate_v2_to_v3.md).