Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yargs: Call existing command in command handler

Tags:

node.js

yargs

I'm trying to implement a restart command using yargs. I already implemented the start and stop commands and all I want to do now, is call these existing commands inside the restart command.

Unfortunately it does not work by simply using 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');
});

I also couldn't find anything related in the Github issues or the API documentation. What am I missing?

Thank you!

like image 683
Marcel Pociot Avatar asked Oct 25 '18 09:10

Marcel Pociot


People also ask

What is Yargs parser?

Yargs helps you build interactive command line tools by parsing arguments and generating an elegant user interface.

What is Yargs NPM?

Description. Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface. It gives you: commands and (grouped) options ( my-program. js serve --port=5000 ).


1 Answers

You can simple use 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);
});

From a brief look at yargs API, I don't see another way of doing this.

like image 124
justin Avatar answered Nov 15 '22 09:11

justin