-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommander-test.js
More file actions
101 lines (85 loc) · 2.82 KB
/
commander-test.js
File metadata and controls
101 lines (85 loc) · 2.82 KB
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
'use strict';
const assert = require('node:assert/strict');
(async () => {
const { Command, Option, Argument } = require('commander');
// Basic option parsing
const program = new Command();
program
.option('-p, --port <number>', 'server port')
.option('-v, --verbose', 'enable verbose logging')
.option('--no-color', 'disable color output');
program.parse(['--port', '8080', '--verbose'], { from: 'user' });
const opts = program.opts();
assert.equal(opts.port, '8080');
assert.equal(opts.verbose, true);
assert.equal(opts.color, true, 'negatable boolean defaults to true');
// Parsing with --no- prefix
const prog2 = new Command();
prog2.option('--no-color', 'disable color');
prog2.parse(['--no-color'], { from: 'user' });
assert.equal(prog2.opts().color, false);
// Required option with default value
const prog3 = new Command();
prog3.option('-t, --timeout <ms>', 'timeout in ms', '3000');
prog3.parse([], { from: 'user' });
assert.equal(prog3.opts().timeout, '3000');
// Arguments (positional)
const prog4 = new Command();
let parsedSource = '';
prog4
.argument('<source>', 'source file')
.argument('[destination]', 'destination file', 'out.txt')
.action((source, destination) => {
parsedSource = source;
});
prog4.parse(['input.txt'], { from: 'user' });
assert.equal(parsedSource, 'input.txt');
// Sub-commands
const prog5 = new Command();
let subCmdRan = false;
let subCmdName = '';
prog5
.command('serve')
.option('--host <addr>', 'bind address', 'localhost')
.action((options) => {
subCmdRan = true;
subCmdName = 'serve';
assert.equal(options.host, '0.0.0.0');
});
prog5
.command('build')
.option('--minify', 'minify output')
.action(() => {
subCmdRan = true;
subCmdName = 'build';
});
prog5.parse(['serve', '--host', '0.0.0.0'], { from: 'user' });
assert.equal(subCmdRan, true, 'sub-command action should run');
assert.equal(subCmdName, 'serve');
// Option with custom processing function
const prog6 = new Command();
prog6.option(
'-n, --number <value>',
'a number',
(val) => parseInt(val, 10),
0
);
prog6.parse(['-n', '42'], { from: 'user' });
assert.equal(prog6.opts().number, 42);
assert.equal(typeof prog6.opts().number, 'number');
// Option class can be used directly
const opt = new Option('-d, --debug', 'enable debug mode');
assert.equal(opt.long, '--debug');
assert.equal(opt.short, '-d');
// Argument class
if (Argument) {
const arg = new Argument('<file>', 'input file');
assert.equal(arg.required, true);
assert.equal(arg.description, 'input file');
}
console.log('commander-test:ok');
})().catch((err) => {
console.error('commander-test:fail');
console.error(err && err.stack ? err.stack : String(err));
process.exit(1);
});