2017-03-17 38 views
0

現狀如何提供跨多個命令選項與node.js的指揮官

我提供了許多命令,像這樣:

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顯示「幫助」輸出:

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

回答

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)