Skip to content

Commit 00c4208

Browse files
committedJan 4, 2020
Remove Command#actionOption()/shortcutOption()
1 parent 9636a19 commit 00c4208

File tree

4 files changed

+60
-72
lines changed

4 files changed

+60
-72
lines changed
 

‎CHANGELOG.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111
- Fixed `Command#showHelp()`, it's now logs help message in console instead of returning it
1212
- Renamed `Command#showHelp()` into `Command#outputHelp()`
1313
- Changed `Command` to store params info (as `Command#params`) even if no params
14-
- Renamed `Command#infoOption()` method into `actionOption()`
15-
- Renamed `Command#shortcut()` method into `shortcutOption()`
16-
- Reworked `Command#shortcut()` method:
17-
- Shortcut method invokes after option value is set
18-
- Pass normalized value to shortcut method
19-
- Ignore unknown keys
20-
- Removed `Command#infoOptionAction` and `infoOptionAction` option for `Command` constructor
14+
- Removed `Command#infoOption()` method, use `action` in option's config instead, i.e. `option(usage, description, { action: ... })`
15+
- Removed `Command#infoOptionAction` and `infoOptionAction` option for `Command` constructor as well
16+
- Removed `Command#shortcut()` method, use `shortcut` in option's config instead, i.e. `option(usage, description, { shortcut: ... })`
2117
- Changed `Command#command()` to raise an exception when subcommand name already in use
2218
- Removed `Command#setOptions()` method
2319
- Removed `Command#setOption()` method
@@ -27,7 +23,7 @@
2723
- Removed `Command#normalize()` method (use `createOptionValues()` instead)
2824
- Changed `Option` to store params info as `Option#params`, it always an object even if no params
2925
- Added `Option#names()` method
30-
- Removed validation for subcommand name
26+
- Removed name validation for subcommands
3127
- Allowed a number for options's short name
3228
- Changed argv parse handlers to [`init()` -> `applyConfig()` -> `prepareContext()`]+ -> `action()`
3329
- Changed exports

‎README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,17 @@ myCommand
6363
.version(value, usage, description, action)
6464
.help(usage, description, action)
6565
.option(usage, description, ...options)
66-
.actionOption(usage, description, action)
67-
.shortcutOption(usage, description, handler, ...options)
6866
.command(nameOrCommand, params, config)
6967
.extend(fn, ...options)
7068
.end()
7169
72-
// argv processing pipeline handlers
70+
// argv processing pipeline handler setters
7371
.init(command, context)
74-
.prepare(context)
72+
.applyConfig(context)
73+
.prerareContenxt(context)
7574
.action(context)
7675
77-
// parse/run methods
76+
// main methods
7877
.parse(argv, suggest)
7978
.run(argv)
8079
@@ -101,7 +100,7 @@ Where `options`:
101100

102101
```
103102
{
104-
value: any, // default value
103+
default: any, // default value
105104
normalize: (value, oldValue) => { ... }, // any value for option is passing through this function and its result stores as option value
106105
shortcut: (value, oldValue) => { ... }, // for shortcut options, the handler is executed after the value is set, and its result (an object) is used as a source of values for other options
107106
action: () => { ... }, // for an action option, which breaks regular args processing and preform and action (e.g. show help or version)

‎lib/command.js

+41-51
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,21 @@ module.exports = class Command {
3939
}
4040

4141
// definition chaining
42+
extend(fn, ...args) {
43+
fn(this, ...args);
44+
return this;
45+
}
4246
description(description) {
4347
this.meta.description = description;
4448

4549
return this;
4650
}
4751
version(version, usage, description, action) {
4852
this.meta.version = version;
49-
this.actionOption(
53+
this.option(
5054
usage || '-v, --version',
5155
description || 'Output version',
52-
action || defaultVersionAction
56+
{ action: action || defaultVersionAction }
5357
);
5458

5559
return this;
@@ -61,10 +65,10 @@ module.exports = class Command {
6165
}
6266

6367
if (usage !== false) {
64-
this.actionOption(
68+
this.option(
6569
usage || '-h, --help',
6670
description || 'Output usage information',
67-
action || defaultHelpAction
71+
{ action: action || defaultHelpAction }
6872
);
6973
this.meta.help = lastAddedOption.get(this);
7074
}
@@ -92,15 +96,6 @@ module.exports = class Command {
9296

9397
return this;
9498
}
95-
actionOption(usage, description, action) {
96-
return this.option(usage, description, { action });
97-
}
98-
shortcutOption(usage, description, shortcut, ...options) {
99-
return this.option(usage, description, {
100-
...Option.normalizeOptions(...options),
101-
shortcut
102-
});
103-
}
10499
command(nameOrCommand, params, config) {
105100
let subcommand;
106101
let name;
@@ -132,11 +127,40 @@ module.exports = class Command {
132127
return host;
133128
}
134129

135-
// helpers
136-
extend(fn, ...args) {
137-
fn(this, ...args);
138-
return this;
130+
// parse & run
131+
parse(argv, suggest) {
132+
let chunk = {
133+
context: {
134+
commandPath: [],
135+
options: null,
136+
args: null,
137+
literalArgs: null
138+
},
139+
next: {
140+
command: this,
141+
argv: argv || process.argv.slice(2)
142+
}
143+
};
144+
145+
do {
146+
chunk = parseArgv(
147+
chunk.next.command,
148+
chunk.next.argv,
149+
chunk.context,
150+
suggest
151+
);
152+
} while (chunk.next);
153+
154+
return chunk;
139155
}
156+
run(argv) {
157+
const chunk = this.parse(argv);
158+
159+
if (typeof chunk.action === 'function') {
160+
return chunk.action.call(null, chunk.context);
161+
}
162+
}
163+
140164
clone(deep) {
141165
const clone = Object.create(Object.getPrototypeOf(this));
142166

@@ -186,40 +210,6 @@ module.exports = class Command {
186210
}), values);
187211
}
188212

189-
// parse & run
190-
parse(argv, suggest) {
191-
let chunk = {
192-
context: {
193-
commandPath: [],
194-
options: null,
195-
args: null,
196-
literalArgs: null
197-
},
198-
next: {
199-
command: this,
200-
argv: argv || process.argv.slice(2)
201-
}
202-
};
203-
204-
do {
205-
chunk = parseArgv(
206-
chunk.next.command,
207-
chunk.next.argv,
208-
chunk.context,
209-
suggest
210-
);
211-
} while (chunk.next);
212-
213-
return chunk;
214-
}
215-
run(argv) {
216-
const chunk = this.parse(argv);
217-
218-
if (typeof chunk.action === 'function') {
219-
return chunk.action.call(null, chunk.context);
220-
}
221-
}
222-
223213
// misc
224214
messageRef() {
225215
return `${this.usage}${this.params.args.map(arg => ` ${arg.name}`)}`;

‎test/command-shortcut-option.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ describe('Command#shortcutOption()', () => {
66
const command = cli.command('test')
77
.option('--foo [foo]', 'Foo', Number)
88
.option('--no-bar', 'Bar')
9-
.shortcutOption('--baz [x]', 'Baz', function(x) {
10-
return {
11-
foo: x,
12-
bar: Boolean(Number(x)),
13-
qux: 'xxx'
14-
};
15-
}, x => x + x);
9+
.option('--baz [x]', 'Baz', {
10+
normalize: x => x + x,
11+
shortcut: function(x) {
12+
return {
13+
foo: x,
14+
bar: Boolean(Number(x)),
15+
qux: 'xxx'
16+
};
17+
}
18+
});
1619

1720
assert.deepEqual(command.run([]).options, {
1821
bar: true

0 commit comments

Comments
 (0)
Please sign in to comment.