forked from tj/commander.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storeOptionsAsProperties-action.js
41 lines (34 loc) · 1.26 KB
/
storeOptionsAsProperties-action.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
40
41
#!/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.
// In addition, you can pass just the options to the action handler
// instead of a commmand object. This allows simpler code, and is more consistent
// with the previous behaviour so less code changes from old code.
//
// Example output:
//
// $ node storeOptionsAsProperties-action.js show
// undefined
// undefined
//
// $ node storeOptionsAsProperties-action.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
.passCommandToAction(false); // <--- change behaviour
program
.name('my-program-name')
.option('-n,--name <name>');
program
.command('show')
.option('-a,--action <action>')
.action((options) => { // <--- passed options, not Command
console.log(options.action); // <--- matches old code
});
program.parse(process.argv);
const programOptions = program.opts(); // <--- use opts to access option values
console.log(programOptions.name);