Skip to content

Commit fdf8253

Browse files
committedJan 9, 2020
Usage only to define a command
1 parent 41bdd93 commit fdf8253

8 files changed

+32
-36
lines changed
 

‎CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## next
22

33
- Restored wrongly removed `Command#extend()`
4-
- Removed config argument for `Command`
4+
- Changed `Command`'s constructor and `Command#command(method)` to take `usage` only (i.e. `command('name [param]')` instead `command('name', '[param]')`)
55
- Added `Command#clone()` method
66
- Added `Command#getCommand(name)` and `Command#getCommands()` methods
77
- Added `Command#getOption(name)` and `Command#getOptions()` methods

‎README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ npm install clap
2121
```js
2222
const cli = require('clap');
2323

24-
const myCommand = cli.command('my-command', '[optional-arg]')
24+
const myCommand = cli.command('my-command [optional-arg]')
2525
.description('Optional description')
2626
.version('1.2.3')
2727
.option('-b, --bool', 'Bollean option')
2828
.option('--foo <foo>', 'Option with required argument')
2929
.option('--bar [bar]', 'Option with optional argument')
30-
.option('--baz [value]', 'Option with optional argument and normalize function', function(value) {
31-
// calls on init and for any value set
32-
return Number(value);
33-
}, 123) // 123 is default
34-
.action(function(args, literalArgs) {
30+
.option('--baz [value]', 'Option with optional argument and normalize function',
31+
value => Number(value),
32+
123 // 123 is default
33+
)
34+
.action(function({ options, args, literalArgs }) {
35+
// options is an object with collected values
3536
// args goes before options
36-
// literal args goes after --
37-
// this.values is an object with collected values
37+
// literal args goes after "--"
3838
});
3939

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

4343
// sub-commands
@@ -63,7 +63,7 @@ myCommand
6363
.version(value, usage, description, action)
6464
.help(usage, description, action)
6565
.option(usage, description, ...options)
66-
.command(nameOrCommand, params, config)
66+
.command(usageOrCommand)
6767
.extend(fn, ...options)
6868
.end()
6969

‎lib/command.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ const handlers = ['init', 'applyConfig', 'finishContext', 'action'].reduce((res,
2121
}, { initial: {}, setters: {} });
2222

2323
module.exports = class Command {
24-
constructor(name, params) {
24+
constructor(usage = '') {
25+
const [name, params] = usage.trim().split(/(\s+.*)$/);
26+
2527
this.name = name;
26-
this.params = new Params(params || '', `"${this.name}" command definition`);
28+
this.params = new Params(params, `"${name}" command definition`);
2729
this.options = new Map();
2830
this.commands = new Map();
2931
this.meta = {
@@ -96,17 +98,11 @@ module.exports = class Command {
9698

9799
return this;
98100
}
99-
command(nameOrCommand, params, config) {
100-
let subcommand;
101-
let name;
102-
103-
if (nameOrCommand instanceof Command) {
104-
subcommand = nameOrCommand;
105-
name = subcommand.name;
106-
} else {
107-
name = nameOrCommand;
108-
subcommand = new Command(name, params, config);
109-
}
101+
command(usageOrCommand) {
102+
const subcommand = typeof usageOrCommand === 'string'
103+
? new Command(usageOrCommand)
104+
: usageOrCommand;
105+
const name = subcommand.name;
110106

111107
// search for existing one
112108
if (this.commands.has(name)) {

‎test/command-action.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('action()', function() {
55
it('should have an expected input', function() {
66
const calls = [];
77
const command = cli
8-
.command('test', '[foo]')
8+
.command('test [foo]')
99
.option('--bar', 'bar option')
1010
.action(function(...args) {
1111
calls.push({

‎test/command-args-and-options.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('command run', function() {
66
let command;
77

88
beforeEach(function() {
9-
command = cli.command('test', '[foo]')
9+
command = cli.command('test [foo]')
1010
.option('--foo', 'Foo')
1111
.option('--bar <number>', 'Bar', Number);
1212
});
@@ -148,9 +148,9 @@ describe('command run', function() {
148148
describe('required argument', () => {
149149
let action;
150150
const command = cli
151-
.command('test', '<arg1>')
151+
.command('test <arg1>')
152152
.action(() => action = '1')
153-
.command('nested', '<arg2>')
153+
.command('nested <arg2>')
154154
.action(() => action = '2')
155155
.end();
156156

‎test/command-help.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('Command help', () => {
5151

5252
it('should show help all cases', () => {
5353
cli
54-
.command('test', '[qux]')
54+
.command('test [qux]')
5555
.description('Test description')
5656
.option('-f, --foo', 'Foo')
5757
.option('--bar <baz>', 'Bar', 8080)
@@ -86,9 +86,9 @@ describe('Command help', () => {
8686

8787
it('should show help for nested command', () => {
8888
cli
89-
.command('test', '[qux]')
89+
.command('test [qux]')
9090
.option('-f, --foo', 'Foo')
91-
.command('nested', '[nested-arg]')
91+
.command('nested [nested-arg]')
9292
.option('--bar <baz>', 'Bar')
9393
.end()
9494
.run(['nested', '--help']);
@@ -109,7 +109,7 @@ describe('Command help', () => {
109109

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

115115
command.outputHelp();

‎test/command-parse-handlers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ describe('init()/applyConfig()/finishContext()', function() {
77

88
beforeEach(function() {
99
calls = [];
10-
command = cli.command('test', '[arg1]')
10+
command = cli.command('test [arg1]')
1111
.init(() => calls.push('init'))
1212
.applyConfig(() => calls.push('applyConfig'))
1313
.finishContext(() => calls.push('finishContext'));
14-
command.command('nested', '[arg2] [arg3]')
14+
command.command('nested [arg2] [arg3]')
1515
.init(() => calls.push('nested init'))
1616
.applyConfig(() => calls.push('nested applyConfig'))
1717
.finishContext(() => calls.push('nested finishContext'));

‎test/suggest.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('suggest', function() {
1818

1919
/* eslint-disable indent */
2020
const command = cli
21-
.command('test', '[arg1]')
21+
.command('test [arg1]')
2222
.option('-f, --foo', 'foo')
2323
.option('-b, --bar', 'bar')
2424
.option('--required-arg <arg>', 'option with required argument')
@@ -28,7 +28,7 @@ describe('suggest', function() {
2828
.option('--bar', 'nested option 2')
2929
.option('--baz', 'nested option 3')
3030
.end()
31-
.command('bar', '[arg2]')
31+
.command('bar [arg2]')
3232
.command('baz').end()
3333
.command('qux').end()
3434
.option('--test', 'test option')

0 commit comments

Comments
 (0)
Please sign in to comment.