-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
storeOptionsAsProperties-opts.js
executable file
·39 lines (32 loc) · 1.06 KB
/
storeOptionsAsProperties-opts.js
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
#!/usr/bin/env node
// To avoid possible name clashes, you can change the default behaviour
// of storing the options values as properties on the command object.
// You access the option values using the .opts() function.
//
// Example output:
//
// $ node storeOptionsAsProperties-opts.js show
// undefined
// undefined
//
// $ node storeOptionsAsProperties-opts.js --name foo show --action jump
// jump
// foo
// const commander = require('commander'); // (normal include)
const commander = require('../'); // include commander in git clone of commander repo
const program = new commander.Command();
program
.storeOptionsAsProperties(false); // <--- change behaviour
program
.name('my-program-name')
.option('-n,--name <name>');
program
.command('show')
.option('-a,--action <action>')
.action((cmd) => {
const options = cmd.opts(); // <--- use opts to access option values
console.log(options.action);
});
program.parse(process.argv);
const programOptions = program.opts(); // <--- use opts to access option values
console.log(programOptions.name);