简体   繁体   English

未选择命令时如何创建 yargs 默认响应

[英]How to create yargs default response when no command selected

I have created a CLI with yargs .我用yargs创建了一个 CLI。 It has a series of nested commands.它有一系列嵌套命令。 All the commands work nicely, but I also want to have a welcome/info message when no command is given or if a command that does not exist is supplied.所有命令都运行良好,但我还希望在未给出命令或提供不存在的命令时收到欢迎/信息消息。

I have been unable to get this to work, what efforts I have used either display a message all the time or not at all.我一直无法让它工作,我所做的努力要么一直显示一条消息,要么根本不显示。 Any suggestions would be welcome欢迎大家提出意见

Took quite a lot of docs reading but got there in the end, the $0 is used as a catch all command and can be added to the builder. 阅读了很多文档,但最终还是到了那里, $0用作捕获所有命令,可以添加到构建器中。

yargs(hideBin(process.argv))
  .version(require('../package.json').version)
  .alias('h', 'help')
  .help()
  .command('stuff', 'makes stuff happen', () => {}, (_) => {
    ...
  })
  .command('$0', 'the default command', () => {}, (_) => {
    console.log(defaultMessage)
  })
  .parse();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM