72e4261688e8d203b2fb5f4badd7c17f8931c7fc
[platform/framework/web/crosswalk-tizen.git] /
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 var args = parser.parseArgs();
46 console.dir(args);
47 ```
48
49 Display help:
50
51 ```
52 $ ./test.js -h
53 usage: example.js [-h] [-v] [-f FOO] [-b BAR]
54
55 Argparse example
56
57 Optional arguments:
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
62 ```
63
64 Parse arguments:
65
66 ```
67 $ ./test.js -f=3 --bar=4
68 { foo: '3', bar: '4' }
69 ```
70
71 More [examples](https://github.com/nodeca/argparse/tree/master/examples).
72
73
74 ArgumentParser objects
75 ======================
76
77 ```
78 new ArgumentParser({paramters hash});
79 ```
80
81 Creates a new ArgumentParser object.
82
83 **Supported params:**
84
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.
95
96 **Not supportied yet**
97
98 - ```fromfilePrefixChars``` - The set of characters that prefix files from which additional arguments should be read.
99
100
101 Details in [original ArgumentParser guide](http://docs.python.org/dev/library/argparse.html#argumentparser-objects)
102
103
104 addArgument() method
105 ====================
106
107 ```
108 ArgumentParser.addArgument([names or flags], {options})
109 ```
110
111 Defines how a single command-line argument should be parsed.
112
113 - ```name or flags``` - Either a name or a list of option strings, e.g. foo or -f, --foo.
114
115 Options:
116
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().
127
128 Details in [original add_argument guide](http://docs.python.org/dev/library/argparse.html#the-add-argument-method)
129
130
131 Action (some details)
132 ================
133
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:
139
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.
160
161 Details in [original action guide](http://docs.python.org/dev/library/argparse.html#action)
162
163
164 Sub-commands
165 ============
166
167 ArgumentParser.addSubparsers()
168
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.
179
180 Example:
181
182 sub_commands.js
183 ```javascript
184 #!/usr/bin/env node
185 'use strict';
186
187 var ArgumentParser = require('../lib/argparse').ArgumentParser;
188 var parser = new ArgumentParser({
189   version: '0.0.1',
190   addHelp:true,
191   description: 'Argparse examples: sub-commands',
192 });
193
194 var subparsers = parser.addSubparsers({
195   title:'subcommands',
196   dest:"subcommand_name"
197 });
198
199 var bar = subparsers.addParser('c1', {addHelp:true});
200 bar.addArgument(
201   [ '-f', '--foo' ],
202   {
203     action: 'store',
204     help: 'foo3 bar3'
205   }
206 );
207 var bar = subparsers.addParser(
208   'c2',
209   {aliases:['co'], addHelp:true}
210 );
211 bar.addArgument(
212   [ '-b', '--bar' ],
213   {
214     action: 'store',
215     type: 'int',
216     help: 'foo3 bar3'
217   }
218 );
219
220 var args = parser.parseArgs();
221 console.dir(args);
222
223 ```
224
225 Details in [original sub-commands guide](http://docs.python.org/dev/library/argparse.html#sub-commands)
226
227
228 Contributors
229 ============
230
231 - [Eugene Shkuropat](https://github.com/shkuropat)
232 - [Paul Jacobson](https://github.com/hpaulj)
233
234 [others](https://github.com/nodeca/argparse/graphs/contributors)
235
236 License
237 =======
238
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.
242
243