Skip to content
Myr

Merge pull request #337 from palanik/master

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

Allowing RegEx options

authored by Randall Koutnik · committed by Randall Koutnik · parents b1ede65, f21e416 · Browse files · 4 files+56 added−2 removed
This commit

State

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

Diff stats

4 files+56 added−2 removed

  • tests:+16 added−0 removed
  • other:+40 added−2 removed

4 files+56 added−2 removed

added:examples/regex+17 added
1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var program = require('../');89program10 .version('0.0.1')11 .usage('test')12 .option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')13 .option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)14 .parse(process.argv);15 16console.log(' size: %j', program.size);17console.log(' drink: %j', program.drink);
added:test/test.options.regex.js+16 added
modified:index.js+11 added-2 removed
347 unmodified lines
348 , name = camelcase(oname);349350 // default as 3rd arg351 if (typeof fn != 'function') {352 if (fn instanceof RegExp) {353 var regex = fn;354 fn = function(val, def) {355 var m = regex.exec(val);356 return m ? m[0] : def;357 }358 }359 else {352 defaultValue = fn;353 fn = null;360 defaultValue = fn;361 fn = null;362 }363 }364365 // preassign default value only for --no-*, [optional], or <required>366 if (false == option.bool || option.optional || option.required) {
679 unmodified lines
modified:Readme.md+12 added
84 unmodified lines
85console.log(' verbosity: %j', program.verbose);86console.log(' args: %j', program.args);87```8889## Regular Expression90```js91program92 .version('0.0.1')93 .option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')94 .option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)95 .parse(process.argv);96 97console.log(' size: %j', program.size);98console.log(' drink: %j', program.drink);99```100101## Variadic arguments102103 The last argument of a command can be variadic, and only the last argument. To make an argument variadic you have to104 append `...` to the argument name. Here is an example:
223 unmodified lines