4 [](http://travis-ci.org/nodeca/argparse)
5 [](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`.
27 var ArgumentParser = require('../lib/argparse').ArgumentParser;
28 var parser = new ArgumentParser({
31 description: 'Argparse example'
45 var args = parser.parseArgs();
53 usage: example.js [-h] [-v] [-f FOO] [-b BAR]
58 -h, --help Show this help message and exit.
59 -v, --version Show program's version number and exit.
60 -f FOO, --foo FOO foo bar
61 -b BAR, --bar BAR bar foo
67 $ ./test.js -f=3 --bar=4
68 { foo: '3', bar: '4' }
71 More [examples](https://github.com/nodeca/argparse/tree/master/examples).
74 ArgumentParser objects
75 ======================
78 new ArgumentParser({paramters hash});
81 Creates a new ArgumentParser object.
85 - ```description``` - Text to display before the argument help.
86 - ```epilog``` - Text to display after the argument help.
87 - ```addHelp``` - Add a -h/–help option to the parser. (default: true)
88 - ```argumentDefault``` - Set the global default value for arguments. (default: null)
89 - ```parents``` - A list of ArgumentParser objects whose arguments should also be included.
90 - ```prefixChars``` - The set of characters that prefix optional arguments. (default: ‘-‘)
91 - ```formatterClass``` - A class for customizing the help output.
92 - ```prog``` - The name of the program (default: `path.basename(process.argv[1])`)
93 - ```usage``` - The string describing the program usage (default: generated)
94 - ```conflictHandler``` - Usually unnecessary, defines strategy for resolving conflicting optionals.
96 **Not supportied yet**
98 - ```fromfilePrefixChars``` - The set of characters that prefix files from which additional arguments should be read.
101 Details in [original ArgumentParser guide](http://docs.python.org/dev/library/argparse.html#argumentparser-objects)
108 ArgumentParser.addArgument([names or flags], {options})
111 Defines how a single command-line argument should be parsed.
113 - ```name or flags``` - Either a name or a list of option strings, e.g. foo or -f, --foo.
117 - ```action``` - The basic type of action to be taken when this argument is encountered at the command line.
118 - ```nargs```- The number of command-line arguments that should be consumed.
119 - ```constant``` - A constant value required by some action and nargs selections.
120 - ```defaultValue``` - The value produced if the argument is absent from the command line.
121 - ```type``` - The type to which the command-line argument should be converted.
122 - ```choices``` - A container of the allowable values for the argument.
123 - ```required``` - Whether or not the command-line option may be omitted (optionals only).
124 - ```help``` - A brief description of what the argument does.
125 - ```metavar``` - A name for the argument in usage messages.
126 - ```dest``` - The name of the attribute to be added to the object returned by parseArgs().
128 Details in [original add_argument guide](http://docs.python.org/dev/library/argparse.html#the-add-argument-method)
131 Action (some details)
134 ArgumentParser objects associate command-line arguments with actions.
135 These actions can do just about anything with the command-line arguments associated
136 with them, though most actions simply add an attribute to the object returned by
137 parseArgs(). The action keyword argument specifies how the command-line arguments
138 should be handled. The supported actions are:
140 - ```store``` - Just stores the argument’s value. This is the default action.
141 - ```storeConst``` - Stores value, specified by the const keyword argument.
142 (Note that the const keyword argument defaults to the rather unhelpful None.)
143 The 'storeConst' action is most commonly used with optional arguments, that
144 specify some sort of flag.
145 - ```storeTrue``` and ```storeFalse``` - Stores values True and False
146 respectively. These are special cases of 'storeConst'.
147 - ```append``` - Stores a list, and appends each argument value to the list.
148 This is useful to allow an option to be specified multiple times.
149 - ```appendConst``` - Stores a list, and appends value, specified by the
150 const keyword argument to the list. (Note, that the const keyword argument defaults
151 is None.) The 'appendConst' action is typically used when multiple arguments need
152 to store constants to the same list.
153 - ```count``` - Counts the number of times a keyword argument occurs. For example,
154 used for increasing verbosity levels.
155 - ```help``` - Prints a complete help message for all the options in the current
156 parser and then exits. By default a help action is automatically added to the parser.
157 See ArgumentParser for details of how the output is created.
158 - ```version``` - Prints version information and exit. Expects a `version=`
159 keyword argument in the addArgument() call.
161 Details in [original action guide](http://docs.python.org/dev/library/argparse.html#action)
167 ArgumentParser.addSubparsers()
169 Many programs split their functionality into a number of sub-commands, for
170 example, the svn program can invoke sub-commands like `svn checkout`, `svn update`,
171 and `svn commit`. Splitting up functionality this way can be a particularly good
172 idea when a program performs several different functions which require different
173 kinds of command-line arguments. `ArgumentParser` supports creation of such
174 sub-commands with `addSubparsers()` method. The `addSubparsers()` method is
175 normally called with no arguments and returns an special action object.
176 This object has a single method `addParser()`, which takes a command name and
177 any `ArgumentParser` constructor arguments, and returns an `ArgumentParser` object
178 that can be modified as usual.
187 var ArgumentParser = require('../lib/argparse').ArgumentParser;
188 var parser = new ArgumentParser({
191 description: 'Argparse examples: sub-commands',
194 var subparsers = parser.addSubparsers({
196 dest:"subcommand_name"
199 var bar = subparsers.addParser('c1', {addHelp:true});
207 var bar = subparsers.addParser(
209 {aliases:['co'], addHelp:true}
220 var args = parser.parseArgs();
225 Details in [original sub-commands guide](http://docs.python.org/dev/library/argparse.html#sub-commands)
231 - [Eugene Shkuropat](https://github.com/shkuropat)
232 - [Paul Jacobson](https://github.com/hpaulj)
234 [others](https://github.com/nodeca/argparse/graphs/contributors)
239 Copyright (c) 2012 [Vitaly Puzrin](https://github.com/puzrin).
240 Released under the MIT license. See
241 [LICENSE](https://github.com/nodeca/argparse/blob/master/LICENSE) for details.