Skip to content

Commit

Permalink
Remove Command#actionOption()/shortcutOption()
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jan 4, 2020
1 parent 9636a19 commit 00c4208
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 72 deletions.
12 changes: 4 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@
- Fixed `Command#showHelp()`, it's now logs help message in console instead of returning it
- Renamed `Command#showHelp()` into `Command#outputHelp()`
- Changed `Command` to store params info (as `Command#params`) even if no params
- Renamed `Command#infoOption()` method into `actionOption()`
- Renamed `Command#shortcut()` method into `shortcutOption()`
- Reworked `Command#shortcut()` method:
- Shortcut method invokes after option value is set
- Pass normalized value to shortcut method
- Ignore unknown keys
- Removed `Command#infoOptionAction` and `infoOptionAction` option for `Command` constructor
- Removed `Command#infoOption()` method, use `action` in option's config instead, i.e. `option(usage, description, { action: ... })`
- Removed `Command#infoOptionAction` and `infoOptionAction` option for `Command` constructor as well
- Removed `Command#shortcut()` method, use `shortcut` in option's config instead, i.e. `option(usage, description, { shortcut: ... })`
- Changed `Command#command()` to raise an exception when subcommand name already in use
- Removed `Command#setOptions()` method
- Removed `Command#setOption()` method
Expand All @@ -27,7 +23,7 @@
- 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
- Removed name validation for subcommands
- Allowed a number for options's short name
- Changed argv parse handlers to [`init()` -> `applyConfig()` -> `prepareContext()`]+ -> `action()`
- Changed exports
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,17 @@ myCommand
.version(value, usage, description, action)
.help(usage, description, action)
.option(usage, description, ...options)
.actionOption(usage, description, action)
.shortcutOption(usage, description, handler, ...options)
.command(nameOrCommand, params, config)
.extend(fn, ...options)
.end()
// argv processing pipeline handlers
// argv processing pipeline handler setters
.init(command, context)
.prepare(context)
.applyConfig(context)
.prerareContenxt(context)
.action(context)
// parse/run methods
// main methods
.parse(argv, suggest)
.run(argv)
Expand All @@ -101,7 +100,7 @@ Where `options`:

```
{
value: any, // default value
default: any, // default value
normalize: (value, oldValue) => { ... }, // any value for option is passing through this function and its result stores as option value
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
action: () => { ... }, // for an action option, which breaks regular args processing and preform and action (e.g. show help or version)
Expand Down
92 changes: 41 additions & 51 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,21 @@ module.exports = class Command {
}

// definition chaining
extend(fn, ...args) {
fn(this, ...args);
return this;
}
description(description) {
this.meta.description = description;

return this;
}
version(version, usage, description, action) {
this.meta.version = version;
this.actionOption(
this.option(
usage || '-v, --version',
description || 'Output version',
action || defaultVersionAction
{ action: action || defaultVersionAction }
);

return this;
Expand All @@ -61,10 +65,10 @@ module.exports = class Command {
}

if (usage !== false) {
this.actionOption(
this.option(
usage || '-h, --help',
description || 'Output usage information',
action || defaultHelpAction
{ action: action || defaultHelpAction }
);
this.meta.help = lastAddedOption.get(this);
}
Expand Down Expand Up @@ -92,15 +96,6 @@ module.exports = class Command {

return this;
}
actionOption(usage, description, action) {
return this.option(usage, description, { action });
}
shortcutOption(usage, description, shortcut, ...options) {
return this.option(usage, description, {
...Option.normalizeOptions(...options),
shortcut
});
}
command(nameOrCommand, params, config) {
let subcommand;
let name;
Expand Down Expand Up @@ -132,11 +127,40 @@ module.exports = class Command {
return host;
}

// helpers
extend(fn, ...args) {
fn(this, ...args);
return this;
// parse & run
parse(argv, suggest) {
let chunk = {
context: {
commandPath: [],
options: null,
args: null,
literalArgs: null
},
next: {
command: this,
argv: argv || process.argv.slice(2)
}
};

do {
chunk = parseArgv(
chunk.next.command,
chunk.next.argv,
chunk.context,
suggest
);
} while (chunk.next);

return chunk;
}
run(argv) {
const chunk = this.parse(argv);

if (typeof chunk.action === 'function') {
return chunk.action.call(null, chunk.context);
}
}

clone(deep) {
const clone = Object.create(Object.getPrototypeOf(this));

Expand Down Expand Up @@ -186,40 +210,6 @@ module.exports = class Command {
}), values);
}

// parse & run
parse(argv, suggest) {
let chunk = {
context: {
commandPath: [],
options: null,
args: null,
literalArgs: null
},
next: {
command: this,
argv: argv || process.argv.slice(2)
}
};

do {
chunk = parseArgv(
chunk.next.command,
chunk.next.argv,
chunk.context,
suggest
);
} while (chunk.next);

return chunk;
}
run(argv) {
const chunk = this.parse(argv);

if (typeof chunk.action === 'function') {
return chunk.action.call(null, chunk.context);
}
}

// misc
messageRef() {
return `${this.usage}${this.params.args.map(arg => ` ${arg.name}`)}`;
Expand Down
17 changes: 10 additions & 7 deletions test/command-shortcut-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ describe('Command#shortcutOption()', () => {
const command = cli.command('test')
.option('--foo [foo]', 'Foo', Number)
.option('--no-bar', 'Bar')
.shortcutOption('--baz [x]', 'Baz', function(x) {
return {
foo: x,
bar: Boolean(Number(x)),
qux: 'xxx'
};
}, x => x + x);
.option('--baz [x]', 'Baz', {
normalize: x => x + x,
shortcut: function(x) {
return {
foo: x,
bar: Boolean(Number(x)),
qux: 'xxx'
};
}
});

assert.deepEqual(command.run([]).options, {
bar: true
Expand Down

0 comments on commit 00c4208

Please sign in to comment.