From e702a651dff719efeb6bc096a8bed6906a6fdaf6 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 31 Jul 2023 13:53:10 -0400 Subject: [PATCH] parse names --- include/blt/parse/argparse.h | 14 ++++++-------- src/blt/parse/argparse.cpp | 11 ++++++++++- src/tests/main.cpp | 2 ++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index 50d99a5..3ba77ec 100644 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -131,15 +131,9 @@ namespace blt::parser { inline const std::string& get(size_t i){ return args[i]; } - inline const std::string& next(size_t& i){ - return args[i++]; - } inline bool hasNext(size_t i){ return (size_t)i < args.size(); } - inline bool isFlag(size_t i){ - return get(i).starts_with(FLAG); - } public: arg_tokenizer() = default; @@ -154,13 +148,17 @@ namespace blt::parser { } inline const std::string& next() { - return next(nextIndex); + return get(nextIndex++); } inline bool hasNext() { return hasNext(nextIndex); } + inline bool isFlag(size_t i){ + return get(i).starts_with(FLAG); + } + inline bool isFlag() { return isFlag(nextIndex); } @@ -173,7 +171,7 @@ namespace blt::parser { struct { std::vector argStorage; HASHMAP flagAssociations; - HASHMAP nameAssociations; + std::vector> nameAssociations; } user_args; struct arg_results { diff --git a/src/blt/parse/argparse.cpp b/src/blt/parse/argparse.cpp index c3a068c..466a5df 100644 --- a/src/blt/parse/argparse.cpp +++ b/src/blt/parse/argparse.cpp @@ -118,7 +118,7 @@ namespace blt::parser { // associate the arg properties per name and flag to allow for quick access when parsing auto& names = args.a_flags.getNames(); for (const auto& name : names) - user_args.nameAssociations[name] = &arg; + user_args.nameAssociations.emplace_back(name, &arg); auto& flags = args.a_flags.getFlags(); for (const auto& flag : flags) @@ -131,6 +131,15 @@ namespace blt::parser { loaded_args.programName = asToken.next(); BLT_TRACE("Loading args for %s", loaded_args.programName.c_str()); + size_t lastPositional; + while (asToken.hasNext()) { + if (!asToken.isFlag()){ + loaded_args.positionalArgs[user_args.nameAssociations[lastPositional].first] = asToken.next(); + continue; + } + + } + return loaded_args; } diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 1ef3520..d1aeebe 100755 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -71,6 +71,8 @@ int main(int argc, const char** argv) { blt::parser::argparse parser; parser.addArgument({{"--foo", "-f"}}); auto args = parser.parse_args(argc, argv); + const char* r[2]{"BLT_TESTS", "--foo"}; + auto args2 = parser.parse_args(2, r); // // if (argc > 1 && std::string(argv[1]) == "--no_color") { // for (int i = (int)blt::logging::log_level::NONE; i < (int)blt::logging::log_level::FATAL; i++) {