Skip to content

Instantly share code, notes, and snippets.

@jackboberg
Created April 3, 2015 13:01
Show Gist options
  • Save jackboberg/025fecbab8632ca4c3c0 to your computer and use it in GitHub Desktop.
Save jackboberg/025fecbab8632ca4c3c0 to your computer and use it in GitHub Desktop.
node cli example
var os = require('os')
var cliopts = require('cliclopts')
var parseArgs = require('minimist')
var rc = require('rc')
var pkg = require('../package')
var opts = require('./options')
var clopts = cliopts(opts.default)
var argv = parseArgs(process.argv.slice(2), clopts.options())
exports.help = function () {
console.log(os.EOL, 'Usage: command [options]', os.EOL)
clopts.print()
process.exit()
}
exports.version = function () {
console.log(pkg.version)
process.exit()
}
exports.exec = function () {
// TODO accept conf as argument
console.log('got sub', argv)
var conf = rc(pkg.name, clopts.default(), argv)
console.log('conf', conf)
}
if (require.main === module) {
switch (true) {
case argv.version:
exports.version()
break
case argv._.length > 0:
exports.exec()
break
case argv.help:
default:
exports.help()
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment