Yargs:在命令处理程序中调用现有命令

时间:2018-10-25 09:17:14

标签: node.js yargs

我正在尝试使用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文档中也找不到任何相关内容。我想念什么?

谢谢!

2 个答案:

答案 0 :(得分:0)

您应该像这样重构代码:

start = function (argv) {
    console.log('Starting');
};

stop = function (argv) {
    console.log('Stopping');
};

yargs.command('start', 'start description', () => {}, start);

yargs.command('stop', 'stop description', () => {}, stop);

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

答案 1 :(得分:0)

您可以简单地使用JS:

yargs

8443 API的简要介绍中,我看不到另一种方法。

相关问题