From f78eec792b67cca65fce89e3d12c49b0cb8b92dd Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 11 Aug 2023 12:10:00 -0400 Subject: [PATCH] make contains work with -- - or non flagged checks --- include/blt/parse/argparse.h | 4 ++++ src/tests/main.cpp | 23 ++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index b64714f..0f85c42 100755 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -287,6 +287,10 @@ namespace blt inline bool contains(const std::string& key) { + if (key.starts_with("--")) + return data.find(key.substr(2)) != data.end(); + if (key.starts_with('-')) + return data.find(key.substr(1)) != data.end(); return data.find(key) != data.end(); } } loaded_args; diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 3216bb8..85c081c 100755 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -70,9 +70,14 @@ int (*func_func_in)(int) = &test_as_func; int main(int argc, const char** argv) { blt::arg_parse parser; parser.addArgument(blt::arg_builder({"-c", "--no-color"}).setAction(blt::arg_action_t::STORE_TRUE).build()); + parser.addArgument(blt::arg_builder("--nbt").setHelp("Run NBT tests.").setAction(blt::arg_action_t::STORE_TRUE).build()); auto args = parser.parse_args(argc, argv); + for (auto& a : args) + BLT_TRACE(a.first); + BLT_TRACE(args.contains("nbt")); + if (args.contains("--no-color")) { for (int i = (int)blt::logging::log_level::NONE; i < (int)blt::logging::log_level::FATAL; i++) { blt::logging::setLogColor((blt::logging::log_level)i, ""); @@ -228,6 +233,7 @@ int main(int argc, const char** argv) { // // BLT_INFO("STDDEV of # random values: %f", stdev); + if (args.contains("--nbt")) { std::fstream nbtFile("super_file.nbt", std::ios::out | std::ios::binary); blt::fs::fstream_block_writer blockWriter(nbtFile); @@ -285,13 +291,16 @@ int main(int argc, const char** argv) { nbtFile.close(); } - std::fstream nbtInputFile("super_file.nbt", std::ios::in | std::ios::binary); - blt::fs::fstream_block_reader blockReader(nbtInputFile); - blt::nbt::NBTReader nbtReader(blockReader); - nbtReader.read(); - - auto shortTag = nbtReader.getTag("shortTest"); - BLT_TRACE("Got short: %d", shortTag->get()); + if (args.contains("--nbt")) + { + std::fstream nbtInputFile("super_file.nbt", std::ios::in | std::ios::binary); + blt::fs::fstream_block_reader blockReader(nbtInputFile); + blt::nbt::NBTReader nbtReader(blockReader); + nbtReader.read(); + + auto shortTag = nbtReader.getTag("shortTest"); + BLT_TRACE("Got short: %d", shortTag->get()); + } return 0; } \ No newline at end of file