-
Notifications
You must be signed in to change notification settings - Fork 92
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
Consider adding named accessor methods #11
Comments
following our discussion in twitter:
std::string operator[](size_t index) const; // as std::vector, std::string, std::array, QVector, QList, ...
std::string operator[](std::string const& name) const; // as std::map, QHash, QMap, ...
// also like std or Qt containers
std::string at(size_t index) const;
std::string at(std::string const& name) const;
// or
template<tynename T>
T value(std::string const& name, T&& default_value) const;
bool is_set(std::string const& name) const;
bool is_set(std::initializer_list<char const* const> init_list) const; Qt
template<class... Args>
string_stream to_stream(Args&&... args) const {
return operator()(std::forward<Args>(args)...); // by current implementation
}
p.s GSL has also some tips for operators. |
I've already fixed const correctness.
Need to push it.
Re: the rest will reply soon.
|
Argh recognizes and demuxes the command line into, 3 different and independent argument types:
Each corresponds to a conceptually different container with different requirements:
Design RationaleArgh supports these multiple intersecting requirements with a minimal set of (conceptually) 3 functions (if we ignore alt-names and defaults for a moment):
So:
Also:
Design ConsiderationsArgh's design optimizes for:
[These may not be a comprehensive list but will do for now] Given these considerations, any additional named functions should retain the exiting principles. So your suggested function If we need a named method, I am leaning towards Regarding
to:
This is much shorter, and IMO just as clear if nor clearer. But this is a subjective matter though. One thing you mentioned is that it isn't possible to initialize a local variable with a non string cli value. You need to declare it first before you can stream into it. This bugged me because it goes against the principle of lowest-possible-impact on user code. So, I am experimenting with a templated
I'm still mulling this over. Finally, I also like the minimalism of: |
your design rationale about three categories of flags, positional and parameters looks familiar and comprehensible for every command line user! you've already classified them as: // parser:
std::multiset<std::string> const& flags();
std::map<std::string, std::string> const& params();
std::vector<std::string> const& pos_args() const;
well, i didn't. i expect imho:
minimal changeswith minimal changes (by separating flags from // operator[] overloads for pos_args / flags
auto action = cmdl[2]; // also may return an empty string
if (action.empty()) {}
if (cmdl["amend"]) {}
// operator() overloads for pos_args / params. may return bad_stream
auto stream = cmdl(2); // stream for positional arg
auto stream = cmdl("username"); // stream for parameter turns into: // operator[] or at() overloads for pos_args / params
auto action = cmdl[2];
auto user = cmdl["username"];
if (cmdl.is_set("amend")) {} there should be minimal surprise for hiding (or deprecating) has_xxx()i really like your idea about return valuesthe current design may also return an empty string or a bad stream (and i'm not against it). the user is supposed to check the return values: // current design:
// given 2 positional args:
auto action = cmdl[7]; // action is empty
// given no argument as port:
uint16_t port{0};
cmdl("port") >> port; // a bad_stream, port is not set exception safety
as<>()it is short and looks nice, but |
There has been a request to add named accessor methods in addition to
operator[]
andoperator()
.See Twitter thread by @azadkuh here.
Though simple to do, there are several subtle aspects that need to be addressed.
I will use this issue to contemplate, document and discuss them.
I have an (as yet non-public) experimental branch with this, but I'm not yet sure about this.
(cc: @arnemertz)
The text was updated successfully, but these errors were encountered: