Created
April 3, 2015 13:01
-
-
Save jackboberg/025fecbab8632ca4c3c0 to your computer and use it in GitHub Desktop.
node cli example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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