Skip to content
Myr

coercion

This file

Path

examples/coercion

46 lines · 1.2 KiB

Last changed by

Merge pull request #198 from AlphaHydrae/repeated-optionsTJ Holowaychukbefore adoption41bc6a9

History of this file

Take it

The walk stopped after 200 commits: lines older than 778faa3 point at it.

miscTj Holowaychukbefore adoption778faa3

lines 1–16why, one step back
1#!/usr/bin/env node
23/**4 * Module dependencies.5 */67var program = require('../');89function range(val) {10 return val.split('..').map(Number);11}1213function list(val) {14 return val.split(',');15}16

Merge pull request #198 from AlphaHydrae/repeated-optionsTJ Holowaychukbefore adoption41bc6a9

lines 17–25why, one step back
17function collect(val, memo) {
18 memo.push(val);19 return memo;20}2122function increaseVerbosity(v, total) {23 return total + 1;24}25

miscTj Holowaychukbefore adoption778faa3

lines 26–27why, one step back
26program
27 .version('0.0.1')

Added .usage() example in docsTj Holowaychukbefore adoption2127275

line 28why, one step back
28 .usage('test')

miscTj Holowaychukbefore adoption778faa3

lines 29–33why, one step back
29 .option('-i, --integer <n>', 'An integer argument', parseInt)
30 .option('-f, --float <n>', 'A float argument', parseFloat)31 .option('-r, --range <a>..<b>', 'A range', range)32 .option('-l, --list <items>', 'A list', list)33 .option('-o, --optional [value]', 'An optional value')

Merge pull request #198 from AlphaHydrae/repeated-optionsTJ Holowaychukbefore adoption41bc6a9

lines 34–35why, one step back
34 .option('-c, --collect [value]', 'A repeatable value', collect, [])
35 .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)

miscTj Holowaychukbefore adoption778faa3

lines 36–43why, one step back
36 .parse(process.argv);
3738console.log(' int: %j', program.integer);39console.log(' float: %j', program.float);40console.log(' optional: %j', program.optional);41program.range = program.range || [];42console.log(' range: %j..%j', program.range[0], program.range[1]);43console.log(' list: %j', program.list);

Merge pull request #198 from AlphaHydrae/repeated-optionsTJ Holowaychukbefore adoption41bc6a9

lines 44–45why, one step back
44console.log(' collect: %j', program.collect);
45console.log(' verbosity: %j', program.verbose);

miscTj Holowaychukbefore adoption778faa3

line 46why, one step back
46console.log(' args: %j', program.args);