Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 94 lines (69 sloc) 2.458 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
260373c @harthur use the cleaner hash syntax for options in readme example
authored
6 var opts = {
7 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 },
260373c @harthur use the cleaner hash syntax for options in readme example
authored
12 debug: {
f1b367b @harthur use getters
authored
13 string: '-d',
14 help: 'Use debug mode'
15 }
260373c @harthur use the cleaner hash syntax for options in readme example
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
3d8f919 @harthur README formatting
authored
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
eeae52a @harthur add usage printing example to readme
authored
38 Values are JSON parsed, so `--debug=true --count=3 --file=log.txt` would give you:
39 { debug: true,
40 count: 3,
41 file: "log.txt"
42 }
43
44 ### positional args
48ce1c9 @harthur formatting fixes for README
authored
45 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`:
260373c @harthur use the cleaner hash syntax for options in readme example
authored
46 var opts = {
47 filename: {
f1b367b @harthur use getters
authored
48 position: 0,
49 help: 'file to edit'
50 }
260373c @harthur use the cleaner hash syntax for options in readme example
authored
51 };
88586d5 @harthur shortcut nomnom.parseArgs()
authored
52 var options = nomnom.parseArgs(opts);
657e88a @harthur support for positional args at beginning
authored
53
54 sys.puts(options.filename);
eeae52a @harthur add usage printing example to readme
authored
55
56 ### printing usage
57 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:
58
59 nomnom.parseArgs(opts, { printHelp: false });
60
61 Usage for these options in `test.js`:
62 var options = {
63 command: {
64 position: 0,
65 help: "either 'test', 'run', or 'xpi'"
66 },
67 config: {
68 string: '-c FILE, --config=FILE',
69 help: 'json file with tests to run',
70 },
71 debug: {
72 string: '-d, --debug',
73 help: 'use debug mode'
74 }
f1b367b @harthur use getters
authored
75 }
eeae52a @harthur add usage printing example to readme
authored
76
77 ...would look like this:
78 Usage: node test.js <command> [options]
79
80 <command> either 'test', 'run', or 'xpi'
81
82 options:
83 -c FILE, --config=FILE json file with tests to run
84 -d, --debug use debug mode
85
86 Nomnom can't detect the alias used to run your script. You can use the `script` option to print the correct name instead of e.g. `node test.js`:
87 nomnom.parseArgs(opts, { script : "test" });
88
89
90
91
12001ad @harthur argparse -> nomnom
authored
92
806ccb4 @harthur print out help string
authored
93
Something went wrong with that request. Please try again.