Skip to content

Commit 6f51e4a

Browse files
authored
Feature/argument arg explicit (#1567)
* Add new methods * Add chain test * Add tests for Argument.required * Add typings for argRequired and argOptional
1 parent 4be69f1 commit 6f51e4a

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

lib/argument.js

+16
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ class Argument {
109109
};
110110
return this;
111111
};
112+
113+
/**
114+
* Make option-argument required.
115+
*/
116+
argRequired() {
117+
this.required = true;
118+
return this;
119+
}
120+
121+
/**
122+
* Make option-argument optional.
123+
*/
124+
argOptional() {
125+
this.required = false;
126+
return this;
127+
}
112128
}
113129

114130
/**

tests/argument.chain.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ describe('Argument methods that should return this for chaining', () => {
1818
const result = argument.choices(['a']);
1919
expect(result).toBe(argument);
2020
});
21+
22+
test('when call .argRequired() then returns this', () => {
23+
const argument = new Argument('<value>');
24+
const result = argument.argRequired();
25+
expect(result).toBe(argument);
26+
});
27+
28+
test('when call .argOptional() then returns this', () => {
29+
const argument = new Argument('<value>');
30+
const result = argument.argOptional();
31+
expect(result).toBe(argument);
32+
});
2133
});

tests/argument.required.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const commander = require('../');
2+
3+
// Low-level tests of setting Argument.required.
4+
// Higher level tests of optional/required arguments elsewhere.
5+
6+
test('when name with surrounding <> then argument required', () => {
7+
const argument = new commander.Argument('<name>');
8+
expect(argument.required).toBe(true);
9+
});
10+
11+
test('when name with surrounding [] then argument optional', () => {
12+
const argument = new commander.Argument('[name]');
13+
expect(argument.required).toBe(false);
14+
});
15+
16+
test('when name without surrounding brackets then argument required', () => {
17+
// default behaviour, allowed from Commander 8
18+
const argument = new commander.Argument('name');
19+
expect(argument.required).toBe(true);
20+
});
21+
22+
test('when call .argRequired() then argument required', () => {
23+
const argument = new commander.Argument('name');
24+
argument.required = false;
25+
argument.argRequired();
26+
expect(argument.required).toBe(true);
27+
});
28+
29+
test('when call .argOptional() then argument optional', () => {
30+
const argument = new commander.Argument('name');
31+
argument.required = true;
32+
argument.argOptional();
33+
expect(argument.required).toBe(false);
34+
});

typings/index.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ export class Argument {
6363
*/
6464
choices(values: string[]): this;
6565

66+
/**
67+
* Make option-argument required.
68+
*/
69+
argRequired(): this;
70+
71+
/**
72+
* Make option-argument optional.
73+
*/
74+
argOptional(): this;
6675
}
6776

6877
export class Option {

typings/index.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ expectType<commander.Argument>(baseArgument.argParser((value: string, previous:
395395
// choices
396396
expectType<commander.Argument>(baseArgument.choices(['a', 'b']));
397397

398+
// argRequired
399+
expectType<commander.Argument>(baseArgument.argRequired());
400+
401+
// argOptional
402+
expectType<commander.Argument>(baseArgument.argOptional());
403+
398404
// createArgument
399405
expectType<commander.Argument>(program.createArgument('<name>'));
400406
expectType<commander.Argument>(program.createArgument('<name>', 'description'));

0 commit comments

Comments
 (0)