parse names

v1
Brett 2023-07-31 13:53:10 -04:00
parent 620a16574d
commit e702a651df
3 changed files with 18 additions and 9 deletions

View File

@ -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<arg_properties> argStorage;
HASHMAP<std::string, arg_properties*> flagAssociations;
HASHMAP<std::string, arg_properties*> nameAssociations;
std::vector<std::pair<std::string, arg_properties*>> nameAssociations;
} user_args;
struct arg_results {

View File

@ -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;
}

View File

@ -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++) {