【问题标题】:Yargs: Call existing command in command handlerYargs:在命令处理程序中调用现有命令
【发布时间】:2023-03-23 09:47:01
【问题描述】:

我正在尝试使用 yargs 实现 restart 命令。我已经实现了startstop 命令,我现在要做的就是在restart 命令中调用这些现有命令。

不幸的是,仅使用yargs.parse('stop'); 不起作用

yargs.command('start', '', () => {}, (argv) => {
    console.log('Starting');
});

yargs.command('stop', '', () => {}, (argv) => {
    console.log('Stopping');
});

yargs.command('restart', 'description', () => {}, (argv) => {
    yargs.parse('stop');
    yargs.parse('start');
});

我在 Github 问题或 API 文档中也找不到任何相关内容。我错过了什么?

谢谢!

【问题讨论】:

    标签: node.js yargs


    【解决方案1】:

    你可以简单的使用JS:

    function start(argv) {
      console.log('Starting');
    }
    function stop(argv) {
      console.log('Stopping');
    }
    
    yargs.command('start', '', () => {}, start);
    
    yargs.command('stop', '', () => {}, stop);
    
    yargs.command('restart', 'description', () => {}, (argv) => {
       start(argv);
       stop(argv);
    });
    

    通过对yargs API 的简要了解,我没有看到其他方法。

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 2012-03-08
      • 2021-08-01
      • 2021-11-24
      • 2021-06-06
      • 2018-12-28
      • 2021-10-28
      • 1970-01-01
      • 2021-09-09
      相关资源
      最近更新 更多