[ox/clargs] Add constructor that takes a SpanView

This commit is contained in:
Gary Talent 2024-11-28 00:51:02 -06:00
parent 3308b4dd72
commit edda8e010e
2 changed files with 10 additions and 6 deletions

View File

@ -11,18 +11,19 @@
namespace ox { namespace ox {
ClArgs::ClArgs(int argc, const char **args) noexcept { ClArgs::ClArgs(int argc, const char **args) noexcept: ClArgs({args, static_cast<size_t>(argc)}) {}
auto const argv = ox::SpanView{args, static_cast<size_t>(argc)};
for (auto i = 0u; i < static_cast<unsigned>(argc); ++i) { ClArgs::ClArgs(ox::SpanView<const char*> args) noexcept {
auto arg = StringView(argv[i]); for (auto i = 0u; i < args.size(); ++i) {
auto arg = StringView(args[i]);
if (arg[0] == '-') { if (arg[0] == '-') {
while (arg[0] == '-' && arg.len()) { while (arg[0] == '-' && arg.len()) {
arg = substr(arg, 1); arg = substr(arg, 1);
} }
m_bools[arg] = true; m_bools[arg] = true;
// parse additional arguments // parse additional arguments
if (i < static_cast<unsigned>(argc) && argv[i + 1]) { if (i < args.size() && args[i + 1]) {
auto val = String(argv[i + 1]); auto val = String(args[i + 1]);
if (val.len() && val[i] != '-') { if (val.len() && val[i] != '-') {
if (val == "false") { if (val == "false") {
m_bools[arg] = false; m_bools[arg] = false;

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <ox/std/hashmap.hpp> #include <ox/std/hashmap.hpp>
#include <ox/std/span.hpp>
#include <ox/std/string.hpp> #include <ox/std/string.hpp>
namespace ox { namespace ox {
@ -22,6 +23,8 @@ class ClArgs {
public: public:
ClArgs(int argc, const char **args) noexcept; ClArgs(int argc, const char **args) noexcept;
ClArgs(ox::SpanView<const char*> args) noexcept;
[[nodiscard]] [[nodiscard]]
bool getBool(ox::StringViewCR arg, bool defaultValue) const noexcept; bool getBool(ox::StringViewCR arg, bool defaultValue) const noexcept;