node package manager

Introducing npm Enterprise add-ons. Integrate third-party dev tools into npm…


Command line parsing tool


Command line parsing tool for Node.js.

Here is an example usage:

var commands = require('commands');
var configuration = commands.parse({
    parameter: {},
    short: {
        shortcut: 's'
    switch: {}
}, ['--parameter', 'foo', '-s', 'bar', --switch]); // {parameter: foo, short: bar, switch: true}

Parameter prefix is '--' (two dashes) and rules names are written without it. Every rule should have default value and shortcut for parameter.

Default value is used if argument is empty or undefined.

var rules = {
    foo: {
        default: 'bar'
commands.parse(rules, ['--foo']); // {foo: 'bar'}

Shortcut is only alias for parameter and is prefixed by '-' (dash).

var rules = {
    foo: {
        shortcut: 'f'
commands.parse(rules, ['-f', 'bar']); // {foo: 'bar'}

You can also generate help message:

var rules = {
    foo: {
        shortcut: 'f',
        description: 'I\'m foo!'
    foobar: {
        shortcut: 'b',
        description: 'I\'m foobar!'

Result will be:

--foo, -f    I'm foo!
--foobar, -b I'm foobar!