Skip to content

Commit 9636a19

Browse files
committedJan 4, 2020
Remove Command#hasOption(s)/hasCommand(s) methods
1 parent 9973491 commit 9636a19

File tree

7 files changed

+26
-42
lines changed

7 files changed

+26
-42
lines changed
 

‎CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- Restored wrongly removed `Command#extend()`
44
- Removed config argument for `Command`
55
- Added `Command#clone()` method
6-
- Added `Command#hasCommand()`, `Command#getCommand(name)` and `Command#getCommands()` methods
6+
- Added `Command#getCommand(name)` and `Command#getCommands()` methods
77
- Added `Command#getOption(name)` and `Command#getOptions()` methods
88
- Added `Command#messageRef()` and `Option#messageRef()` methods
99
- Added `Command#createOptionValues(values)` method
@@ -21,9 +21,13 @@
2121
- Changed `Command#command()` to raise an exception when subcommand name already in use
2222
- Removed `Command#setOptions()` method
2323
- Removed `Command#setOption()` method
24+
- Removed `Command#hasOptions()` method
25+
- Removed `Command#hasOption()` method
26+
- Removed `Command#hasCommands()` method
2427
- Removed `Command#normalize()` method (use `createOptionValues()` instead)
2528
- Changed `Option` to store params info as `Option#params`, it always an object even if no params
2629
- Added `Option#names()` method
30+
- Removed validation for subcommand name
2731
- Allowed a number for options's short name
2832
- Changed argv parse handlers to [`init()` -> `applyConfig()` -> `prepareContext()`]+ -> `action()`
2933
- Changed exports

‎README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,9 @@ myCommand
8181
// misc
8282
.clone(deep)
8383
.createOptionValues()
84-
.hasCommand(name)
85-
.hasCommands()
8684
.getCommand(name)
8785
.getCommands()
88-
.hasOption(name)
89-
.hasOptions()
90-
.getOption()
86+
.getOption(name)
9187
.getOptions()
9288
.outputHelp()
9389
```

‎lib/command.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ module.exports = class Command {
7777
const names = option.names();
7878

7979
names.forEach((name, idx) => {
80-
if (this.hasOption(name)) {
80+
if (this.options.has(name)) {
8181
throw new Error(
8282
`${nameType[names.length === 2 ? idx * 2 : idx]} name "${name}" already in use by ${this.getOption(name).messageRef()}`
8383
);
@@ -113,10 +113,6 @@ module.exports = class Command {
113113
subcommand = new Command(name, params, config);
114114
}
115115

116-
if (!/^[a-z][a-z0-9\-\_]*$/i.test(name)) {
117-
throw new Error(`Bad subcommand name: ${name}`);
118-
}
119-
120116
// search for existing one
121117
if (this.commands.has(name)) {
122118
throw new Error(
@@ -228,24 +224,12 @@ module.exports = class Command {
228224
messageRef() {
229225
return `${this.usage}${this.params.args.map(arg => ` ${arg.name}`)}`;
230226
}
231-
hasOption(name) {
232-
return this.options.has(name);
233-
}
234-
hasOptions() {
235-
return this.options.size > 0;
236-
}
237227
getOption(name) {
238228
return this.options.get(name) || null;
239229
}
240230
getOptions() {
241231
return [...new Set(this.options.values())];
242232
}
243-
hasCommand(name) {
244-
return this.commands.has(name);
245-
}
246-
hasCommands() {
247-
return this.commands.size > 0;
248-
}
249233
getCommand(name) {
250234
return this.commands.get(name) || null;
251235
}

‎lib/help.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function formatLines(lines) {
7676
}
7777

7878
function commandsHelp(command) {
79-
if (!command.hasCommands()) {
79+
if (command.commands.size === 0) {
8080
return '';
8181
}
8282

@@ -95,7 +95,7 @@ function commandsHelp(command) {
9595
}
9696

9797
function optionsHelp(command) {
98-
if (!command.hasOptions()) {
98+
if (command.options.size === 0) {
9999
return '';
100100
}
101101

@@ -138,8 +138,8 @@ module.exports = function getCommandHelp(command, commandPath) {
138138
'Usage:\n\n' +
139139
' ' + chalk.cyan(commandPath) +
140140
args(command.params, chalk.magenta) +
141-
(command.hasOptions() ? ' [' + chalk.yellow('options') + ']' : '') +
142-
(command.hasCommands() ? ' [' + chalk.green('command') + ']' : ''),
141+
(command.options.size !== 0 ? ' [' + chalk.yellow('options') + ']' : '') +
142+
(command.commands.size !== 0 ? ' [' + chalk.green('command') + ']' : ''),
143143
commandsHelp(command) +
144144
optionsHelp(command)
145145
].join('\n');

‎test/command-help.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Command help', () => {
1010
it('should remove default help when .help(false)', function() {
1111
const command = cli.command('test').help(false);
1212

13-
assert.equal(command.hasOption('help'), false);
13+
assert.strictEqual(command.getOption('help'), null);
1414
});
1515

1616
it('should show help', () => {

‎test/names.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,35 @@ describe('names', function() {
77
.option('--bool');
88

99
assert([...command.options.keys()], ['--bool', 'bool']);
10-
assert(command.hasOption('--bool'));
11-
assert(command.hasOption('bool'));
10+
assert.notStrictEqual(command.getOption('--bool'), null);
11+
assert.notStrictEqual(command.getOption('bool'), null);
1212
});
1313

1414
it('inverted bool option should be in values and options as normal name and as is in long', function() {
1515
const command = cli.command()
1616
.option('--no-bool');
1717

1818
assert([...command.options.keys()], ['--no-bool', 'bool']);
19-
assert(command.hasOption('--no-bool'));
20-
assert(command.hasOption('bool'));
19+
assert.notStrictEqual(command.getOption('--no-bool'), null);
20+
assert.notStrictEqual(command.getOption('bool'), null);
2121
});
2222

2323
it('dasherized option should store as camelName in options', function() {
2424
const command = cli.command()
2525
.option('--bool-option');
2626

2727
assert([...command.options.keys()], ['--bool-option', 'boolOption']);
28-
assert(command.hasOption('--bool-option'));
29-
assert(command.hasOption('boolOption'));
28+
assert.notStrictEqual(command.getOption('--bool-option'), null);
29+
assert.notStrictEqual(command.getOption('boolOption'), null);
3030
});
3131

3232
it('non-bool option should have name as is', function() {
3333
const command = cli.command()
3434
.option('--no-bool <arg>');
3535

3636
assert([...command.options.keys()], ['--no-bool', 'noBool']);
37-
assert(command.hasOption('--no-bool'));
38-
assert(command.hasOption('noBool'));
37+
assert.notStrictEqual(command.getOption('--no-bool'), null);
38+
assert.notStrictEqual(command.getOption('noBool'), null);
3939
});
4040

4141
it('should be exception if no long form', function() {
@@ -45,17 +45,17 @@ describe('names', function() {
4545
);
4646
});
4747

48-
it('#hasOption should not resolve option name by long form', function() {
48+
it('#getOption() should not resolve option name by long form', function() {
4949
const command = cli.command()
5050
.option('--long-form');
5151

52-
assert(command.hasOption('long-form') === false);
52+
assert.strictEqual(command.getOption('long-form'), null);
5353
});
5454

55-
it('#hasOption should resolve option name by camelName', function() {
55+
it('#getOption() should resolve option name by camelName', function() {
5656
const command = cli.command()
5757
.option('--long-form');
5858

59-
assert(command.hasOption('longForm'));
59+
assert.notStrictEqual(command.getOption('longForm'), null);
6060
});
6161
});

‎test/option-one-arg.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('one arg options', function() {
99

1010
const { options } = command.run([]);
1111
assert.deepEqual(options, Object.create(null));
12-
assert(command.hasOption('option'));
12+
assert.notStrictEqual(command.getOption('option'), null);
1313
});
1414

1515
it('should store default value', function() {
@@ -105,7 +105,7 @@ describe('one arg options', function() {
105105

106106
const { options } = command.run([]);
107107
assert.deepEqual(options, Object.create(null));
108-
assert(command.hasOption('option'));
108+
assert.notStrictEqual(command.getOption('option'), null);
109109
});
110110

111111
it('should store default value', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.