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)
7 CLI arguments parser for node.js. Javascript port of python's
8 [argparse](http://docs.python.org/dev/library/argparse.html) module
9 (original version 3.2). That's a full port, except some very rare options,
10 recorded in issue tracker.
12 **NB. Difference with original.**
14 - Method names changed to camelCase. See [generated docs](http://nodeca.github.com/argparse/).
15 - Use `defaultValue` instead of `default`.
16 - Use `argparse.Const.REMAINDER` instead of `argparse.REMAINDER`, and
17 similarly for constant values `OPTIONAL`, `ZERO_OR_MORE`, and `ONE_OR_MORE`
18 (aliases for `nargs` values `'?'`, `'*'`, `'+'`, respectively), and
31 var ArgumentParser = require('../lib/argparse').ArgumentParser;
32 var parser = new ArgumentParser({
35 description: 'Argparse example'
55 var args = parser.parseArgs();
63 usage: example.js [-h] [-v] [-f FOO] [-b BAR] [--baz BAZ]
68 -h, --help Show this help message and exit.
69 -v, --version Show program's version number and exit.
70 -f FOO, --foo FOO foo bar
71 -b BAR, --bar BAR bar foo
78 $ ./test.js -f=3 --bar=4 --baz 5
79 { foo: '3', bar: '4', baz: '5' }
82 More [examples](https://github.com/nodeca/argparse/tree/master/examples).
85 ArgumentParser objects
86 ======================
89 new ArgumentParser({parameters hash});
92 Creates a new ArgumentParser object.
96 - ```description``` - Text to display before the argument help.
97 - ```epilog``` - Text to display after the argument help.
98 - ```addHelp``` - Add a -h/–help option to the parser. (default: true)
99 - ```argumentDefault``` - Set the global default value for arguments. (default: null)
100 - ```parents``` - A list of ArgumentParser objects whose arguments should also be included.
101 - ```prefixChars``` - The set of characters that prefix optional arguments. (default: ‘-‘)
102 - ```formatterClass``` - A class for customizing the help output.
103 - ```prog``` - The name of the program (default: `path.basename(process.argv[1])`)
104 - ```usage``` - The string describing the program usage (default: generated)
105 - ```conflictHandler``` - Usually unnecessary, defines strategy for resolving conflicting optionals.
107 **Not supported yet**
109 - ```fromfilePrefixChars``` - The set of characters that prefix files from which additional arguments should be read.
112 Details in [original ArgumentParser guide](http://docs.python.org/dev/library/argparse.html#argumentparser-objects)
119 ArgumentParser.addArgument(name or flag or [name] or [flags...], {options})
122 Defines how a single command-line argument should be parsed.
124 - ```name or flag or [name] or [flags...]``` - Either a positional name
125 (e.g., `'foo'`), a single option (e.g., `'-f'` or `'--foo'`), an array
126 of a single positional name (e.g., `['foo']`), or an array of options
127 (e.g., `['-f', '--foo']`).
131 - ```action``` - The basic type of action to be taken when this argument is encountered at the command line.
132 - ```nargs```- The number of command-line arguments that should be consumed.
133 - ```constant``` - A constant value required by some action and nargs selections.
134 - ```defaultValue``` - The value produced if the argument is absent from the command line.
135 - ```type``` - The type to which the command-line argument should be converted.
136 - ```choices``` - A container of the allowable values for the argument.
137 - ```required``` - Whether or not the command-line option may be omitted (optionals only).
138 - ```help``` - A brief description of what the argument does.
139 - ```metavar``` - A name for the argument in usage messages.
140 - ```dest``` - The name of the attribute to be added to the object returned by parseArgs().
142 Details in [original add_argument guide](http://docs.python.org/dev/library/argparse.html#the-add-argument-method)
145 Action (some details)
148 ArgumentParser objects associate command-line arguments with actions.
149 These actions can do just about anything with the command-line arguments associated
150 with them, though most actions simply add an attribute to the object returned by
151 parseArgs(). The action keyword argument specifies how the command-line arguments
152 should be handled. The supported actions are:
154 - ```store``` - Just stores the argument’s value. This is the default action.
155 - ```storeConst``` - Stores value, specified by the const keyword argument.
156 (Note that the const keyword argument defaults to the rather unhelpful None.)
157 The 'storeConst' action is most commonly used with optional arguments, that
158 specify some sort of flag.
159 - ```storeTrue``` and ```storeFalse``` - Stores values True and False
160 respectively. These are special cases of 'storeConst'.
161 - ```append``` - Stores a list, and appends each argument value to the list.
162 This is useful to allow an option to be specified multiple times.
163 - ```appendConst``` - Stores a list, and appends value, specified by the
164 const keyword argument to the list. (Note, that the const keyword argument defaults
165 is None.) The 'appendConst' action is typically used when multiple arguments need
166 to store constants to the same list.
167 - ```count``` - Counts the number of times a keyword argument occurs. For example,
168 used for increasing verbosity levels.
169 - ```help``` - Prints a complete help message for all the options in the current
170 parser and then exits. By default a help action is automatically added to the parser.
171 See ArgumentParser for details of how the output is created.
172 - ```version``` - Prints version information and exit. Expects a `version=`
173 keyword argument in the addArgument() call.
175 Details in [original action guide](http://docs.python.org/dev/library/argparse.html#action)
181 ArgumentParser.addSubparsers()
183 Many programs split their functionality into a number of sub-commands, for
184 example, the svn program can invoke sub-commands like `svn checkout`, `svn update`,
185 and `svn commit`. Splitting up functionality this way can be a particularly good
186 idea when a program performs several different functions which require different
187 kinds of command-line arguments. `ArgumentParser` supports creation of such
188 sub-commands with `addSubparsers()` method. The `addSubparsers()` method is
189 normally called with no arguments and returns an special action object.
190 This object has a single method `addParser()`, which takes a command name and
191 any `ArgumentParser` constructor arguments, and returns an `ArgumentParser` object
192 that can be modified as usual.
201 var ArgumentParser = require('../lib/argparse').ArgumentParser;
202 var parser = new ArgumentParser({
205 description: 'Argparse examples: sub-commands',
208 var subparsers = parser.addSubparsers({
210 dest:"subcommand_name"
213 var bar = subparsers.addParser('c1', {addHelp:true});
221 var bar = subparsers.addParser(
223 {aliases:['co'], addHelp:true}
234 var args = parser.parseArgs();
239 Details in [original sub-commands guide](http://docs.python.org/dev/library/argparse.html#sub-commands)
245 - [Eugene Shkuropat](https://github.com/shkuropat)
246 - [Paul Jacobson](https://github.com/hpaulj)
248 [others](https://github.com/nodeca/argparse/graphs/contributors)
253 Copyright (c) 2012 [Vitaly Puzrin](https://github.com/puzrin).
254 Released under the MIT license. See
255 [LICENSE](https://github.com/nodeca/argparse/blob/master/LICENSE) for details.