完整的 node.js 命令行解決方案。
tj/commander.js: node.js command-line interfaces made easy
1 只使用 option
這個是最簡單和好理解的,直接使用看官方的例子即可:
commander.js/options-common.js at master · tj/commander.js
program
.option('-d, --debug', 'output extra debugging')
.option('-s, --small', 'small pizza size')
.option('-p, --pizza-type <type>', 'flavour of pizza');
如果沒有指定參數,則 option 默認是 boolean 類型,默認為 undefined,指定了就是 true。
以下演示中,默認使用如下代碼模板,並且主命令假設為 foo
const { Command } = require("commander");
const program = new Command();
// code,這里是不同的演示代碼
program.parse();
2 使用 argument
這里是命令的參數,調用形式上和 command 有點像,但是不同的東西。
program
.version("0.1.0")
.argument("<username>", "user to login")
.argument("[password]", "password for user, if required", "no password given")
.description("example program for argument")
.action((username, password) => {
console.log("username:", username);
console.log("password:", password);
});
program
.version("0.1.0")
.arguments("<username> [password]")
.description("test command")
.action((username, password) => {
console.log("username:", username);
console.log("password:", password || "no password given");
});
調用形式就是 foo my-name my-password
3 argument 和 option
program
.version("0.1.0")
.argument("<username>", "user to login")
.argument("[password]", "password for user, if required", "no password given")
.option("-c, --check", "check option")
.option("-C, --no-check", "no check option")
.option("-o, --output <output>", "output options", "./temp")
.description("example program for argument")
.action((username, password, options) => {
console.log("username:", username);
console.log("password:", password);
console.log(options);
});
調用形式就是 foo my-name my-password -C -o "/temp"
這里使用了取反 option --no-check
,當指定 -C
或者 --no-check
時,option 中的 check 為 false。(沒有名為 no-check 的選項,控制的都是 check 選項的值)
即:check 選項一共有三種值,不指定:undefined, -c/--check:true,-C/--no-check:false。
4 command 和 option
program
.command("join")
.option("-c, --check", "check option")
.option("-o, --output <output>", "output options", "./temp")
.description("example program for command")
.action((options, command) => {
console.log(options);
});
調用形式:foo join -c -o "\temp"
這里的 join 是子命令,形式上和 argument 很像,但語義上不一樣,需要根據實際業務選擇不同的實現方式。
5 command 和 argument 和 option
program
.version("0.1.1")
.command("join")
.argument("<env>", "environment")
.argument("[second]", "second command")
.option("-c, --check", "check option")
.option("-o, --output <output>", "output options", "./temp")
.description("example program for command")
.action((env, second, options, command) => {
console.log(env);
console.log(second);
console.log(options);
});
調用形式:foo join my-env my-second-argument -c --output "./temp"
6 command 和 可變參數 和 option
program
.version("0.1.1")
.command("join")
.argument("<env>", "environment")
.argument("<str...>", "string list")
.option("-c, --check", "check option")
.description("example program for variadic argument ")
.action((env, str, options, command) => {
console.log(env);
console.log(str);
console.log(options);
});
調用形式 foo join my-env s1 s2 s3 s4 -c
這里 s1 s2 s3 s4 都會放到 str 數組中
7 command 使用獨立的處理文件
program
.version("0.1.0")
.command("install [name]", "install one or more packages") // index-install
.command("search [query]", "search with optional query", {
executableFile: "mySearchSubCommand",
})
.command("list", "list packages installed", { isDefault: true });
如果沒有指定文件名,則使用 當前文件名-command 的形式查找處理文件,否則報錯,如這里的 install 子命令。
如果指定了文件名,則使用指定的文件名(相對路徑),如這里的 search 子命令。
具體執行文件中怎么寫呢?其實就是一個新的命令行解析處理。
如下,同樣可以繼續使用 command argument option
const { Command } = require('commander');
const program = new Command();
program
.argument("<abc>")
.option('--ignore-case', 'ignore case',false)
.action((abc,options,command)=>{
console.log("action",abc);
console.log("action",options);
console.log("action",command.args);
});
program.parse(process.argv);
const arguments = program.args;
const options = program.opts();
console.log("@",arguments);
console.log("@",options);
console.log("@",program.processedArgs);
8 異步處理
上面的模板說明中,使用的是同步處理方式(program.parse()
),如果需要異步處理,則使用 await program.parseAsync(process.argv)
async function run() {
/* code goes here */
}
async function main() {
program.command("run").action(run);
await program.parseAsync(process.argv);
}
示例代碼
其它
github social image generate
Github Social Image Generator - Bannerbear