-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some commander/optimist/nopt examples script
- Loading branch information
Showing
13 changed files
with
345 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var program = require('commander'); | ||
|
||
function range(val) { | ||
return val.split('..').map(Number); | ||
} | ||
|
||
function list(val) { | ||
return val.split(','); | ||
} | ||
|
||
|
||
|
||
|
||
program | ||
.version('0.0.1') | ||
.option('-i, --integer <n>', 'An integer argument', parseInt) | ||
.option('-f, --float <n>', 'A float argument', parseFloat) | ||
.option('-r, --range <a>..<b>', 'A range', range) | ||
.option('-l, --list <items>', 'A list', list) | ||
.option('-o, --optional [value]', 'An optional value') | ||
.parse(process.argv); | ||
|
||
|
||
// return console.log(program.options); | ||
|
||
console.log(' int: %j', program.integer); | ||
console.log(' float: %j', program.float); | ||
console.log(' optional: %j', program.optional); | ||
program.range = program.range || []; | ||
console.log(' range: %j..%j', program.range[0], program.range[1]); | ||
console.log(' list: %j', program.list); | ||
console.log(' args: %j', program.args); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var program = require('../'); | ||
|
||
function list(val) { | ||
return val.split(',').map(Number); | ||
} | ||
|
||
program | ||
.version('0.0.1') | ||
.option('-f, --foo', 'enable some foo') | ||
.option('-b, --bar', 'enable some bar') | ||
.option('-B, --baz', 'enable some baz'); | ||
|
||
// must be before .parse() since | ||
// node's emit() is immediate | ||
|
||
program.on('--help', function(){ | ||
console.log(' Examples:'); | ||
console.log(''); | ||
console.log(' $ custom-help --help'); | ||
console.log(' $ custom-help -h'); | ||
console.log(''); | ||
}); | ||
|
||
program.parse(process.argv); | ||
|
||
return console.log(program.options); | ||
|
||
console.log('stuff'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var program = require('../'); | ||
|
||
function list(val) { | ||
return val.split(',').map(Number); | ||
} | ||
|
||
program | ||
.version('0.0.1') | ||
.option('-t, --template-engine [engine]', 'Add template [engine] support', 'jade') | ||
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble') | ||
.option('-l, --list [items]', 'Specify list items defaulting to 1,2,3', list, [1,2,3]) | ||
.parse(process.argv); | ||
|
||
console.log(' - %s template engine', program.templateEngine); | ||
console.log(' - %s cheese', program.cheese); | ||
console.log(' - %j', program.list); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var program = require('../'); | ||
|
||
program | ||
.version('0.0.1') | ||
.option('-C, --chdir <path>', 'change the working directory') | ||
.option('-c, --config <path>', 'set config path. defaults to ./deploy.conf') | ||
.option('-T, --no-tests', 'ignore test hook') | ||
|
||
program | ||
.command('setup [env]') | ||
.description('run setup commands for all envs') | ||
.action(function(env){ | ||
env = env || 'all'; | ||
console.log('setup for %s env(s)', env); | ||
}); | ||
|
||
program | ||
.command('exec <cmd>') | ||
.description('execute the given remote cmd') | ||
.action(function(cmd){ | ||
console.log('exec "%s"', cmd); | ||
}); | ||
|
||
program | ||
.command('*') | ||
.action(function(env){ | ||
console.log('deploying "%s"', env); | ||
}); | ||
|
||
program.parse(process.argv); | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var program = require('../'); | ||
|
||
program | ||
.version('0.0.1') | ||
.option('-s, --sessions', 'add session support') | ||
.option('-t, --template <engine>', 'specify template engine (jade|ejs) [jade]', 'jade') | ||
.option('-c, --css <engine>', 'specify stylesheet engine (stylus|sass|less) [css]', 'css') | ||
.parse(process.argv); | ||
|
||
console.log(' - sessions %j', program.sessions); | ||
console.log(' - template %j', program.template); | ||
console.log(' - css %j', program.css); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var program = require('../'); | ||
|
||
program | ||
.version('0.0.1') | ||
.option('-p, --peppers', 'Add peppers') | ||
.option('-P, --pineapple', 'Add pineapple') | ||
.option('-b, --bbq', 'Add bbq sauce') | ||
.option('-c, --cheese <type>', 'Add the specified type of cheese [marble]') | ||
.option('-C, --no-cheese', 'You do not want any cheese') | ||
.parse(process.argv); | ||
|
||
console.log('you ordered a pizza with:'); | ||
if (program.peppers) console.log(' - peppers'); | ||
if (program.pineappe) console.log(' - pineapple'); | ||
if (program.bbq) console.log(' - bbq'); | ||
|
||
var cheese = true === program.cheese | ||
? 'marble' | ||
: program.cheese || 'no'; | ||
|
||
console.log(' - %s cheese', cheese); | ||
console.log(program.args); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env node | ||
|
||
//process.env.DEBUG_NOPT = 1 | ||
|
||
// my-program.js | ||
var nopt = require("../lib/nopt") | ||
, Stream = require("stream").Stream | ||
, path = require("path") | ||
, knownOpts = { "foo" : [String, null] | ||
, "bar" : [Stream, Number] | ||
, "baz" : path | ||
, "bloo" : [ "big", "medium", "small" ] | ||
, "flag" : Boolean | ||
, "pick" : Boolean | ||
} | ||
, shortHands = { "foofoo" : ["--foo", "Mr. Foo"] | ||
, "b7" : ["--bar", "7"] | ||
, "m" : ["--bloo", "medium"] | ||
, "p" : ["--pick"] | ||
, "f" : ["--flag", "true"] | ||
, "g" : ["--flag"] | ||
, "s" : "--flag" | ||
} | ||
// everything is optional. | ||
// knownOpts and shorthands default to {} | ||
// arg list defaults to process.argv | ||
// slice defaults to 2 | ||
, parsed = nopt(knownOpts, shortHands, process.argv, 2) | ||
|
||
console.log("parsed =\n"+ require("util").inspect(parsed)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env node | ||
|
||
//process.env.DEBUG_NOPT = 1 | ||
|
||
// my-program.js: https://raw.github.com/isaacs/nopt/master/examples/my-program.js | ||
// | ||
var nopt = require('nopt') | ||
, Stream = require("stream").Stream | ||
, path = require("path") | ||
, knownOpts = { "foo" : [String, null] | ||
, "bar" : [Stream, Number] | ||
, "baz" : path | ||
, "bloo" : [ "big", "medium", "small" ] | ||
, "flag" : Boolean | ||
, "pick" : Boolean | ||
} | ||
, shortHands = { "foofoo" : ["--foo", "Mr. Foo"] | ||
, "b7" : ["--bar", "7"] | ||
, "m" : ["--bloo", "medium"] | ||
, "p" : ["--pick"] | ||
, "f" : ["--flag", "true"] | ||
, "g" : ["--flag"] | ||
, "s" : "--flag" | ||
} | ||
// everything is optional. | ||
// knownOpts and shorthands default to {} | ||
// arg list defaults to process.argv | ||
// slice defaults to 2 | ||
, parsed = nopt(knownOpts, shortHands, process.argv, 2) | ||
|
||
// exports knownOpts and aliases so that we could play with them and the completion | ||
exports.opts = knownOpts | ||
exports.alias = shortHands | ||
|
||
console.log("parsed =\n"+ require("util").inspect(parsed, false, 2, true)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env node | ||
var argv = require('optimist') | ||
.usage('Count the lines in a file.\nUsage: $0') | ||
.demand('f') | ||
.alias('f', 'file') | ||
.describe('f', 'Load a file') | ||
.argv | ||
; | ||
|
||
var fs = require('fs'); | ||
var s = fs.createReadStream(argv.file); | ||
|
||
var lines = 0; | ||
s.on('data', function (buf) { | ||
lines += buf.toString().match(/\n/g).length; | ||
}); | ||
|
||
s.on('end', function () { | ||
console.log(lines); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env node | ||
var argv = require('optimist') | ||
.usage('Count the lines in a file.\nUsage: $0') | ||
.options({ | ||
file : { | ||
demand : true, | ||
alias : 'f', | ||
description : 'Load a file' | ||
}, | ||
base : { | ||
alias : 'b', | ||
description : 'Numeric base to use for output', | ||
default : 10, | ||
}, | ||
}) | ||
.argv | ||
; | ||
|
||
var fs = require('fs'); | ||
var s = fs.createReadStream(argv.file); | ||
|
||
var lines = 0; | ||
s.on('data', function (buf) { | ||
lines += buf.toString().match(/\n/g).length; | ||
}); | ||
|
||
s.on('end', function () { | ||
console.log(lines.toString(argv.base)); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env node | ||
var argv = require('optimist') | ||
.usage('Count the lines in a file.\nUsage: $0') | ||
.wrap(80) | ||
.demand('f') | ||
.alias('f', [ 'file', 'filename' ]) | ||
.describe('f', | ||
"Load a file. It's pretty important." | ||
+ " Required even. So you'd better specify it." | ||
) | ||
.alias('b', 'base') | ||
.describe('b', 'Numeric base to display the number of lines in') | ||
.default('b', 10) | ||
.describe('x', 'Super-secret optional parameter which is secret') | ||
.default('x', '') | ||
.argv | ||
; | ||
|
||
var fs = require('fs'); | ||
var s = fs.createReadStream(argv.file); | ||
|
||
var lines = 0; | ||
s.on('data', function (buf) { | ||
lines += buf.toString().match(/\n/g).length; | ||
}); | ||
|
||
s.on('end', function () { | ||
console.log(lines.toString(argv.base)); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var optimist = require('optimist'); | ||
|
||
var argv = optimist.usage('This is my awesome program', { | ||
'about': { | ||
description: 'Provide some details about the author of this program', | ||
required: true, | ||
short: 'a', | ||
}, | ||
'info': { | ||
description: 'Provide some information about the node.js agains!!!!!!', | ||
boolean: true, | ||
short: 'i' | ||
} | ||
}).argv; | ||
|
||
optimist.showHelp(); | ||
|
||
console.log('\n\nInspecting options'); | ||
console.dir(argv); |
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