Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing parent options from subcommand #243

Closed
marc0s opened this issue Aug 27, 2014 · 8 comments
Closed

Accessing parent options from subcommand #243

marc0s opened this issue Aug 27, 2014 · 8 comments

Comments

@marc0s
Copy link

marc0s commented Aug 27, 2014

Are parent's options available to subcommands? In my tests, they aren't.

It would be nice to have them available. In my case is for configuring the remote server my CLI app needs to connect to, so I have not to define the same options in every subcommand.

# ./cmd
#!/usr/bin/env node

var program = require("commander");
program.version("0.0.1")
  .option("-H, --host <host>", "server host")
  .command("sub", "subcommand");
program.parse(process.argv);
console.log(program.host);

# ./cmd-sub
#!/usr/bin/env node

var program = require("commander");
program.version("0.0.1");
program.parse(process.argv);
console.log(program.host);

And if you run them:

$ ./cmd --host localhost 
localhost
$ ./cmd-sub --host localhost 
error: unknown option `--host'

Any trick to get them from the subcommand or is just not possible?

Thanks.

@thethomaseffect
Copy link
Collaborator

That's not the best idea, subcommands should behave like independent programs. .option and .command are just methods of a commander object, why not make a function that takes a commander object, runs the methods on it and returns the object with it's new methods. This way you can share your --host option and more with as many subcommands as you want. I've never tried this (never had subcommands that had all that much in common) but it sounds okay in theory.

@marc0s
Copy link
Author

marc0s commented Aug 27, 2014

Thanks for the tip!

@thethomaseffect
Copy link
Collaborator

Let me know if it works :)

@marc0s
Copy link
Author

marc0s commented Aug 28, 2014

It did :)

I just create a basic commander object with the common options inside a function that returns it and use that function to initialize each of the commander objects I need, so they're all already populated with the common options.

Thanks!

@b4dnewz
Copy link

b4dnewz commented Jan 22, 2018

hi @marc0s, can you share some code or example of how you did? just for the records, thankx

@pburgmer
Copy link

pburgmer commented Jan 29, 2018

@b4dnewz I did it like this

const command = commander.command('merge');

command
      .description('foo')
      .option('--bar', 'really?', false)
      .action(async (args) => { });

addLoggingOption(command);

and

function addLoggingOption(command) {
  command
    .option('--log-level <level>', 'print log messages of given level and above only', 'info')
    .option('--verbose', 'set log level to debug')
    .option('--quiet', 'set log level to warn');
}

@shadowspawn
Copy link
Collaborator

Opened poll on possible enhancements to global options: #1551

@shadowspawn
Copy link
Collaborator

Opened a draft PR for .addCommonOption() #1670

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants