Skip to content

Commit

Permalink
Feature/argument arg explicit (#1567)
Browse files Browse the repository at this point in the history
* Add new methods

* Add chain test

* Add tests for Argument.required

* Add typings for argRequired and argOptional
  • Loading branch information
shadowspawn authored Jul 12, 2021
1 parent 4be69f1 commit 6f51e4a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ class Argument {
};
return this;
};

/**
* Make option-argument required.
*/
argRequired() {
this.required = true;
return this;
}

/**
* Make option-argument optional.
*/
argOptional() {
this.required = false;
return this;
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/argument.chain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ describe('Argument methods that should return this for chaining', () => {
const result = argument.choices(['a']);
expect(result).toBe(argument);
});

test('when call .argRequired() then returns this', () => {
const argument = new Argument('<value>');
const result = argument.argRequired();
expect(result).toBe(argument);
});

test('when call .argOptional() then returns this', () => {
const argument = new Argument('<value>');
const result = argument.argOptional();
expect(result).toBe(argument);
});
});
34 changes: 34 additions & 0 deletions tests/argument.required.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const commander = require('../');

// Low-level tests of setting Argument.required.
// Higher level tests of optional/required arguments elsewhere.

test('when name with surrounding <> then argument required', () => {
const argument = new commander.Argument('<name>');
expect(argument.required).toBe(true);
});

test('when name with surrounding [] then argument optional', () => {
const argument = new commander.Argument('[name]');
expect(argument.required).toBe(false);
});

test('when name without surrounding brackets then argument required', () => {
// default behaviour, allowed from Commander 8
const argument = new commander.Argument('name');
expect(argument.required).toBe(true);
});

test('when call .argRequired() then argument required', () => {
const argument = new commander.Argument('name');
argument.required = false;
argument.argRequired();
expect(argument.required).toBe(true);
});

test('when call .argOptional() then argument optional', () => {
const argument = new commander.Argument('name');
argument.required = true;
argument.argOptional();
expect(argument.required).toBe(false);
});
9 changes: 9 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export class Argument {
*/
choices(values: string[]): this;

/**
* Make option-argument required.
*/
argRequired(): this;

/**
* Make option-argument optional.
*/
argOptional(): this;
}

export class Option {
Expand Down
6 changes: 6 additions & 0 deletions typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,12 @@ expectType<commander.Argument>(baseArgument.argParser((value: string, previous:
// choices
expectType<commander.Argument>(baseArgument.choices(['a', 'b']));

// argRequired
expectType<commander.Argument>(baseArgument.argRequired());

// argOptional
expectType<commander.Argument>(baseArgument.argOptional());

// createArgument
expectType<commander.Argument>(program.createArgument('<name>'));
expectType<commander.Argument>(program.createArgument('<name>', 'description'));

0 comments on commit 6f51e4a

Please sign in to comment.