jarro2783 / cxxopts

Lightweight C++ command line option parser
MIT License
4.16k stars 582 forks source link

Extend cxxopts API to support std::optional non-throwing query of values #421

Closed nigels-com closed 6 months ago

nigels-com commented 6 months ago

We have some internal usage of std::optional that can integrate very tidily with cxxopts providing this query for std::optional value of given name and type.

No need for an if-check of count or catching an exception.


This change is Reviewable

jarro2783 commented 6 months ago

Thanks for your PR. I will take a look properly when I get a chance in a couple of days.

nigels-com commented 6 months ago

No rush. We're having some interesting internal discussion. Want to check a few assumptions when I get a chance.

jarro2783 commented 6 months ago

This looks good, I'm also thinking about whether there is a better name for the function. I wonder if there some convention from other projects like something with maybe in the name.

jarro2783 commented 6 months ago

What do you think about calling it maybe_as, I think that fits the convention when a function can return an optional, and makes it clear that it is the same behaviour as as.

nigels-com commented 6 months ago

I was thinking of make_optional in the manner of std::make_shared.

It turns out that std::make_optional does already exist too. (News to me!)

nigels-com commented 6 months ago

I understand your reasoning with maybe but isn't that just a synonym for optional? Is clarity improved by assigning a "maybe" to an "optional"?

jarro2783 commented 6 months ago

Or as_optional might be even clearer. I'm just thinking that optional by itself is not the clearest name as to what it does on that class.

nigels-com commented 6 months ago

Yes, as_optional sounds good to me.

nigels-com commented 6 months ago

Updated as discussed.