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
{{ message }}
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
There is code to handle std::optionalviaCLARA_CONFIG_OPTIONAL_TYPE; however Opt still is expecting an argument if none is given.
For example for logging: ./program, run with no logging (default), ./program -l enable logging, ./program -l file.log enable logging to named file.
This would make sense to use std::optional here.
For example, the default will not do anything; giving the "flag" -l will act like a pass the log through to std::clog; giving the optional with a filename -l /path/to/file.log will pass through to the /path/to/file.log.
My current work around is to use a class with a set of stream operators.
edited slightly to only show the logging
namespaceutils {
// in utils.hxxclasslog {
public:log() = default;
explicitlog(std::ostream& os)
: m_log{os.rdbuf()} {}
explicitlog(fs::path const& path)
: m_log_{path}
, m_log{m_log_.rdbuf()} {}
template <typename T>
friendautoinlineoperator<<(utils::log& log, T const& value) -> utils::log& {
log.m_log << value;
returnlog;
}
friendautooperator>>(std::istream& is, log& log) -> std::istream&;
private:
std::ofstream m_log_{};
std::ostream m_log{nullptr};
};
// in utils.cxxautooperator>>(std::istream& is, log& log) -> std::istream& {
auto buf = std::string{};
is >> buf;
// check if boolean valueautoconststatic negative = std::array{"0"s, "false"s, "off"s, "n"s, "no"s};
autoconststatic positive = std::array{"1"s, "true"s, "on"s, "y"s, "yes"s};
auto boolean = buf;
std::transform(std::begin(boolean), std::end(boolean), std::begin(boolean),
[](unsignedchar c) { returnstd::tolower(c); });
if (std::find(std::begin(negative), std::end(negative), boolean) != std::end(negative)) {
// disable logging
} elseif (std::find(std::begin(positive), std::end(positive), boolean) != std::end(positive)) {
// enable logging to stderrlog.m_log.rdbuf(std::clog.rdbuf());
} else {
// enable logging to fileautoconst filename = fs::path{buf};
log.m_log_.open(filename);
log.m_log.rdbuf(log.m_log_.rdbuf());
}
return is;
}
} // namespace utils// relevant bit of parserbool m_help{false};
utils::log m_log{};
auto args = clara::Help(m_help)
| clara::Opt{m_log, "false|true|filename"s}["-l"s]["--log"s](
"<false> disable logging\n""<true> enable logging to stderr\n""<filename> enable logging to filename\t{false}"s);
As you can see it's not quite ideal; I also have to call it ./program -l true rather than ./program -l.
Considering Clara also has bool parsing, and std::optional, I think it would be a nice addition to the library.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
There is code to handle
std::optional
viaCLARA_CONFIG_OPTIONAL_TYPE
; howeverOpt
still is expecting an argument if none is given.For example for logging:
./program
, run with no logging (default),./program -l
enable logging,./program -l file.log
enable logging to named file.This would make sense to use
std::optional
here.For example, the default will not do anything; giving the "flag"
-l
will act like a pass the log through tostd::clog
; giving the optional with a filename-l /path/to/file.log
will pass through to the/path/to/file.log
.The text was updated successfully, but these errors were encountered: