cmdr

command line apps made easy

node-cmdr

command line apps made easy

Minimalist framework for command line apps. CoffeeScript demo:

  (require 'cmdr')
  push:
    origin: (target) -> console.log "something like git push origin #{target}"

As I said. Minimalist.

This library is still in development so expect heavy refactoring, broken master, and sparse documentation until I have more time to settle everything.

Please open a github issue if something is broken - thanks!

  • Use node-complete ?: https://github.com/hij1nx/complete
  • Use node-commander to parse args?

BSD (see LICENCE.txt for details)

$ cd myproject
$ npm install cmdr

This will create a copy of node-cmdr an put it inside myproject/node_modules/cmdr

Don't forget to open your package.json and add this to dependencies:

  "cmdr": "0.0.x"
$ npm install cmdr -g
No tests for the moment

To install node-cmdr in a development setup:

$ git clone http://github.com/daizoru/node-cmdr.git
$ cd node-cmdr
$ npm link

To compile sources (located in /src, written in CoffeeScript) to /lib (in JavaScript):

$ npm run-script build
// use 
var cmdr = require('cmdr');
 
// design 
var api = {
  help: {
    pifunction () {
      console.log "don't use me"
    }
  },
 
  foofunction (name) {
    if (name !== undefined) {
      console.log("you need to provide a bar name");
      process.exit(1);
    } else {
      console.log("creating bar #{name}");
      process.exit(0);
    }
  },
 
  list: {
    thingsfunction () {
      // replace by your custom join(), colors, cliff whatever code 
      console.log("- this\n- and this\n- and this");
    }
  },
 
  pifunction () { 
    console.log( 3.1415);
  }
};
 
// profit 
cmdr(api);
 

Now you should be able to use your program in command-line, to call the functions:

  $ myprogram foo
  you need to provide a bar name
 
  $ myprogram foo bara
  creating bar bara
 
  $ myprogram help pi
  don't use me
 
  $ myprogram pi
  3.1415
 
  $ myprogram list things
  - this
  - and this
  - and this

cmdr don't know if you want to emit warnings, errors or log messages, so you have to print everything yourself.

You can find it in /examples

(require 'cmdr')
 
  help:
    pi: -> console.log "very important for a restaurant"
 
  order: (meal="") ->
    unless meal
      console.error "you need to order something"
      process.exit 1
 
    console.log "delivering #{meal}"
 
  list:
    meals: -> console.log "- chicken\n- cheese\n- potatoes"
 
  pi: -> console.log 3.1415