Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/argument arg explicit #1567

Merged
merged 4 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -66,6 +66,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'));