Skip to content
Myr

Merge pull request #967 from shadowspawn/feature/readme-bits-and-pieces

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

Add TypeScript to README, and remove option RegExpr

authored by John Gee · committed by GitHub · parents 3598303, da3efe7 · Browse files · 3 files+19 added−29 removed
This commit

State

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

Diff stats

3 files+19 added−29 removed

  • other:+19 added−29 removed

3 files+19 added−29 removed

deleted:examples/regex-17 removed
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);
modified:index.js+2 added
377 unmodified lines
378379 // default as 3rd arg380 if (typeof fn !== 'function') {381 if (fn instanceof RegExp) {382 // This is a bit simplistic (especially no error messages), and probably better handled by caller using custom option processing.383 // No longer documented in README, but still present for backwards compatibility.384 var regex = fn;385 fn = function(val, def) {386 var m = regex.exec(val);387 return m ? m[0] : def;
842 unmodified lines
modified:Readme.md+17 added-12 removed
245 unmodified lines
246```247248A command's options are validated when the command is used. Any unknown options will be reported as an error. However, if an action-based command does not define an action, then the options are not validated.249250## Regular Expression251```js252program253 .version('0.1.0')254 .option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')255 .option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)256 .parse(process.argv);257258console.log(' size: %j', program.size);259console.log(' drink: %j', program.drink);260```261250## Variadic arguments251252 The last argument of a command can be variadic, and only the last argument. To make an argument variadic you have to253 append `...` to the argument name. Here is an example:
199 unmodified lines
453 process.exit(1);454});455```456457## Bits and pieces458459### TypeScript460461The Commander package includes its TypeScript Definition file, but also requires the node types which you need to install yourself. e.g.462463```bash464npm install commander465npm install --save-dev @types/node466```467468If you use `ts-node` and git-style sub-commands written as `.ts` files, you need to call your program through node to get the sub-commands called correctly. e.g.469470```bash471node -r ts-node/register pm.ts472```473474## Examples475476```js477var program = require('commander');
46 unmodified lines