1

情况

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

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 "help" 输出中:

 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 帮助中?

4

1 回答 1

1

找到了我自己的答案!

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

 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)
于 2017-03-22T01:50:24.900 回答