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