vietjtnguyen / argagg

A simple C++11 command line argument parser
MIT License
224 stars 28 forks source link

Method of using defaut argument still fails #19

Closed raptor closed 6 years ago

raptor commented 6 years ago

Hi,

I've been trying to use the method you describe to supply a default option for a flag; however, I cannot seem to get it to work. I do just as you suggest:

int32_t mode = args["mode"].as<int32_t>(1234);

If I say an arg has one parameter, but then don't supply it on the command line, the program fails with:

Encountered exception while parsing arguments: last option "--def-opt" expects an argument but the parser ran out of command line arguments

(Here, even your sample1 program seems to have the problem)

Am I doing something wrong?

Thanks!

vietjtnguyen commented 6 years ago

This is actually intended behavior and has been brought up before in #16. Here's an edit of my response:

Yea, my concern with having that style of optional argument is that it actually introduces a third state to the flag.

  • flag not specified
  • flag specified followed by non-flag
  • flag specified followed by a flag (and thus specified without argument)

Currently a flag has two states:

  • flag not specified
  • flag specified with argument

I feel like if there's some default behavior you want with a flag specified with no argument it can be captured by its own flag. Consider git log for example, --oneline is equivalent to --pretty=oneline.

Also, it messes with the current greedy argument behavior. For example, foo --log --verbose would make --verbose an argument to --log.

I'm open to further discussion on this though.

raptor commented 6 years ago

Hi,

I read that issue, and agree with your assessment; however, the --def-opt flag still does not work as stated on the main page readme:

If you want to get an option argument but fallback on a default value if it doesn't exist then you can use the argagg::option_results::as() API and provide a default value

When the value doesn't exist, I actually get that error described in my opening post. Am I misunderstanding this?

Thanks

vietjtnguyen commented 6 years ago

I see, the wording in my README is poor then. I think it is more precisely worded as:

If you want to get an option argument but fallback on a default value if the option is not specified then you can use the argagg::option_results::as() API and provide a default value.

For the sample, I added some unit test cases that hopefully clarify the behavior:

Let me know if that clarifies the behavior or not.

raptor commented 6 years ago

Ah OK, I understand better now. Thanks for the clarification.