ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
yargs 模块还允许通过 command 方法,设置 Git 风格的子命令。 ~~~ #!/usr/bin/env node var argv = require('yargs') .command("morning", "good morning", function (yargs) { console.log("Good Morning"); }) .command("evening", "good evening", function (yargs) { console.log("Good Evening"); }) .argv; console.log('hello ', argv.n); ~~~ 用法如下。 ~~~ $ hello morning -n tom Good Morning hello tom ~~~ 可以将这个功能与 shellojs 模块结合起来。 ~~~ #!/usr/bin/env node require('shelljs/global'); var argv = require('yargs') .command("morning", "good morning", function (yargs) { echo("Good Morning"); }) .command("evening", "good evening", function (yargs) { echo("Good Evening"); }) .argv; console.log('hello ', argv.n); ~~~ 每个子命令往往有自己的参数,这时就需要在回调函数中单独指定。回调函数中,要先用 reset 方法重置 yargs 对象。 ~~~ #!/usr/bin/env node require('shelljs/global'); var argv = require('yargs') .command("morning", "good morning", function (yargs) { echo("Good Morning"); var argv = yargs.reset() .option("m", { alias: "message", description: "provide any sentence" }) .help("h") .alias("h", "help") .argv; echo(argv.m); }) .argv; ~~~ 用法如下。 ~~~ $ hello morning -m "Are you hungry?" Good Morning Are you hungry? ~~~