Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
#!/usr/bin/env node
2
3
'use strict';
4
5
6
var ArgumentParser = require('../lib/argparse').ArgumentParser;
7
var parser = new ArgumentParser({ description: 'Process some integers.' });
8
9
10
function sum(arr) {
11
return arr.reduce(function (a, b) {
12
return a + b;
13
}, 0);
14
}
15
function max(arr) {
16
return Math.max.apply(Math, arr);
17
}
18
19
20
parser.addArgument(['integers'], {
21
metavar: 'N',
22
type: 'int',
23
nargs: '+',
24
help: 'an integer for the accumulator'
25
});
26
parser.addArgument(['--sum'], {
27
dest: 'accumulate',
28
action: 'storeConst',
29
constant: sum,
30
defaultValue: max,
31
help: 'sum the integers (default: find the max)'
32
});
33
34
var args = parser.parseArgs('--sum 1 2 -1'.split(' '));
35
console.log(args.accumulate(args.integers));
36
37