You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Semantically, an optional argument is "optional" to present. But currently, if default_value isn't set, an optional argument's "not presented" state is not representable. Attempting to access the state (value) will get a logic_error. Representing the "not presented" state is often necessary because the option's type may leave no value for a good default_value. For example, an empty string may be an valid argument to an option.
Python argparse has no such problem because any option may be None. Universally Nullable is a problem, but allowing None for specific types is adopted by many languages. In C++ it's std::optional<T>.
I suggest to add a program.present<T>("--option") API in addition to the existing program.get<T>("--option") API. The new API returns std::optional<T> by reifying T's possible values and the "not presented" state. Its usage looks like the following:
if (auto v = program.present<int>("-n")) {
do_something_with(*v);
}
The name "present" is taken from Java's Optional.ifPresent. Bike-shedding is mildly welcome.
The text was updated successfully, but these errors were encountered:
Semantically, an optional argument is "optional" to present. But currently, if
default_value
isn't set, an optional argument's "not presented" state is not representable. Attempting to access the state (value) will get alogic_error
. Representing the "not presented" state is often necessary because the option's type may leave no value for a gooddefault_value
. For example, an empty string may be an valid argument to an option.Python argparse has no such problem because any option may be
None
. Universally Nullable is a problem, but allowingNone
for specific types is adopted by many languages. In C++ it'sstd::optional<T>
.I suggest to add a
program.present<T>("--option")
API in addition to the existingprogram.get<T>("--option")
API. The new API returnsstd::optional<T>
by reifyingT
's possible values and the "not presented" state. Its usage looks like the following:The name "present" is taken from Java's
Optional.ifPresent
. Bike-shedding is mildly welcome.The text was updated successfully, but these errors were encountered: