admin管理员组文章数量:1401673
I'm using yargs
to create a build tool with submands for "build", "link", "clean", etc.
I'd like to be able to type ./build.js
with no arguments and invoke the "build" submand handler as a default.
I was able to do it thusly:
var argv = yargs
.usage("I am usage.")
mand('bundle', 'Create JS bundles', bundle)
mand('link', 'Symlink JS files that do not need bundling', link)
mand('clean', 'Remove build artifacts', clean)
mand('build', 'Perform entire build process.', build)
.help('help')
.argv;
if (argv._.length === 0) { build(); }
But it seems a bit hacky to me, and it will likely cause problems if I ever want to add any additional positional arguments to the "build" submand.
Is there any way to acplish this within the semantics of yargs? The documentation on mand()
could be more clear.
I'm using yargs
to create a build tool with submands for "build", "link", "clean", etc.
I'd like to be able to type ./build.js
with no arguments and invoke the "build" submand handler as a default.
I was able to do it thusly:
var argv = yargs
.usage("I am usage.")
.mand('bundle', 'Create JS bundles', bundle)
.mand('link', 'Symlink JS files that do not need bundling', link)
.mand('clean', 'Remove build artifacts', clean)
.mand('build', 'Perform entire build process.', build)
.help('help')
.argv;
if (argv._.length === 0) { build(); }
But it seems a bit hacky to me, and it will likely cause problems if I ever want to add any additional positional arguments to the "build" submand.
Is there any way to acplish this within the semantics of yargs? The documentation on .mand()
could be more clear.
2 Answers
Reset to default 7As mented by @sthzg, you can have default mands now:
const argv = require('yargs')
.mand('$0', 'the default mand', () => {}, (argv) => {
console.log('this mand will be run by default')
})
Yargs doesn't seem to provide this functionality by itself. There is a third party package on NPM that augments yargs to do what you want. https://www.npmjs./package/yargs-default-mand
var yargs = require('yargs');
var args = require('yargs-default-mand')(yargs);
args
.mand('*', 'default mand', build)
.mand('build', 'build mand', build)
.args;
本文标签: javascriptHow do I specify a default subcommand in yargsStack Overflow
版权声明:本文标题:javascript - How do I specify a default subcommand in yargs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744304089a2599726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论