【问题标题】:How to provide options across many commands with node.js commander如何使用 node.js 指挥官跨多个命令提供选项
【发布时间】:2017-03-17 18:54:04
【问题描述】:

情况

我提供了很多命令,像这样:

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

【问题讨论】:

    标签: node.js node-commander


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      相关资源
      最近更新 更多