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

View File

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