I have read the yargs documetation multiple times, but can't figure out this one. Here are my requirements:
- My CLI should provide two commands:
cmd1andcmd2. - The user must specify one of these two commands, otherwise the CLI must print a help message and exit.
This is my attempt:
async function main() {
await yargs(process.argv.slice(2))
.command('cmd1', 'Command 1', {}, () => console.log('Executing command1'))
.command('cmd2', 'Command 2', {}, () => console.log('Executing command2'))
.help().argv;
}
Following commands work as expected:
my-cli cmd1 # prints "Executing command1"
my-cli cmd2 # prints "Executing command2"
However following commands quit silently:
my-cli
my-cli cmd3
What am I missing?
According to the documentation and the refine of OP, the correct
yargcode handling undefined arguments is as follow:strictCommands(): accepts only defined commands (i.e. does not accept undefined command likecmd3) DocumentationdemandCommand(): accepts minimum 1 argument (i.e. does not accept a command with no argument); 1 is the default value of minimum; can also addmaxoption to constraint to exactly 1 argument bydemandCommand(1, 1)Documentation