Skip to content

Commit

Permalink
Usage only to define a command
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jan 9, 2020
1 parent 41bdd93 commit fdf8253
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## next

- Restored wrongly removed `Command#extend()`
- Removed config argument for `Command`
- Changed `Command`'s constructor and `Command#command(method)` to take `usage` only (i.e. `command('name [param]')` instead `command('name', '[param]')`)
- Added `Command#clone()` method
- Added `Command#getCommand(name)` and `Command#getCommands()` methods
- Added `Command#getOption(name)` and `Command#getOptions()` methods
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ npm install clap
```js
const cli = require('clap');

const myCommand = cli.command('my-command', '[optional-arg]')
const myCommand = cli.command('my-command [optional-arg]')
.description('Optional description')
.version('1.2.3')
.option('-b, --bool', 'Bollean option')
.option('--foo <foo>', 'Option with required argument')
.option('--bar [bar]', 'Option with optional argument')
.option('--baz [value]', 'Option with optional argument and normalize function', function(value) {
// calls on init and for any value set
return Number(value);
}, 123) // 123 is default
.action(function(args, literalArgs) {
.option('--baz [value]', 'Option with optional argument and normalize function',
value => Number(value),
123 // 123 is default
)
.action(function({ options, args, literalArgs }) {
// options is an object with collected values
// args goes before options
// literal args goes after --
// this.values is an object with collected values
// literal args goes after "--"
});

myCommand.run(); // runs with process.argv.slice(2)
myCommand.run(); // the same as "myCommnad.run(process.argv.slice(2))"
myCommand.run(['--foo', '123', '-b'])

// sub-commands
Expand All @@ -63,7 +63,7 @@ myCommand
.version(value, usage, description, action)
.help(usage, description, action)
.option(usage, description, ...options)
.command(nameOrCommand, params, config)
.command(usageOrCommand)
.extend(fn, ...options)
.end()
Expand Down
22 changes: 9 additions & 13 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ const handlers = ['init', 'applyConfig', 'finishContext', 'action'].reduce((res,
}, { initial: {}, setters: {} });

module.exports = class Command {
constructor(name, params) {
constructor(usage = '') {
const [name, params] = usage.trim().split(/(\s+.*)$/);

this.name = name;
this.params = new Params(params || '', `"${this.name}" command definition`);
this.params = new Params(params, `"${name}" command definition`);
this.options = new Map();
this.commands = new Map();
this.meta = {
Expand Down Expand Up @@ -96,17 +98,11 @@ module.exports = class Command {

return this;
}
command(nameOrCommand, params, config) {
let subcommand;
let name;

if (nameOrCommand instanceof Command) {
subcommand = nameOrCommand;
name = subcommand.name;
} else {
name = nameOrCommand;
subcommand = new Command(name, params, config);
}
command(usageOrCommand) {
const subcommand = typeof usageOrCommand === 'string'
? new Command(usageOrCommand)
: usageOrCommand;
const name = subcommand.name;

// search for existing one
if (this.commands.has(name)) {
Expand Down
2 changes: 1 addition & 1 deletion test/command-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('action()', function() {
it('should have an expected input', function() {
const calls = [];
const command = cli
.command('test', '[foo]')
.command('test [foo]')
.option('--bar', 'bar option')
.action(function(...args) {
calls.push({
Expand Down
6 changes: 3 additions & 3 deletions test/command-args-and-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('command run', function() {
let command;

beforeEach(function() {
command = cli.command('test', '[foo]')
command = cli.command('test [foo]')
.option('--foo', 'Foo')
.option('--bar <number>', 'Bar', Number);
});
Expand Down Expand Up @@ -148,9 +148,9 @@ describe('command run', function() {
describe('required argument', () => {
let action;
const command = cli
.command('test', '<arg1>')
.command('test <arg1>')
.action(() => action = '1')
.command('nested', '<arg2>')
.command('nested <arg2>')
.action(() => action = '2')
.end();

Expand Down
8 changes: 4 additions & 4 deletions test/command-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Command help', () => {

it('should show help all cases', () => {
cli
.command('test', '[qux]')
.command('test [qux]')
.description('Test description')
.option('-f, --foo', 'Foo')
.option('--bar <baz>', 'Bar', 8080)
Expand Down Expand Up @@ -86,9 +86,9 @@ describe('Command help', () => {

it('should show help for nested command', () => {
cli
.command('test', '[qux]')
.command('test [qux]')
.option('-f, --foo', 'Foo')
.command('nested', '[nested-arg]')
.command('nested [nested-arg]')
.option('--bar <baz>', 'Bar')
.end()
.run(['nested', '--help']);
Expand All @@ -109,7 +109,7 @@ describe('Command help', () => {

it('should show help message when Command#outputHelp called', function() {
const command = cli
.command('test', '[qux]')
.command('test [qux]')
.option('-f, --foo', 'Foo');

command.outputHelp();
Expand Down
4 changes: 2 additions & 2 deletions test/command-parse-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ describe('init()/applyConfig()/finishContext()', function() {

beforeEach(function() {
calls = [];
command = cli.command('test', '[arg1]')
command = cli.command('test [arg1]')
.init(() => calls.push('init'))
.applyConfig(() => calls.push('applyConfig'))
.finishContext(() => calls.push('finishContext'));
command.command('nested', '[arg2] [arg3]')
command.command('nested [arg2] [arg3]')
.init(() => calls.push('nested init'))
.applyConfig(() => calls.push('nested applyConfig'))
.finishContext(() => calls.push('nested finishContext'));
Expand Down
4 changes: 2 additions & 2 deletions test/suggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('suggest', function() {

/* eslint-disable indent */
const command = cli
.command('test', '[arg1]')
.command('test [arg1]')
.option('-f, --foo', 'foo')
.option('-b, --bar', 'bar')
.option('--required-arg <arg>', 'option with required argument')
Expand All @@ -28,7 +28,7 @@ describe('suggest', function() {
.option('--bar', 'nested option 2')
.option('--baz', 'nested option 3')
.end()
.command('bar', '[arg2]')
.command('bar [arg2]')
.command('baz').end()
.command('qux').end()
.option('--test', 'test option')
Expand Down

0 comments on commit fdf8253

Please sign in to comment.