Skip to content

Commit a645a9a

Browse files
committed
Port python example to README
1 parent 70fc26e commit a645a9a

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

Diff for: README.md

+42-41
Original file line numberDiff line numberDiff line change
@@ -20,60 +20,61 @@ More details in [doc](./doc).
2020
Example
2121
-------
2222

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-
// Formatter with support of `\n` in Help texts.
33-
class HelpFormatter extends ArgumentParser.RawDescriptionHelpFormatter {
34-
// executes parent _split_lines for each line of the help, then flattens the result
35-
_split_lines(text, width) {
36-
return [].concat(...text.split('\n').map(line => super._split_lines(line, width)));
37-
}
38-
}
39-
40-
const parser = new ArgumentParser({
41-
description: 'Argparse example',
42-
add_help: true,
43-
formatter_class: HelpFormatter
44-
});
45-
46-
parser.add_argument('-v', '--version', { action: 'version', version });
47-
parser.add_argument('-f', '--foo', { help: 'foo bar' });
48-
parser.add_argument('-b', '--bar', { help: 'bar foo' });
49-
parser.add_argument('--baz', { help: 'baz bar' });
50-
51-
console.dir(parser.parse_args());
23+
Following code is a JS program that takes a list of integers and produces either the sum or the max:
24+
25+
```js
26+
const { ArgumentParser } = require('argparse')
27+
28+
const parser = new ArgumentParser({ description: 'Process some integers.' })
29+
30+
let sum = ints => ints.reduce((a, b) => a + b)
31+
let max = ints => ints.reduce((a, b) => a > b ? a : b)
32+
33+
parser.add_argument('integers', { metavar: 'N', type: 'int', nargs: '+',
34+
help: 'an integer for the accumulator' })
35+
parser.add_argument('--sum', { dest: 'accumulate', action: 'store_const',
36+
const: sum, default: max,
37+
help: 'sum the integers (default: find the max)' });
38+
39+
let args = parser.parse_args()
40+
console.log(args.accumulate(args.integers))
5241
```
5342

54-
Display help:
43+
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:
5544

5645
```
57-
$ ./test.js -h
58-
usage: test.js [-h] [-v] [-f FOO] [-b BAR] [--baz BAZ]
46+
$ node prog.js -h
47+
usage: prog.js [-h] [--sum] N [N ...]
48+
49+
Process some integers.
5950
60-
Argparse example
51+
positional arguments:
52+
N an integer for the accumulator
6153
6254
optional arguments:
63-
-h, --help show this help message and exit
64-
-v, --version show program's version number and exit
65-
-f FOO, --foo FOO foo bar
66-
-b BAR, --bar BAR bar foo
67-
--baz BAZ baz bar
55+
-h, --help show this help message and exit
56+
--sum sum the integers (default: find the max)
6857
```
6958

70-
Parse arguments:
59+
When run with the appropriate arguments, it prints either the sum or the max of the command-line integers:
7160

7261
```
73-
$ ./test.js -f=3 --bar=4 --baz 5
74-
{ foo: '3', bar: '4', baz: '5' }
62+
$ node prog.js 1 2 3 4
63+
4
64+
$ node prog.js 1 2 3 4 --sum
65+
10
7566
```
7667

68+
If invalid arguments are passed in, it will issue an error:
69+
70+
```
71+
$ node prog.js a b c
72+
usage: prog.js [-h] [--sum] N [N ...]
73+
prog.js: error: argument N: invalid 'int' value: 'a'
74+
```
75+
76+
This is an example ported from Python. You can find detailed explanation [here](https://docs.python.org/3.9/library/argparse.html).
77+
7778

7879
API docs
7980
--------

0 commit comments

Comments
 (0)