-
Notifications
You must be signed in to change notification settings - Fork 274
cmdlinet
: add value_opt
methods
#8525
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
Conversation
This adds an alternative to cmdlinet::get_value, which returns std::optional, as opposed to defaulting to an empty string when the option isn't set.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #8525 +/- ##
===========================================
+ Coverage 78.58% 78.85% +0.26%
===========================================
Files 1728 1728
Lines 199522 198844 -678
Branches 18374 18379 +5
===========================================
+ Hits 156792 156795 +3
+ Misses 42730 42049 -681 ☔ View full report in Codecov by Sentry. |
I had the same thought when seeing your hw-cbmc PR... |
std::optional<std::string> value_opt(char option) const; | ||
std::optional<std::string> value_opt(const char *option) const; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about changing this API to become value_or
? So that it would be used like this: cmdline.value_or("log", "-")
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be confusing given that it is the same method name as std::optional::value_or
. Right now what you do is only a little bit longer: cmdline.value_opt("log").value_or("-")
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could consider operator()
? Then it would be cmdline("log").value_or("-")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps value_or
isn't the best name to choose, but it still seems like a nuisance when the only thing we really want is value_opt
immediately followed by value_or
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is some (very modest) benefit in doing
auto foo_opt = cmdline.value_opt("foo");
if(foo_opt) .... foo_opt.value()
The benefit is that you avoid the double lookup in the argument map that you'd do with cmdline.isset("foo")
followed by cmdline.get_value("foo")
. But of course not measurable.
This adds an alternative to
cmdlinet::get_value
, which returns std::optional, as opposed to defaulting to an empty string when the option isn't set.