[ox/clargs] Make accessors noexcept

This commit is contained in:
Gary Talent 2021-04-01 21:59:04 -05:00
parent bcc4b5f816
commit 61fb5207ea
2 changed files with 36 additions and 14 deletions

View File

@ -11,7 +11,7 @@
namespace ox { namespace ox {
ClArgs::ClArgs(int argc, const char **args) { ClArgs::ClArgs(int argc, const char **args) noexcept {
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
String arg = args[i]; String arg = args[i];
if (arg[0] == '-') { if (arg[0] == '-') {
@ -19,7 +19,6 @@ ClArgs::ClArgs(int argc, const char **args) {
arg = arg.c_str() + 1; arg = arg.c_str() + 1;
} }
m_bools[arg] = true; m_bools[arg] = true;
// parse additional arguments // parse additional arguments
if (i < argc && args[i + 1]) { if (i < argc && args[i + 1]) {
String val = args[i + 1]; String val = args[i + 1];
@ -38,18 +37,34 @@ ClArgs::ClArgs(int argc, const char **args) {
} }
} }
bool ClArgs::getBool(const char *arg) const { bool ClArgs::getBool(const char *arg, bool defaultValue) const noexcept {
auto out = m_ints.at(arg); auto [value, err] = m_ints.at(arg);
return out.value ? *out.value : false; return !err ? *value : defaultValue;
} }
String ClArgs::getString(const char *argName, const char *defaultArg) const { String ClArgs::getString(const char *arg, const char *defaultValue) const noexcept {
return m_strings.contains(argName) ? m_strings.at(argName).value->c_str() : defaultArg; auto [value, err] = m_strings.at(arg);
return !err ? *value : defaultValue;
} }
int ClArgs::getInt(const char *arg) const { int ClArgs::getInt(const char *arg, int defaultValue) const noexcept {
auto out = m_ints.at(arg); auto [value, err] = m_ints.at(arg);
return out.value ? *out.value : 0; return !err ? *value : defaultValue;
}
Result<bool> ClArgs::getBool(const char *arg) const noexcept {
oxRequire(out, m_bools.at(arg));
return *out;
}
Result<String> ClArgs::getString(const char *argName) const noexcept {
oxRequire(out, m_strings.at(argName));
return *out;
}
Result<int> ClArgs::getInt(const char *arg) const noexcept {
oxRequire(out, m_ints.at(arg));
return *out;
} }
} }

View File

@ -21,13 +21,20 @@ class ClArgs {
HashMap<String, int> m_ints; HashMap<String, int> m_ints;
public: public:
ClArgs(int argc, const char **args); ClArgs(int argc, const char **args) noexcept;
bool getBool(const char *arg) const; bool getBool(const char *arg, bool defaultValue) const noexcept;
String getString(const char *argName, const char *defaultArg = "") const; String getString(const char *argName, const char *defaultArg) const noexcept;
int getInt(const char *arg, int defaultValue) const noexcept;
Result<bool> getBool(const char *arg) const noexcept;
Result<String> getString(const char *argName) const noexcept;
Result<int> getInt(const char *arg) const noexcept;
int getInt(const char *arg) const;
}; };
} }