Version 1
[yaffs-website] / node_modules / argparse / README.md
1 argparse
2 ========
3
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)
6
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.
11
12 **NB. Difference with original.**
13
14 - Method names changed to camelCase. See [generated docs](http://nodeca.github.com/argparse/).
15 - Use `defaultValue` instead of `default`.
16
17
18 Example
19 =======
20
21 test.js file:
22
23 ```javascript
24 #!/usr/bin/env node
25 'use strict';
26
27 var ArgumentParser = require('../lib/argparse').ArgumentParser;
28 var parser = new ArgumentParser({
29   version: '0.0.1',
30   addHelp:true,
31   description: 'Argparse example'
32 });
33 parser.addArgument(
34   [ '-f', '--foo' ],
35   {
36     help: 'foo bar'
37   }
38 );
39 parser.addArgument(
40   [ '-b', '--bar' ],
41   {
42     help: 'bar foo'
43   }
44 );
45 parser.addArgument(
46   '--baz',
47   {
48     help: 'baz bar'
49   }
50 );
51 var args = parser.parseArgs();
52 console.dir(args);
53 ```
54
55 Display help:
56
57 ```
58 $ ./test.js -h
59 usage: example.js [-h] [-v] [-f FOO] [-b BAR] [--baz BAZ]
60
61 Argparse example
62
63 Optional arguments:
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
68   --baz BAZ          baz bar
69 ```
70
71 Parse arguments:
72
73 ```
74 $ ./test.js -f=3 --bar=4 --baz 5
75 { foo: '3', bar: '4', baz: '5' }
76 ```
77
78 More [examples](https://github.com/nodeca/argparse/tree/master/examples).
79
80
81 ArgumentParser objects
82 ======================
83
84 ```
85 new ArgumentParser({paramters hash});
86 ```
87
88 Creates a new ArgumentParser object.
89
90 **Supported params:**
91
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.
102
103 **Not supportied yet**
104
105 - ```fromfilePrefixChars``` - The set of characters that prefix files from which additional arguments should be read.
106
107
108 Details in [original ArgumentParser guide](http://docs.python.org/dev/library/argparse.html#argumentparser-objects)
109
110
111 addArgument() method
112 ====================
113
114 ```
115 ArgumentParser.addArgument(name or flag or [name] or [flags...], {options})
116 ```
117
118 Defines how a single command-line argument should be parsed.
119
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']`).
124
125 Options:
126
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().
137
138 Details in [original add_argument guide](http://docs.python.org/dev/library/argparse.html#the-add-argument-method)
139
140
141 Action (some details)
142 ================
143
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:
149
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.
170
171 Details in [original action guide](http://docs.python.org/dev/library/argparse.html#action)
172
173
174 Sub-commands
175 ============
176
177 ArgumentParser.addSubparsers()
178
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.
189
190 Example:
191
192 sub_commands.js
193 ```javascript
194 #!/usr/bin/env node
195 'use strict';
196
197 var ArgumentParser = require('../lib/argparse').ArgumentParser;
198 var parser = new ArgumentParser({
199   version: '0.0.1',
200   addHelp:true,
201   description: 'Argparse examples: sub-commands',
202 });
203
204 var subparsers = parser.addSubparsers({
205   title:'subcommands',
206   dest:"subcommand_name"
207 });
208
209 var bar = subparsers.addParser('c1', {addHelp:true});
210 bar.addArgument(
211   [ '-f', '--foo' ],
212   {
213     action: 'store',
214     help: 'foo3 bar3'
215   }
216 );
217 var bar = subparsers.addParser(
218   'c2',
219   {aliases:['co'], addHelp:true}
220 );
221 bar.addArgument(
222   [ '-b', '--bar' ],
223   {
224     action: 'store',
225     type: 'int',
226     help: 'foo3 bar3'
227   }
228 );
229
230 var args = parser.parseArgs();
231 console.dir(args);
232
233 ```
234
235 Details in [original sub-commands guide](http://docs.python.org/dev/library/argparse.html#sub-commands)
236
237
238 Contributors
239 ============
240
241 - [Eugene Shkuropat](https://github.com/shkuropat)
242 - [Paul Jacobson](https://github.com/hpaulj)
243
244 [others](https://github.com/nodeca/argparse/graphs/contributors)
245
246 License
247 =======
248
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.
252
253