JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 321174
  • Score
    100M100P100Q40126F

Limited option parser

Package Exports

  • nomnom

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nomnom) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

nomnom

nomnom is an option parser for node and CommonJS. It just noms your args and gives them back to you in a hash.

var nomnom = require("nomnom");

var opts = {
  config: {
    string: '-c PATH, --config=PATH',
    default: 'config.json',
    help: 'JSON file with tests to run'
  },
  debug: {
    string: '-d',
    help: 'Use debug mode'
  }
};

var options = nomnom.parseArgs(opts);

if(options.debug)
  // do stuff

You don't even have to specify anything if you don't want to:

var options = 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

Install

for node.js and npm:

npm install nomnom

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 = nomnom();

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.command('browser')
  .callback(runBrowser)
  .help("run browser tests");

parser.parseArgs(globalOpts);

More Details

By default, nomnom parses node's process.argv. You can also pass in the args:

var options = nomnom.parseArgs(opts, { argv: ["-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: {
    position: 0,
    help: 'file to edit'
  }
};
var options = nomnom.parseArgs(opts);

sys.puts(options.filename);

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:

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" });