Skip to content
Myr

Added default option value support

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 05a1816 · Browse files · 2 files+42 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+42 added−5 removed

  • other:+42 added−5 removed

2 files+42 added−5 removed

added:examples/defaults+16 added
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program10 .version('0.0.1')11 .option('-t, --template-engine [engine]', 'Add template [engine] support', 'jade')12 .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')13 .parse(process.argv);1415console.log(' - %s template engine', program.templateEngine);16console.log(' - %s cheese', program.cheese);
modified:lib/commander.js+26 added-5 removed
247 unmodified lines
248 * program.chdir249 * // => "/tmp"250 *251 * // optional argument252 * program.option('-c, --cheese [type]', 'add cheese [cheddar]');252 * program.option('-c, --cheese [type]', 'add cheese [marble]');253 *254 * @param {String} flags255 * @param {String} description256 * @param {Function} fn256 * @param {Function|Mixed} fn or default257 * @param {Mixed} defaultValue258 * @return {Command} for chaining259 * @api public260 */261261Command.prototype.option = function(flags, description, fn){262Command.prototype.option = function(flags, description, fn, defaultValue){263 var self = this264 , option = new Option(flags, description)264 , name = option.name();265 , name = camelcase(option.name());266267 // default as 3rd arg268 if ('function' != typeof fn) defaultValue = fn, fn = null;269270 // when --no-* we default to true267 if (false == option.bool) self[name] = true;271 if (false == option.bool) defaultValue = true;272273 // default274 if (undefined !== defaultValue) self[name] = defaultValue;275276 // register the option277 this.options.push(option);278279 // when it's passed assign the value
565 unmodified lines
845 again();846};847848/**849 * Camel-case the given `flag`850 *851 * @param {String} flag852 * @return {String}853 * @api private854 */855856function camelcase(flag) {857 return flag.split('-').reduce(function(str, word){858 return str + word[0].toUpperCase() + word.slice(1);859 });860}861862/**863 * Parse a boolean `str`.864 *865 * @param {String} str866 * @return {Boolean}
20 unmodified lines