node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »


Elerium\Commands Build Status

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'}

Help generating

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!