2017-03-17 6 views
0

La situationComment fournir des options à travers de nombreuses commandes avec Node.js commandant

Je fournissent de nombreuses commandes, comme ceci:

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 
    }); 

Le problème

Avis Je dois répète mes déclarations d'option. J'ai beaucoup d'options et ceci crée la répétition.

En outre, ces options ne seront pas visibles dans mon -h "aide" Sortie:

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 

La question

Comment puis-je déclarer les options qu'une seule fois, de les appliquer à tous commandes, et les faire apparaître dans l'aide -h?

Répondre

0

J'ai trouvé ma propre réponse!

Vous pouvez également spécifier des options sur l'objet du programme:

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)