Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 48 lines (35 sloc) 1.368 kB
a940e34 @harthur README md formatting fix
authored
1 # argparse
d151e63 @harthur helpString() and test runner
authored
2 argparse is a small option parser for CommonJS.
0a310cc @harthur initial argparse code plus README and package.json
authored
3
4 var argparse = require("argparse");
5
db5c1ec @harthur fix a confusing variable name in README
authored
6 var opts = [
a940e34 @harthur README md formatting fix
authored
7 { name: 'config',
f427d4a @harthur some README wording
authored
8 string: '-c PATH',
888fda4 @harthur 'full' -> 'long'
authored
9 long: '--config=PATH',
d151e63 @harthur helpString() and test runner
authored
10 default: 'config.json',
11 help: 'JSON file with tests to run'},
0a310cc @harthur initial argparse code plus README and package.json
authored
12
a940e34 @harthur README md formatting fix
authored
13 { name: 'debug',
44e353c @harthur 'flag' -> 'string' in JSON options
authored
14 string: '-d'}
a940e34 @harthur README md formatting fix
authored
15 ];
0a310cc @harthur initial argparse code plus README and package.json
authored
16
db5c1ec @harthur fix a confusing variable name in README
authored
17 var parser = new argparse.ArgParser(opts);
0a310cc @harthur initial argparse code plus README and package.json
authored
18 var options = parser.parse();
19
20 if(options.debug)
d4bc2fd @harthur mention parse() argv param
authored
21 // do stuff
22
ecdff84 @harthur move around README sections
authored
23 # Install
24 for [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
25 git clone http://github.com/harthur/argparse
26 cd argparse
27 npm install .
28
29 # More Details
b489b95 @harthur capitalize some letters in README
authored
30 By default, argparse parses [node](http://nodejs.org/)'s `process.argv`. You can also pass in the args:
d4bc2fd @harthur mention parse() argv param
authored
31 var options = parser.parse(["-xvf", "--atomic=true"])
d151e63 @harthur helpString() and test runner
authored
32
47ed5fb @harthur positional args can be anywhere in arg array
authored
33 All parsed arguments that don't follow the form '-a' or '--atomic' and can't be attached to an option are positional and can be matched on via the `position`:
657e88a @harthur support for positional args at beginning
authored
34 var opts = [
35 { name: 'filename',
36 position: 0,
37 default: 'test.js'},
38 ];
39
40 var parser = new argparse.ArgParser(opts);
41 var options = parser.parse();
42
43 sys.puts(options.filename);
44
888fda4 @harthur 'full' -> 'long'
authored
45 Argparse prints out a usage message if `--help` or `-h` is an argument. You can disable this with the `printHelp` flag and specify the printing function with `printFunc` if you're not using node:
806ccb4 @harthur print out help string
authored
46
888fda4 @harthur 'full' -> 'long'
authored
47 var parser = new argparse.ArgParser(opts, {printHelp: false});
Something went wrong with that request. Please try again.