Permalink
Browse files

add usage printing example to readme

  • Loading branch information...
1 parent 9c92b20 commit eeae52a18e1dc012ff684a4f795e8a9b154d5dd2 @harthur committed Apr 8, 2011
Showing with 46 additions and 10 deletions.
  1. +43 −7 README.md
  2. +3 −3 lib/nomnom.js
View
@@ -35,6 +35,13 @@ for [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
By default, nomnom parses [node](http://nodejs.org/)'s `process.argv`. You can also pass in the args:
var options = nomnom.parseArgs(opts, {}, ["-xvf", "--atomic=true"])
+Values are JSON parsed, so `--debug=true --count=3 --file=log.txt` would give you:
+ { debug: true,
+ count: 3,
+ file: "log.txt"
+ }
+
+### positional args
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`:
var opts = {
filename: {
@@ -45,13 +52,42 @@ All parsed arguments that don't fit the `-a` or `--atomic` format and aren't att
var options = nomnom.parseArgs(opts);
sys.puts(options.filename);
-
-Values are JSON parsed, so `--debug=true --count=3 --file=log.txt` would give you:
- { debug: true,
- count: 3,
- file: "log.txt"
+
+### printing usage
+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:
+
+ nomnom.parseArgs(opts, { printHelp: false });
+
+Usage for these options in `test.js`:
+ var options = {
+ command: {
+ position: 0,
+ help: "either 'test', 'run', or 'xpi'"
+ },
+ config: {
+ string: '-c FILE, --config=FILE',
+ help: 'json file with tests to run',
+ },
+ debug: {
+ string: '-d, --debug',
+ help: 'use debug mode'
+ }
}
+
+...would look like this:
+ Usage: node test.js <command> [options]
+
+ <command> either 'test', 'run', or 'xpi'
+
+ options:
+ -c FILE, --config=FILE json file with tests to run
+ -d, --debug use debug mode
+
+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`:
+ nomnom.parseArgs(opts, { script : "test" });
+
+
+
+
-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:
- nomnom.parseArgs(opts, {printHelp: false});
View
@@ -107,7 +107,7 @@ ArgParser.prototype = {
helpString : function() {
var script = this.script || process.argv[0] + " "
+ require('path').basename(process.argv[1]);
- var str = "usage: " + script;
+ var str = "Usage: " + script;
var positionals = this.options.filter(function(opt) {
return opt.position != undefined;
@@ -121,13 +121,13 @@ ArgParser.prototype = {
str += " [options]\n\n";
positionals.forEach(function(pos) {
- str += pos.name + "\t" + (pos.help || "") + "\n";
+ str += "<" + pos.name + ">\t\t" + (pos.help || "") + "\n";
});
str += "\noptions:\n"
this.options.forEach(function(option) {
if(option.position == undefined)
- str += option.string + "\t" + (option.help || "") + "\n";
+ str += option.string + "\t\t" + (option.help || "") + "\n";
});
return str;
}

0 comments on commit eeae52a

Please sign in to comment.