Skip to content

Support option name aliases / multiple long options #240

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

Closed
eyalroz opened this issue Jul 7, 2020 · 12 comments
Closed

Support option name aliases / multiple long options #240

eyalroz opened this issue Jul 7, 2020 · 12 comments

Comments

@eyalroz
Copy link
Contributor

eyalroz commented Jul 7, 2020

(This is a feature I added to boost::program_options a couple of years back.)

Right now, if you name an option "x,long-name-of-x,another-long-name-of-x" - you get an error:

terminate called after throwing an instance of 'cxxopts::invalid_option_format_error'
  what():  Invalid option format 'x,long-name-of-x,another-long-name-of-x'

instead, cxxopts should support having multiple aliases for the (first and primary) name of the option - all of which would be accepted on the command-line just as though the primary name were used.

@KappaDistributive
Copy link

I'd like to work on this. If there's anything I should keep in mind, please do let me know.

Otherwise, I'll share a first draft as soon as I have one and we can take it from there.

@jarro2783
Copy link
Owner

I can't think of anything, a short and a long name are basically already an alias for each other, so I think the syntax just needs to be extended. Go for it.

@KappaDistributive
Copy link

Sorry for the slow reply -- live happened.

I've taken a closer look at the code now and thought about how to implement it. AFAIK, there are a few viable options, ranked in increasing order of intrusiveness:

  • We could overload Options::add_option once more and allow it to receive an array of long names and then adapt OptionAdder::operator() and the relevant RegEx accordingly. (The only option that is 100% backward compatible)
  • We could do away with the assumption that long names consist of only one alias and replace the current implementation of Options::add_option and OptionAdder::operator(). (OptionAdder::operator() would remain backward compatible but Options::add_option would not.)
  • We could add an additional layer of abstraction and create a bunch of Command (name tbd) entities that we split the initial string into and then handle both short and long names uniformly. (OptionAdder::operator() would remain backward compatible but Options::add_option would not.)

Since I don't know this package and its users well enough to make this call, I require your expertise to make this decision. Once we've narrowed it down, I'll create an initial implementation that we can discuss in detail.

@eyalroz
Copy link
Contributor Author

eyalroz commented Sep 6, 2020

I think that the minimal change in terms of user-facing functions is simply being willing to parse "x,foo,bar" and not just "x,foo":

    options.add_options()
        ("x,foo,bar", "Frobincate the bar", cxxopts::value<bool>()->default_value("true"));

That's your second option IIUC.

But it doesn't have to be just one way of specifying multiple aliases. And I like the first option as well, i.e. I'd also like it if I could write:

    options.add_options()
        ({'x', "foo", "bar" }, "Frobincate the bar", cxxopts::value<bool>()->default_value("true"));

and for that to work.

@KappaDistributive
Copy link

Yeah, that's the OptionAdder::operator() part -- that would remain compatible in any of my suggestions, i.e. the user would use

options.add_options()
        ("x,foo,bar", "Frobincate the bar", cxxopts::value<bool>()->default_value("true"));

to define aliases. My question is about the backend -- i.e. about how OptionAdder::operator() and Options::add_option should talk to one another. Right now, the implementation seems a bit ad-hoc. And the core of the question is whether we would like to refactor that part now or simply bolt on a new feature.

@eyalroz
Copy link
Contributor Author

eyalroz commented Sep 6, 2020

Change the backend, of course. (Goes without saying IMHO).

@jarro2783
Copy link
Owner

You should extend the interface without breaking existing behaviour if possible, and change the backend to support it. I think this is easily done in this case.

@eyalroz
Copy link
Contributor Author

eyalroz commented Apr 29, 2021

@KappaDistributive : Did you get around to working on this?

@KappaDistributive
Copy link

KappaDistributive commented Apr 29, 2021

@KappaDistributive : Did you get around to working on this?

No. I was planning to work on this as my vacation project but then decided to go with another project instead. Sorry for not letting you know sooner.

@artpaul
Copy link

artpaul commented Jul 9, 2022

@eyalroz I'm just interested why would you need extra aliases for the single option?

eyalroz added a commit to eyalroz/cxxopts that referenced this issue Jul 9, 2022
* We now use a vector of long option names instead of a single name
* When specifying an option, you can provide multiple names separated by commas, at most one of which may have a length of 1 (not necessarily the first specified name). The length-1 name is the single-hyphen switch (the "short name").
* Hashing uses the first long name
* Option help currently only uses the first long name. Not sure if that should change.
* No tests for this functionality yet
* Not sure whether option lookup works already with names other than the first...
@eyalroz
Copy link
Contributor Author

eyalroz commented Jul 9, 2022

@artpaul : Basically for the same reason you want both a short name and a long name: Convenience for the user. This way they don't have to remember whether to type, for example --with-foo=bar or --foo=bar or --enable-foo=bar - any of them would work.

It also makes it easier to change option names without breaking existing scripts.

eyalroz added a commit to eyalroz/cxxopts that referenced this issue Jul 9, 2022
* We now use a vector of long option names instead of a single name
* When specifying an option, you can provide multiple names separated by commas, at most one of which may have a length of 1 (not necessarily the first specified name). The length-1 name is the single-hyphen switch (the "short name").
* Hashing uses the first long name
* Option help currently only uses the first long name. Not sure if that should change.
* No tests for this functionality yet
* Not sure whether option lookup works already with names other than the first...
eyalroz added a commit to eyalroz/cxxopts that referenced this issue Jul 9, 2022
* We now use a vector of long option names instead of a single name
* When specifying an option, you can provide multiple names separated by commas, at most one of which may have a length of 1 (not necessarily the first specified name). The length-1 name is the single-hyphen switch (the "short name").
* Hashing uses the first long name
* Option help currently only uses the first long name. Not sure if that should change.
* No tests for this functionality yet
* Not sure whether option lookup works already with names other than the first...
eyalroz added a commit to eyalroz/cxxopts that referenced this issue Jul 9, 2022
* We now use a vector of long option names instead of a single name
* When specifying an option, you can provide multiple names separated by commas, at most one of which may have a length of 1 (not necessarily the first specified name). The length-1 name is the single-hyphen switch (the "short name").
* Hashing uses the first long name
* Option help currently only uses the first long name. Not sure if that should change.
* No tests for this functionality yet
* Not sure whether option lookup works already with names other than the first...
@eyalroz eyalroz changed the title Support option name aliases Support option name aliases / multiple long options Jul 9, 2022
@eyalroz
Copy link
Contributor Author

eyalroz commented Jul 9, 2022

@KappaDistributive : Your input on my WIP would be appreciated.

eyalroz added a commit to eyalroz/cxxopts that referenced this issue Jul 11, 2022
* Now also covering the no-regex case
* Using `std::token_regex_iterator` to tokenize for option-splitting instead of doing it "ourselves"
* Still almost no tests for this functionality
eyalroz added a commit to eyalroz/cxxopts that referenced this issue Jul 12, 2022
* We now use a vector of long option names instead of a single name
* When specifying an option, you can provide multiple names separated by commas, at most one of which may have a length of 1 (not necessarily the first specified name). The length-1 name is the single-hyphen switch (the "short name").
* Hashing uses the first long name
* Option help currently only uses the first long name. Not sure if that should change.
* Almost no tests for this functionality yet
* Using `std::token_regex_iterator` to tokenize for option-splitting
eyalroz added a commit to eyalroz/cxxopts that referenced this issue Jul 12, 2022
* We now use a vector of long option names instead of a single name
* When specifying an option, you can provide multiple names separated by commas, at most one of which may have a length of 1 (not necessarily the first specified name). The length-1 name is the single-hyphen switch (the "short name").
* Hashing uses the first long name
* Option help currently only uses the first long name. Not sure if that should change.
* Almost no tests for this functionality yet
* Using `std::token_regex_iterator` to tokenize for option-splitting
eyalroz added a commit to eyalroz/cxxopts that referenced this issue Jul 12, 2022
* We now use a vector of long option names instead of a single name
* When specifying an option, you can provide multiple names separated by commas, at most one of which may have a length of 1 (not necessarily the first specified name). The length-1 name is the single-hyphen switch (the "short name").
* Hashing uses the first long name
* Option help currently only uses the first long name. Not sure if that should change.
* Almost no tests for this functionality yet
* Using `std::token_regex_iterator` to tokenize for option-splitting
eyalroz added a commit to eyalroz/cxxopts that referenced this issue Jul 12, 2022
amirsojoodi added a commit to amirsojoodi/cxxopts that referenced this issue Nov 30, 2023
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

4 participants