commander
Advanced tools
Comparing version 0.0.1 to 0.0.3
0.0.3 / 2011-08-15 | ||
================== | ||
* Added default option value support | ||
0.0.2 / 2011-08-15 | ||
================== | ||
* Added mask support to `Command#password(str[, mask], fn)` | ||
* Added `Command#password(str, fn)` | ||
0.0.1 / 2010-01-03 | ||
@@ -3,0 +14,0 @@ ================== |
@@ -14,2 +14,3 @@ | ||
, path = require('path') | ||
, tty = require('tty') | ||
, basename = path.basename; | ||
@@ -252,7 +253,8 @@ | ||
* // optional argument | ||
* program.option('-c, --cheese [type]', 'add cheese [cheddar]'); | ||
* program.option('-c, --cheese [type]', 'add cheese [marble]'); | ||
* | ||
* @param {String} flags | ||
* @param {String} description | ||
* @param {Function} fn | ||
* @param {Function|Mixed} fn or default | ||
* @param {Mixed} defaultValue | ||
* @return {Command} for chaining | ||
@@ -262,10 +264,16 @@ * @api public | ||
Command.prototype.option = function(flags, description, fn){ | ||
Command.prototype.option = function(flags, description, fn, defaultValue){ | ||
var self = this | ||
, option = new Option(flags, description) | ||
, name = option.name(); | ||
, name = camelcase(option.name()); | ||
// default as 3rd arg | ||
if ('function' != typeof fn) defaultValue = fn, fn = null; | ||
// when --no-* we default to true | ||
if (false == option.bool) self[name] = true; | ||
if (false == option.bool) defaultValue = true; | ||
// default | ||
if (undefined !== defaultValue) self[name] = defaultValue; | ||
// register the option | ||
@@ -705,10 +713,10 @@ this.options.push(option); | ||
* | ||
* program.prompt('Username: ', function(name){ | ||
* console.log('hi %s', name); | ||
* }); | ||
* program.prompt('Username: ', function(name){ | ||
* console.log('hi %s', name); | ||
* }); | ||
* | ||
* program.prompt('Description:', function(desc){ | ||
* console.log('description was "%s"', desc.trim()); | ||
* }); | ||
* | ||
* program.prompt('Description:', function(desc){ | ||
* console.log('description was "%s"', desc.trim()); | ||
* }); | ||
* | ||
* @param {String} str | ||
@@ -725,2 +733,57 @@ * @param {Function} fn | ||
/** | ||
* Prompt for password with `str`, `mask` char and callback `fn(val)`. | ||
* | ||
* The mask string defaults to '', aka no output is | ||
* written while typing, you may want to use "*" etc. | ||
* | ||
* Examples: | ||
* | ||
* program.password('Password: ', function(pass){ | ||
* console.log('got "%s"', pass); | ||
* process.stdin.destroy(); | ||
* }); | ||
* | ||
* program.password('Password: ', '*', function(pass){ | ||
* console.log('got "%s"', pass); | ||
* process.stdin.destroy(); | ||
* }); | ||
* | ||
* @param {String} str | ||
* @param {String} mask | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
Command.prototype.password = function(str, mask, fn){ | ||
// default mask | ||
if ('function' == typeof mask) { | ||
fn = mask; | ||
mask = ''; | ||
} | ||
tty.setRawMode(true); | ||
var buf = ''; | ||
process.stdout.write(str); | ||
// keypress | ||
process.stdin.on('keypress', function(c, key){ | ||
if ('enter' == key.name) { | ||
console.log(); | ||
process.stdin.removeAllListeners('keypress'); | ||
tty.setRawMode(false); | ||
fn(buf); | ||
return; | ||
} | ||
if (key && key.ctrl && 'c' == key.name) { | ||
console.log('%s', buf); | ||
process.exit(); | ||
} | ||
process.stdout.write(mask); | ||
buf += c; | ||
}).resume(); | ||
}; | ||
/** | ||
* Confirmation prompt with `str` and callback `fn(bool)` | ||
@@ -791,2 +854,16 @@ * | ||
/** | ||
* Camel-case the given `flag` | ||
* | ||
* @param {String} flag | ||
* @return {String} | ||
* @api private | ||
*/ | ||
function camelcase(flag) { | ||
return flag.split('-').reduce(function(str, word){ | ||
return str + word[0].toUpperCase() + word.slice(1); | ||
}); | ||
} | ||
/** | ||
* Parse a boolean `str`. | ||
@@ -793,0 +870,0 @@ * |
{ | ||
"name": "commander" | ||
, "version": "0.0.1" | ||
, "version": "0.0.3" | ||
, "description": "the complete solution for node.js command-line programs" | ||
@@ -5,0 +5,0 @@ , "keywords": ["command", "option", "parser", "prompt", "stdin"] |
@@ -28,3 +28,3 @@ | ||
.option('-b, --bbq', 'Add bbq sauce') | ||
.option('-c, --cheese <type>', 'Add the specified type of cheese [marble]') | ||
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble') | ||
.parse(process.argv); | ||
@@ -36,6 +36,6 @@ | ||
if (program.bbq) console.log(' - bbq'); | ||
console.log(' - %s cheese', program.cheese || 'marble'); | ||
console.log(' - %s cheese', program.cheese); | ||
``` | ||
Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. | ||
Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc. | ||
@@ -123,2 +123,22 @@ ## Automated --help | ||
## .password(msg[, mask], fn) | ||
Prompt for password without echoing: | ||
```js | ||
program.password('Password: ', function(pass){ | ||
console.log('got "%s"', pass); | ||
process.stdin.destroy(); | ||
}); | ||
``` | ||
Prompt for password with mask char "*": | ||
```js | ||
program.password('Password: ', '*', function(pass){ | ||
console.log('got "%s"', pass); | ||
process.stdin.destroy(); | ||
}); | ||
``` | ||
## .confirm(msg, fn) | ||
@@ -147,2 +167,9 @@ | ||
## Links | ||
- [ascii tables](https://github.com/LearnBoost/cli-table) | ||
- [progress bars](https://github.com/visionmedia/node-progress) | ||
- [more progress bars](https://github.com/substack/node-multimeter) | ||
- [examples](https://github.com/visionmedia/commander.js/tree/master/examples) | ||
## License | ||
@@ -149,0 +176,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
31963
25
865
194