Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 58 lines (44 sloc) 1.72 kB
12001ad @harthur argparse -> nomnom
authored
1 # nomnom
bd770e5 @harthur fix some usage whitespace issues
authored
2 nomnom is a small option parser for node and CommonJS. It just parses your args and gives them back to you in a hash.
0a310cc @harthur initial argparse code plus README and package.json
authored
3
12001ad @harthur argparse -> nomnom
authored
4 var nomnom = require("nomnom");
0a310cc @harthur initial argparse code plus README and package.json
authored
5
db5c1ec @harthur fix a confusing variable name in README
authored
6 var opts = [
a940e34 @harthur README md formatting fix
authored
7 { name: 'config',
5553efa @harthur combine short 'string' and 'long' into 'string'
authored
8 string: '-c PATH, --config=PATH',
d151e63 @harthur helpString() and test runner
authored
9 default: 'config.json',
f1b367b @harthur use getters
authored
10 help: 'JSON file with tests to run'
11 },
a940e34 @harthur README md formatting fix
authored
12 { name: 'debug',
f1b367b @harthur use getters
authored
13 string: '-d',
14 help: 'Use debug mode'
15 }
a940e34 @harthur README md formatting fix
authored
16 ];
88586d5 @harthur shortcut nomnom.parseArgs()
authored
17
18 var options = nomnom.parseArgs(opts);
0a310cc @harthur initial argparse code plus README and package.json
authored
19
20 if(options.debug)
d4bc2fd @harthur mention parse() argv param
authored
21 // do stuff
f1b367b @harthur use getters
authored
22
23 You don't even have to specify anything if you don't want to:
24 var options = nomnom.parseArgs();
25
26 var url = options[0]; // get the first positional arg
27 var debug = options.debug // see if --debug was specified
28 var verbose = options.v // see if -v was specified
12001ad @harthur argparse -> nomnom
authored
29
ecdff84 @harthur move around README sections
authored
30 # Install
31 for [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
a06da1a @harthur bump to v0.1.2, update about npm install
authored
32 npm install nomnom
ecdff84 @harthur move around README sections
authored
33
34 # More Details
12001ad @harthur argparse -> nomnom
authored
35 By default, nomnom parses [node](http://nodejs.org/)'s `process.argv`. You can also pass in the args:
712aa03 @harthur switch around arguments to parseArgs()
authored
36 var options = nomnom.parseArgs(opts, {}, ["-xvf", "--atomic=true"])
d151e63 @harthur helpString() and test runner
authored
37
48ce1c9 @harthur formatting fixes for README
authored
38 All parsed arguments that don't fit the `-a` or `--atomic` format and aren't attached to an option are positional and can be matched on via the `position`:
657e88a @harthur support for positional args at beginning
authored
39 var opts = [
40 { name: 'filename',
f1b367b @harthur use getters
authored
41 position: 0,
42 help: 'file to edit'
43 }
657e88a @harthur support for positional args at beginning
authored
44 ];
88586d5 @harthur shortcut nomnom.parseArgs()
authored
45 var options = nomnom.parseArgs(opts);
657e88a @harthur support for positional args at beginning
authored
46
47 sys.puts(options.filename);
48
f1b367b @harthur use getters
authored
49 Values are JSON parsed, so `--debug=true --count=3 --file=log.txt` would give you:
50 { debug: true,
51 count: 3,
52 file: "log.txt"
53 }
12001ad @harthur argparse -> nomnom
authored
54
55 Nomnom 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
56
f1b367b @harthur use getters
authored
57 nomnom.parseArgs(opts, {printHelp: false});
Something went wrong with that request. Please try again.