Skip to content

Commit

Permalink
Port python example to README
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed May 10, 2022
1 parent 70fc26e commit a645a9a
Showing 1 changed file with 42 additions and 41 deletions.
83 changes: 42 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,61 @@ More details in [doc](./doc).
Example
-------

`test.js` file:

```javascript
#!/usr/bin/env node
'use strict';

const { ArgumentParser } = require('argparse');
const { version } = require('./package.json');

// Formatter with support of `\n` in Help texts.
class HelpFormatter extends ArgumentParser.RawDescriptionHelpFormatter {
// executes parent _split_lines for each line of the help, then flattens the result
_split_lines(text, width) {
return [].concat(...text.split('\n').map(line => super._split_lines(line, width)));
}
}

const parser = new ArgumentParser({
description: 'Argparse example',
add_help: true,
formatter_class: HelpFormatter
});

parser.add_argument('-v', '--version', { action: 'version', version });
parser.add_argument('-f', '--foo', { help: 'foo bar' });
parser.add_argument('-b', '--bar', { help: 'bar foo' });
parser.add_argument('--baz', { help: 'baz bar' });

console.dir(parser.parse_args());
Following code is a JS program that takes a list of integers and produces either the sum or the max:

```js
const { ArgumentParser } = require('argparse')

const parser = new ArgumentParser({ description: 'Process some integers.' })

let sum = ints => ints.reduce((a, b) => a + b)
let max = ints => ints.reduce((a, b) => a > b ? a : b)

parser.add_argument('integers', { metavar: 'N', type: 'int', nargs: '+',
help: 'an integer for the accumulator' })
parser.add_argument('--sum', { dest: 'accumulate', action: 'store_const',
const: sum, default: max,
help: 'sum the integers (default: find the max)' });

let args = parser.parse_args()
console.log(args.accumulate(args.integers))
```

Display help:
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:

```
$ ./test.js -h
usage: test.js [-h] [-v] [-f FOO] [-b BAR] [--baz BAZ]
$ node prog.js -h
usage: prog.js [-h] [--sum] N [N ...]
Process some integers.
Argparse example
positional arguments:
N an integer for the accumulator
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-f FOO, --foo FOO foo bar
-b BAR, --bar BAR bar foo
--baz BAZ baz bar
-h, --help show this help message and exit
--sum sum the integers (default: find the max)
```

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

```
$ ./test.js -f=3 --bar=4 --baz 5
{ foo: '3', bar: '4', baz: '5' }
$ node prog.js 1 2 3 4
4
$ node prog.js 1 2 3 4 --sum
10
```

If invalid arguments are passed in, it will issue an error:

```
$ node prog.js a b c
usage: prog.js [-h] [--sum] N [N ...]
prog.js: error: argument N: invalid 'int' value: 'a'
```

This is an example ported from Python. You can find detailed explanation [here](https://docs.python.org/3.9/library/argparse.html).


API docs
--------
Expand Down

0 comments on commit a645a9a

Please sign in to comment.