/
commander-tests.ts
161 lines (132 loc) · 4.47 KB
/
commander-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as program from './index';
interface ExtendedOptions extends program.CommandOptions {
isNew: any;
}
const commandInstance = new program.Command('-f');
const optionsInstance = new program.Option('-f');
const errorInstance = new program.CommanderError(1, 'code', 'message');
const name = program.name();
program.storeOptionsAsProperties(true);
program.passCommandToAction(true);
program
.name('set name')
.version('0.0.1')
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq', 'Add bbq sauce')
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
.parse(process.argv);
console.log('you ordered a pizza with:');
if (program['peppers']) console.log(' - peppers');
if (program['pineapple']) console.log(' - pineapple');
if (program['bbq']) console.log(' - bbq');
console.log(' - %s cheese', program['cheese']);
function range(val: string) {
return val.split('..').map(Number);
}
function list(val: string) {
return val.split(',');
}
function collect(val: string, memo: string[]) {
memo.push(val);
return memo;
}
function increaseVerbosity(v: any, total: number) {
return total + 1;
}
function syncCall() {
console.log("Sync success!");
}
async function asyncCall() {
return;
}
program
.version('0.0.1')
.usage('[options] <file ...>')
.option('-i, --integer <n>', 'An integer argument', parseInt)
.option('-f, --float <n>', 'A float argument', parseFloat)
.option('-r, --range <a>..<b>', 'A range', range)
.option('-l, --list <items>', 'A list', list)
.option('-o, --optional [value]', 'An optional value')
.option('-c, --collect [value]', 'A repeatable value', collect, [])
.option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)
.parse(process.argv);
console.log(' int: %j', program['integer']);
console.log(' float: %j', program['float']);
console.log(' optional: %j', program['optional']);
program['range'] = program['range'] || [];
console.log(' range: %j..%j', program['range'][0], program['range'][1]);
console.log(' list: %j', program['list']);
console.log(' collect: %j', program['collect']);
console.log(' verbosity: %j', program['verbose']);
console.log(' args: %j', program['args']);
program
.version('0.0.1')
.option('-f, --foo', 'enable some foo')
.option('-b, --bar', 'enable some bar')
.option('-B, --baz', 'enable some baz');
// must be before .parse()
program.on('--help', () => {
console.log(' Examples:');
console.log('');
console.log(' $ custom-help --help');
console.log(' $ custom-help -h');
console.log('');
});
program
.command('allow-unknown-option')
.description("description")
.allowUnknownOption()
.action(() => {
console.log('unknown option is allowed');
});
program
.requiredOption('-a,--aaa', 'description')
.requiredOption('-b,--bbb <value>', 'description')
.requiredOption('-c,--ccc [value]', 'description')
.requiredOption('-d,--ddd <value>', 'description', 'default value')
.requiredOption('-e,--eee <value>', 'description', (value, memo) => { return value; })
.requiredOption('-f,--fff <value>', 'description', (value, memo) => { return value; }, 'starting value')
.requiredOption('-g,--ggg <value>', 'description')
.requiredOption('-G,--no-ggg <value>', 'description for negation');
program
.version('0.0.1')
.arguments('<cmd> [env]')
.action((cmd, env) => {
console.log(cmd, env);
});
program
.command("name1", "description")
.command("name2", "description", { isDefault:true });
program
.command("name3").action(syncCall)
.command("name4").action(asyncCall);
const preparedCommand = new program.Command('prepared');
program.addCommand(preparedCommand);
program
.exitOverride();
program.exitOverride((err):never => {
console.log(err.code);
console.log(err.message);
console.log(err.nestedError);
return process.exit(err.exitCode);
});
program.exitOverride((err):void => {
if (err.code !== 'commander.executeSubCommandAsync') {
throw err;
} else {
// Async callback from spawn events, not useful to throw.
}
});
program.parse(process.argv);
program.parse();
program.parse(["foo"], { from: "user" });
program.parseAsync(process.argv).then(() => {
console.log('parseAsync success');
}).catch(err => {
console.log('parseAsync failed');
});
program.help();
program.outputHelp();
const info = program.helpInformation();
console.log('stuff');