Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
add usage printing example to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
harthur committed Apr 8, 2011
1 parent 9c92b20 commit eeae52a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
50 changes: 43 additions & 7 deletions README.md
Expand Up @@ -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: {
Expand All @@ -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});
6 changes: 3 additions & 3 deletions lib/nomnom.js
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit eeae52a

Please sign in to comment.