Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

readme prettier

  • Loading branch information...
commit 18b15dcb58b45737a6b26d9fefcf3516335c4f74 1 parent 3f98ad0
@harthur authored
Showing with 92 additions and 90 deletions.
  1. +90 −88 README.md
  2. +2 −2 package.json
View
178 README.md
@@ -1,37 +1,41 @@
# nomnom
nomnom is an option parser for node and CommonJS. It noms your args and gives them back to you in a hash.
- var options = require("nomnom")
- .opts({
- version: {
- string: '--version',
- help: 'print version and exit',
- callback: function() {
- return "version 1.2.4";
- }
- },
- debug : {
- string: '-d, --debug',
- help: 'Print debugging info'
- },
- config: {
- string: '-c PATH, --config=PATH',
- default: 'config.json',
- help: 'JSON file with tests to run'
- }
- })
- .parseArgs();
-
- if(options.debug)
- // do stuff
+```javascript
+var options = require("nomnom")
+ .opts({
+ version: {
+ string: '--version',
+ help: 'print version and exit',
+ callback: function() {
+ return "version 1.2.4";
+ }
+ },
+ debug : {
+ string: '-d, --debug',
+ help: 'Print debugging info'
+ },
+ config: {
+ string: '-c PATH, --config=PATH',
+ default: 'config.json',
+ help: 'JSON file with tests to run'
+ }
+ })
+ .parseArgs();
+
+if(options.debug)
+ // do stuff
+```
You don't have to specify anything if you don't want to:
- var options = require("nomnom").parseArgs();
+```javascript
+var options = require("nomnom").parseArgs();
- var url = options[0]; // get the first positional arg
- var debug = options.debug // see if --debug was specified
- var verbose = options.v // see if -v was specified
+var url = options[0]; // get the first positional arg
+var debug = options.debug // see if --debug was specified
+var verbose = options.v // see if -v was specified
+```
# Install
for [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
@@ -41,31 +45,33 @@ for [node.js](http://nodejs.org/) and [npm](http://github.com/isaacs/npm):
# Commands
Nomnom supports command-based interfaces (e.g. with git: `git add -p` and `git rebase -i` where `add` and `rebase` are the commands):
- var parser = require("nomnom");
-
- parser.command('browser')
- .callback(runBrowser)
- .help("run browser tests");
-
- parser.command('sanity')
- .opts({
- filename: {
- position: 1,
- help: 'test file to run'
- },
- config: {
- string: '-c FILE, --config=FILE',
- default: 'config.json',
- help: 'json file with tests to run'
- }
- })
- .callback(function(options) {
- runSanity(options.filename);
- })
- .help("run the sanity tests")
-
- parser.parseArgs();
-
+```javascript
+var parser = require("nomnom");
+
+parser.command('browser')
+ .callback(runBrowser)
+ .help("run browser tests");
+
+parser.command('sanity')
+ .opts({
+ filename: {
+ position: 1,
+ help: 'test file to run'
+ },
+ config: {
+ string: '-c FILE, --config=FILE',
+ default: 'config.json',
+ help: 'json file with tests to run'
+ }
+ })
+ .callback(function(options) {
+ runSanity(options.filename);
+ })
+ .help("run the sanity tests")
+
+parser.parseArgs();
+```
+
Each command generates its own usage message when `-h` or `--help` is specified with the command.
# More Details
@@ -82,20 +88,22 @@ Values are JSON parsed, so `--debug=true --count=3 --file=log.txt` would give yo
# Usage
Nomnom prints out a usage message if `--help` or `-h` is an argument. Usage for these options in `test.js`:
- var options = nomnom.opts({
- action: {
- position: 0,
- help: "either 'test', 'run', or 'xpi'"
- },
- config: {
- string: '-c FILE, --config=FILE',
- help: 'Config file with tests to run',
- },
- debug: {
- string: '-d, --debug',
- help: 'Print debugging info'
- }
- }).parseArgs();
+```javascript
+var options = nomnom.opts({
+ action: {
+ position: 0,
+ help: "either 'test', 'run', or 'xpi'"
+ },
+ config: {
+ string: '-c FILE, --config=FILE',
+ help: 'Config file with tests to run',
+ },
+ debug: {
+ string: '-d, --debug',
+ help: 'Print debugging info'
+ }
+}).parseArgs();
+```
...would look like this:
@@ -135,21 +143,17 @@ The value to give the option if it's not specified in the arguments.
A callback that will be executed as soon as the option is encountered. If the callback returns a string it will print the string and exit:
- var opts = {
- version: {
- string: '--version',
- callback: function() {
- return "version 1.2.4";
- }
- },
- date: {
- string: '-y YYYY-MM-DD, --date=YYYY-MM-DD',
- callback: function(date) {
- if(!(/^\d{4}\-\d\d\-\d\d$/).test(date))
- return "date must be in format yyyy-mm-dd";
- }
- }
- }
+```javascript
+var opts = {
+ count: {
+ string: '-n COUNT',
+ callback: function(count) {
+ if(count != parseInt(count))
+ return "count must be an integer";
+ }
+ }
+}
+```
#### position
@@ -193,11 +197,7 @@ Nomnom can't detect the alias used to run your script. You can use `scriptName`
#### printFunc
-Overrides the usage printing function:
-
- nomnom.printFunc(function(usage) {
- console.log(usage);
- });
+Overrides the usage printing function.
#### command
@@ -205,7 +205,7 @@ Takes a command name and gives you a command object on which you can chain comma
#### callback
-A callback that will be called with the parsed options. If any commands are specified, this is only called if no command was used.
+A callback that will be called with the parsed options. If a command is expected, this is the fallback callback when no command is specified.
#### globalOpts
@@ -215,7 +215,9 @@ The global options when commands are specified. Any options in here will be incl
Parses node's `process.argv` and returns the parsed options hash. You can also provide argv:
- var options = nomnom.parseArgs(["-xvf", "--atomic=true"])
+```javascript
+var options = nomnom.parseArgs(["-xvf", "--atomic=true"])
+```
# Command interface
A command is specified with `nomnom.command('name')`. All these functions can be chained on a command:
View
4 package.json
@@ -1,6 +1,6 @@
{
"name": "nomnom",
- "description": "Option parser with support for usage and commands",
+ "description": "Option parser with generated usage and commands",
"version": "0.4.8",
"author": "Heather Arthur <fayearthur@gmail.com>",
"repository": {
@@ -8,7 +8,7 @@
"url": "http://github.com/harthur/nomnom.git"
},
"main": "./nomnom",
- "keywords": ["arguments", "option parser", "command line", "options"],
+ "keywords": ["arguments", "option parser", "command line", "options", "parser"],
"dependencies": {
"underscore": ">= 1.1.5"
}
Please sign in to comment.
Something went wrong with that request. Please try again.