1 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
command參數(shù)的哲學(xué)是它們可以是任何東西,因此必須在action.
如果您需要一組有限的選項(xiàng),您可能需要使用option.
一般處理這種情況的解決方法是使用git 樣式的子命令:
#!/usr/bin/env node
// main entry point for the program, let's call it mycmd
const program = require('commander');
program.command('greet', 'greets someone');
program.parse(process.argv);
然后有g(shù)reet命令文件:
#!/usr/bin/env node
// entry point for `greet` subcommand, named mycmd-greet
const program = require('commander');
// declare all available cases
// (you could also obviously reuse the same function as the action callback)
program
.command('giuseppe')
.action(() => {
console.log("Hi Giu!");
});
program
.command('mick')
.action(() => {
console.log("Hey Micky!");
});
program
.command('bob')
.action(() => {
console.log("That's my Bobby!");
});
// handle `greet` failures here in one shot
program
.command('*')
.action((x) => {
console.log(`Sorry, I don't know no ${x}.`);
});
program.parse(process.argv);
希望這可以幫助!
添加回答
舉報(bào)