You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've tried defining a command with a single required argument, and attempted to validate that exactly one source specified:
import * as program from 'commander';
import { version } from 'pjson';
program
.version(version)
.arguments('<source.md>')
.action((source: string): void => {
console.log('source=' + source);
program.source = source;
})
.parse(process.argv);
console.log(program.args);
console.log(program.args.length);
if (program.args.length !== 1) {
console.log('Invalid arguments');
program.help();
}
console.log(program.source);
And running the program with no arguments, correctly prints 'Invalid arguments' since program.args.length is 0. But with 1 argument (program a) program.args.length is 2!? and outputs the following, claiming that program.args.length is 2!?
There is not currently error detection for excess arguments, and it is not currently easy to handle it yourself in a general way. In simple cases you can check the argument count of the original arguments (process.argv or program.rawArgs).
#749 covers the case with extra arguments passed to subcommands.
I've tried defining a command with a single required argument, and attempted to validate that exactly one source specified:
And running the program with no arguments, correctly prints 'Invalid arguments' since
program.args.length
is 0. But with 1 argument (program a
)program.args.length
is 2!? and outputs the following, claiming thatprogram.args.length
is 2!?Weirdly running with four arguments
program a b c d
also appears to drop theb
argument somehow:So I guess I have two questions:
The text was updated successfully, but these errors were encountered: