如何使用node.js命令提供多个命令的选项

时间:2017-03-17 18:54:05

标签: node.js node-commander

情况

我提供了许多命令,如下所示:

program
  .command('foo-command')
  .description('Do foo things')
  .option('-s, --staging', 'Tells the system to use the staging instance')
  .option('-c, --certroot <path>', 'Path to the certoot')
  .action(function(optional) {
    //do foo things
  });

program
    .command('bar-command')
    .description('Do bar things')
    .option('-s, --staging', 'Tells the system to use the staging instance')
    .option('-c, --certroot <path>', 'Path to the certoot')
    .action(function(optional) {
      //do bar things
    });

问题

注意我必须重复我的选项声明。我有很多选择,这会造成重复。

此外,这些选项不会出现在我的-h&#34;帮助&#34;输出:

 Usage: cert_manager [options] [command]


  Commands:

    generate   Any CMS websites without an SSL cert will have a cert created and it will be uploaded to our CDN
    renew      Any SSL cert that is about to expire will be renewed and re-uploaded to our CDN

  Options:

    -h, --help  output usage information

问题

我如何只声明一次选项,让它们适用于所有命令,并将它们显示在-h帮助中?

1 个答案:

答案 0 :(得分:0)

找到我自己的答案!

您还可以在程序对象上指定选项:

 program
  .command('foo-command')
  .description('Do foo things')
  .action(function(optional) {
    //do foo things
  });

program
    .command('bar-command')
    .description('Do bar things')
    .action(function(optional) {
      //do bar things
    });

program.option('-c, --certroot <path>' , 'Options for all commands').parse(process.argv)