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

RequiredOption is undefined, when no value is passed to the command #1265

Closed
benwinding opened this issue May 25, 2020 · 2 comments
Closed

Comments

@benwinding
Copy link

I'm getting an issue where a requiredOption is undefined, when no value is passed to the command.

Code

program
  .command('build')
  .requiredOption('-c, --config', 'Configuration Name')
  .action(async function (env, options) {
// options is undefined
  });

Args

$ ./mycommander build -c

Result

options is undefined
image

Expected

I would expect requiredOption to be an option that enforces a value, not just the flag to be present

Also related: #230

Ps: Love the library, it's pretty nice to work with 👍

@shadowspawn
Copy link
Collaborator

  1. Your build command does declare any arguments, so the first parameter passed to the action handler will be the options.
program
   .command('build')
   // ...
   .action(async function (options)

From the README (https://github.com/tj/commander.js#action-handler-subcommands):

The action handler gets passed a parameter for each argument you declared, and one additional argument which is the command object itself. This command argument has the values for the command-specific options added as properties.

  1. From its description, it sounds like your option expect a value. This might just be a typo in your example, but in case it causes confusion:
.requiredOption('-c, --config <name>', 'Configuration Name')

@benwinding
Copy link
Author

Ahh I see now, works as expected.

Thanks @shadowspawn!

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

2 participants