gpt4 book ai didi

javascript - 如何在 yargs 中指定默认子命令?

转载 作者:行者123 更新时间:2023-11-30 09:47:29 27 4
gpt4 key购买 nike

我正在使用 yargs 创建一个带有“build”、“link”、“clean”等子命令的构建工具。

我希望能够键入不带参数的 ./build.js 并默认调用“build”子命令处理程序。

我是这样做到的:

var argv = yargs
.usage("I am usage.")
.command('bundle', 'Create JS bundles', bundle)
.command('link', 'Symlink JS files that do not need bundling', link)
.command('clean', 'Remove build artifacts', clean)
.command('build', 'Perform entire build process.', build)
.help('help')
.argv;
if (argv._.length === 0) { build(); }

但这对我来说似乎有点老套,如果我想向“构建”子命令添加任何额外的位置参数,它可能会导致问题。

有没有办法在 yargs 的语义中完成这个? .command() 上的文档可能更清楚。

最佳答案

正如@sthzg 所评论的,您可以拥有 default commands现在:

const argv = require('yargs')
.command('$0', 'the default command', () => {}, (argv) => {
console.log('this command will be run by default')
})

关于javascript - 如何在 yargs 中指定默认子命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38256119/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com