CLI parsing in node

I'm online!



λ



JS




$ clever deploy




$ clever log




$ clever status

Command name

$ executable
   -a -b
   --test=value
   argument

Flags

$ executable
   -a -b
   --test=value
   argument

Flags (condensed)

$ executable
   -ab
   --test=value
   argument

Flags (long)

$ executable
   --auto --bytes
   --test=value
   argument

Options

$ executable
   -ab
   --test=value
   argument

Options

$ executable
   -ab
   --test value
   argument

Options

$ executable
   -ab
   -t value
   argument

Options

$ executable
   -ab
   -tvalue
   argument

Arguments

$ executable
   -a -b
   --test=value
   argument

Arguments

$ executable
   -a -b
   --test=value
   -- -argument

It's ambiguous

It's tedious

It's already solved

minimist

var parse = require('minimist');

var argv = parse(process.argv.slice(2));

node executable.js
   -ab --test=value
   argument

{ _: ["argument"]
, a: true
, b: true
, test: "value"
}

Automatic Help

yargs


var argv = require('yargs').argv;

if (argv.rif - 5 * argv.xup > 7.138) {
  console.log('Plunder more riffiwobbles!');
} else {
  console.log('Drop the xupptumblers!');
}

var argv = require('yargs')
   .usage('Usage: $0 -x [num] -y [num]')
   .demand(['x','y'])
   .argv;

Commands

nomnom


var parser = require("nomnom");

parser.command('browser')
   .callback(function(opts) {
      runBrowser(opts.url);
   })
   .help("run browser tests");

Subcommands

Tree /= List

"Fluent" my arse

Explicit > Implicit

Declarative

var testCli = cli({
  name: "my-executable",
  description: "Description",
  commands: [
    command(
      "echo",
      { description: "Display the given value",
        args: [
            argument("value",
                     { description: "V" })
        ],
        options: [
            flag("reverse",
                 { aliases: ["r"],
                   description: "Reverse the value"})
        ]
      },
      echoModule)
  ]
});

cliparse.parse(testCli);

CommandTree -> ???

Parser (uses minimist)

Help text

Help flag

./subcommands.js --help
Usage: testCli
Simple CLI written for the sake of the example

Options:
[--help, -?]        Display help about this program
[--version, -V]     Display the version of this program

Available Commands:
help                display help about this program
number              perform simple arithmetic calculations

Help command

./subcommands.js help number    
Usage: testCli number
perform simple arithmetic calculations

Options:
[--help, -?]        Display help about this program
[--version, -V]     Display the version of this program

Available Commands:
add                 add two integers
multiply            multiply two integers

Custom parsers for arguments / values






argument("int",
  { default: 0,
    parser: intParser,
    description: "Int to add 2 to" })

Autocomplete (commands, options)

Demo

Custom Autocomplete

var colorCompleter = function() {
  return autocomplete.words([
   'mauve',
   'blue',
   'yellow',
   'purple',
   'parabolic'
  ]);
};

ToDo list

PRs welcome

divarvel/cliparse-node on GitHub