Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 158 lines (121 sloc) 3.907 kb
12001ad @harthur argparse -> nomnom
authored
1 # nomnom
98697eb @harthur change parser options hash into chainable functions
authored
2 nomnom is an option parser for node and CommonJS. It noms your args and gives them back to you in a hash.
0a310cc @harthur initial argparse code plus README and package.json
authored
3
98697eb @harthur change parser options hash into chainable functions
authored
4 var options = require("nomnom")
5 .opts({
8293e17 @harthur an opt can specify a callback
authored
6 debug: {
7 string: '-d, --debug',
8 help: 'Print debugging info'
9 },
98697eb @harthur change parser options hash into chainable functions
authored
10 config: {
11 string: '-c PATH, --config=PATH',
12 default: 'config.json',
13 help: 'JSON file with tests to run'
14 }
15 })
16 .parseArgs();
0a310cc @harthur initial argparse code plus README and package.json
authored
17
18 if(options.debug)
d4bc2fd @harthur mention parse() argv param
authored
19 // do stuff
f1b367b @harthur use getters
authored
20
98697eb @harthur change parser options hash into chainable functions
authored
21 You don't have to specify anything if you don't want to:
80a9cde @harthur readme mistakes
authored
22
98697eb @harthur change parser options hash into chainable functions
authored
23 var options = require("nomnom").parseArgs();
f1b367b @harthur use getters
authored
24
25 var url = options[0]; // get the first positional arg
26 var debug = options.debug // see if --debug was specified
3d8f919 @harthur README formatting
authored
27 var verbose = options.v // see if -v was specified
12001ad @harthur argparse -> nomnom
authored
28
ecdff84 @harthur move around README sections
authored
29 # Install
30 for [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
80a9cde @harthur readme mistakes
authored
31
a06da1a @harthur bump to v0.1.2, update about npm install
authored
32 npm install nomnom
ecdff84 @harthur move around README sections
authored
33
59d4b91 @harthur add note about commands to readme
authored
34 # Commands
80a9cde @harthur readme mistakes
authored
35 Nomnom supports command-based interfaces, e.g. with git: `git add -p` and `git rebase -i` where `add` and `rebase` are the commands:
59d4b91 @harthur add note about commands to readme
authored
36
8293e17 @harthur an opt can specify a callback
authored
37 var parser = nomnom.opts({
38 debug: {
39 string: '-d, --debug',
40 help: 'print debugging info'
41 }
42 });
59d4b91 @harthur add note about commands to readme
authored
43
44 parser.command('sanity')
45 .opts({
46 filename: {
47 position: 1,
48 help: 'test file to run'
49 },
50 config: {
51 string: '-c FILE, --config=FILE',
52 default: 'config.json',
53 help: 'json file with tests to run'
54 }
55 })
56 .callback(function(options) {
57 runSanity(options.filename);
58 })
59 .help("run the sanity tests")
60
61 parser.command('browser')
62 .callback(runBrowser)
63 .help("run browser tests");
64
98697eb @harthur change parser options hash into chainable functions
authored
65 parser.parseArgs();
59d4b91 @harthur add note about commands to readme
authored
66
ecdff84 @harthur move around README sections
authored
67 # More Details
12001ad @harthur argparse -> nomnom
authored
68 By default, nomnom parses [node](http://nodejs.org/)'s `process.argv`. You can also pass in the args:
80a9cde @harthur readme mistakes
authored
69
98697eb @harthur change parser options hash into chainable functions
authored
70 var options = nomnom.parseArgs(["-xvf", "--atomic=true"])
d151e63 @harthur helpString() and test runner
authored
71
eeae52a @harthur add usage printing example to readme
authored
72 Values are JSON parsed, so `--debug=true --count=3 --file=log.txt` would give you:
80a9cde @harthur readme mistakes
authored
73
8293e17 @harthur an opt can specify a callback
authored
74 {
75 "debug": true,
76 "count": 3,
77 "file": "log.txt"
eeae52a @harthur add usage printing example to readme
authored
78 }
79
80 ### positional args
48ce1c9 @harthur formatting fixes for README
authored
81 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`:
80a9cde @harthur readme mistakes
authored
82
8293e17 @harthur an opt can specify a callback
authored
83 var options = nomnom.opts({
84 filename: {
85 position: 0,
86 help: 'file to edit'
87 }
88 }).parseArgs();
657e88a @harthur support for positional args at beginning
authored
89
98697eb @harthur change parser options hash into chainable functions
authored
90 console.log(options.filename);
8293e17 @harthur an opt can specify a callback
authored
91
92 ### callbacks
93 You can provide a callback that will be executed as soon as the arg is encountered. If the callback returns a string it will print the string and exit:
94
95 var opts = {
96 version: {
97 string: '--version'
98 callback: function() {
99 return "version 1.2.4";
100 }
101 },
102 date: {
103 string: '-d YYYY-MM-DD, --date=YYYY-MM-DD',
104 callback: function(date) {
105 if(!(/^\d{4}\-\d\d\-\d\d$/).test(date))
106 return "date must be in format yyyy-mm-dd";
107 }
108 }
109 }
110
111 var options = nomnom.opts(opts).parseArgs();
eeae52a @harthur add usage printing example to readme
authored
112
113 ### printing usage
98697eb @harthur change parser options hash into chainable functions
authored
114 Nomnom prints out a usage message if `--help` or `-h` is an argument. You can override the usage string with `usage`:
eeae52a @harthur add usage printing example to readme
authored
115
98697eb @harthur change parser options hash into chainable functions
authored
116 nomnom.usage("node test.js <filename> --debug");
117
118 override the printing function with `printFunc`:
119
120 nomnom.printFunc(function(usage) {
121 console.log(usage);
122 });
123
124 and add a line to the usage with `help`:
125
126 nomnom.help("runtests.js will run all the tests in the current directory");
eeae52a @harthur add usage printing example to readme
authored
127
128 Usage for these options in `test.js`:
80a9cde @harthur readme mistakes
authored
129
98697eb @harthur change parser options hash into chainable functions
authored
130 var options = nomnom.opts({
eeae52a @harthur add usage printing example to readme
authored
131 command: {
132 position: 0,
133 help: "either 'test', 'run', or 'xpi'"
134 },
135 config: {
136 string: '-c FILE, --config=FILE',
137 help: 'json file with tests to run',
138 },
139 debug: {
140 string: '-d, --debug',
141 help: 'use debug mode'
142 }
98697eb @harthur change parser options hash into chainable functions
authored
143 }).parseArgs();
eeae52a @harthur add usage printing example to readme
authored
144
145 ...would look like this:
80a9cde @harthur readme mistakes
authored
146
eeae52a @harthur add usage printing example to readme
authored
147 Usage: node test.js <command> [options]
148
149 <command> either 'test', 'run', or 'xpi'
150
151 options:
152 -c FILE, --config=FILE json file with tests to run
153 -d, --debug use debug mode
154
98697eb @harthur change parser options hash into chainable functions
authored
155 Nomnom can't detect the alias used to run your script. You can use the `scriptName` option to print the correct name instead of e.g. `node test.js`:
80a9cde @harthur readme mistakes
authored
156
98697eb @harthur change parser options hash into chainable functions
authored
157 nomnom.scriptName("runtests");
Something went wrong with that request. Please try again.