Skip to content

Commit

Permalink
Remove Command#hasOption(s)/hasCommand(s) methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jan 4, 2020
1 parent 9973491 commit 9636a19
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 42 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- Restored wrongly removed `Command#extend()`
- Removed config argument for `Command`
- Added `Command#clone()` method
- Added `Command#hasCommand()`, `Command#getCommand(name)` and `Command#getCommands()` methods
- Added `Command#getCommand(name)` and `Command#getCommands()` methods
- Added `Command#getOption(name)` and `Command#getOptions()` methods
- Added `Command#messageRef()` and `Option#messageRef()` methods
- Added `Command#createOptionValues(values)` method
Expand All @@ -21,9 +21,13 @@
- Changed `Command#command()` to raise an exception when subcommand name already in use
- Removed `Command#setOptions()` method
- Removed `Command#setOption()` method
- Removed `Command#hasOptions()` method
- Removed `Command#hasOption()` method
- Removed `Command#hasCommands()` method
- Removed `Command#normalize()` method (use `createOptionValues()` instead)
- Changed `Option` to store params info as `Option#params`, it always an object even if no params
- Added `Option#names()` method
- Removed validation for subcommand name
- Allowed a number for options's short name
- Changed argv parse handlers to [`init()` -> `applyConfig()` -> `prepareContext()`]+ -> `action()`
- Changed exports
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,9 @@ myCommand
// misc
.clone(deep)
.createOptionValues()
.hasCommand(name)
.hasCommands()
.getCommand(name)
.getCommands()
.hasOption(name)
.hasOptions()
.getOption()
.getOption(name)
.getOptions()
.outputHelp()
```
Expand Down
18 changes: 1 addition & 17 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module.exports = class Command {
const names = option.names();

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

if (!/^[a-z][a-z0-9\-\_]*$/i.test(name)) {
throw new Error(`Bad subcommand name: ${name}`);
}

// search for existing one
if (this.commands.has(name)) {
throw new Error(
Expand Down Expand Up @@ -228,24 +224,12 @@ module.exports = class Command {
messageRef() {
return `${this.usage}${this.params.args.map(arg => ` ${arg.name}`)}`;
}
hasOption(name) {
return this.options.has(name);
}
hasOptions() {
return this.options.size > 0;
}
getOption(name) {
return this.options.get(name) || null;
}
getOptions() {
return [...new Set(this.options.values())];
}
hasCommand(name) {
return this.commands.has(name);
}
hasCommands() {
return this.commands.size > 0;
}
getCommand(name) {
return this.commands.get(name) || null;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function formatLines(lines) {
}

function commandsHelp(command) {
if (!command.hasCommands()) {
if (command.commands.size === 0) {
return '';
}

Expand All @@ -95,7 +95,7 @@ function commandsHelp(command) {
}

function optionsHelp(command) {
if (!command.hasOptions()) {
if (command.options.size === 0) {
return '';
}

Expand Down Expand Up @@ -138,8 +138,8 @@ module.exports = function getCommandHelp(command, commandPath) {
'Usage:\n\n' +
' ' + chalk.cyan(commandPath) +
args(command.params, chalk.magenta) +
(command.hasOptions() ? ' [' + chalk.yellow('options') + ']' : '') +
(command.hasCommands() ? ' [' + chalk.green('command') + ']' : ''),
(command.options.size !== 0 ? ' [' + chalk.yellow('options') + ']' : '') +
(command.commands.size !== 0 ? ' [' + chalk.green('command') + ']' : ''),
commandsHelp(command) +
optionsHelp(command)
].join('\n');
Expand Down
2 changes: 1 addition & 1 deletion test/command-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Command help', () => {
it('should remove default help when .help(false)', function() {
const command = cli.command('test').help(false);

assert.equal(command.hasOption('help'), false);
assert.strictEqual(command.getOption('help'), null);
});

it('should show help', () => {
Expand Down
24 changes: 12 additions & 12 deletions test/names.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ describe('names', function() {
.option('--bool');

assert([...command.options.keys()], ['--bool', 'bool']);
assert(command.hasOption('--bool'));
assert(command.hasOption('bool'));
assert.notStrictEqual(command.getOption('--bool'), null);
assert.notStrictEqual(command.getOption('bool'), null);
});

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

assert([...command.options.keys()], ['--no-bool', 'bool']);
assert(command.hasOption('--no-bool'));
assert(command.hasOption('bool'));
assert.notStrictEqual(command.getOption('--no-bool'), null);
assert.notStrictEqual(command.getOption('bool'), null);
});

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

assert([...command.options.keys()], ['--bool-option', 'boolOption']);
assert(command.hasOption('--bool-option'));
assert(command.hasOption('boolOption'));
assert.notStrictEqual(command.getOption('--bool-option'), null);
assert.notStrictEqual(command.getOption('boolOption'), null);
});

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

assert([...command.options.keys()], ['--no-bool', 'noBool']);
assert(command.hasOption('--no-bool'));
assert(command.hasOption('noBool'));
assert.notStrictEqual(command.getOption('--no-bool'), null);
assert.notStrictEqual(command.getOption('noBool'), null);
});

it('should be exception if no long form', function() {
Expand All @@ -45,17 +45,17 @@ describe('names', function() {
);
});

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

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

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

assert(command.hasOption('longForm'));
assert.notStrictEqual(command.getOption('longForm'), null);
});
});
4 changes: 2 additions & 2 deletions test/option-one-arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('one arg options', function() {

const { options } = command.run([]);
assert.deepEqual(options, Object.create(null));
assert(command.hasOption('option'));
assert.notStrictEqual(command.getOption('option'), null);
});

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

const { options } = command.run([]);
assert.deepEqual(options, Object.create(null));
assert(command.hasOption('option'));
assert.notStrictEqual(command.getOption('option'), null);
});

it('should store default value', function() {
Expand Down

0 comments on commit 9636a19

Please sign in to comment.