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

Option identified as argument #2280

Closed
AlexandraMN opened this issue Nov 6, 2024 · 1 comment
Closed

Option identified as argument #2280

AlexandraMN opened this issue Nov 6, 2024 · 1 comment

Comments

@AlexandraMN
Copy link

I was using some examples from this repository to check if I can use commander in my application and I have a question.
Example: options-common.js program
Code:

const commander = require('commander');
const program = new commander.Command();

program
  .option('-d, --debug', 'output extra debugging')
  .option('-s, --small', 'small pizza size')
  .option('-p, --pizza-type <type>', 'flavour of pizza');

program.parse(process.argv);

const options = program.opts();
if (options.debug) console.log(options);
console.log('pizza details:');
if (options.small) console.log('- small pizza size');
if (options.pizzaType) console.log(`- ${options.pizzaType}`);

The pizza-type option requires an argument, but if I do not add an argument and I use another option like -s, the pizza-type option use it as an argument.

node options-common.js --pizza-type -s        
pizza details:
- -s

My expectation was to receive the argument missing message. I want to add some checks in my code for the received argument, but I thought that the program would identify firstly that -s is another option, before verifying something else.

@shadowspawn
Copy link
Collaborator

This is intended behaviour, although surprising when you first encounter it and expect something different! It is the standard POSIX behaviour for options which have a required option-argument.

Quoting from different parts of the README (see the README for the associated code):

Options with an expected option-argument are greedy and will consume the following argument whatever the value. So --id -xyz reads -xyz as the option-argument.

Options with an optional option-argument are not greedy and will ignore arguments starting with a dash. So id behaves as a boolean option for --id -5, but you can use a combined form if needed like --id=-5.

@AlexandraMN AlexandraMN closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2024
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