From 56f148943fa7b3b02781b8270d54bdb0e91b3acb Mon Sep 17 00:00:00 2001 From: Przemyslaw Rokosz Date: Sun, 11 Aug 2024 09:37:31 +0200 Subject: [PATCH 1/2] wip --- src/utils/CMakeLists.txt | 7 +++++++ src/utils/args.cpp | 5 +++++ src/utils/args.h | 16 ++++++++++++++++ test/utils/CMakeLists.txt | 5 +++++ test/utils/args_test.cpp | 0 5 files changed, 33 insertions(+) create mode 100644 src/utils/args.cpp create mode 100644 src/utils/args.h create mode 100644 test/utils/args_test.cpp diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 0a5d4a4..1a8b24c 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,3 +1,10 @@ cmake_minimum_required(VERSION 3.20.0) +target_sources(vgraph_lib + PRIVATE + args.cpp + PUBLIC + args.h +) + add_subdirectory(logging) diff --git a/src/utils/args.cpp b/src/utils/args.cpp new file mode 100644 index 0000000..29e7be0 --- /dev/null +++ b/src/utils/args.cpp @@ -0,0 +1,5 @@ +#include "args.h" + +args args::parse(int argc, char* argv[]) +{ +} diff --git a/src/utils/args.h b/src/utils/args.h new file mode 100644 index 0000000..e8d7878 --- /dev/null +++ b/src/utils/args.h @@ -0,0 +1,16 @@ +#ifndef ARGS_H +#define ARGS_H + +namespace vgraph { +namespace utils { + +struct args { + bool debug; + + static args parse(int argc, char* argv[]); +}; + +} // namespace utils +} // namespace vgraph + +#endif // ARGS_H \ No newline at end of file diff --git a/test/utils/CMakeLists.txt b/test/utils/CMakeLists.txt index 0a5d4a4..84e465b 100644 --- a/test/utils/CMakeLists.txt +++ b/test/utils/CMakeLists.txt @@ -1,3 +1,8 @@ cmake_minimum_required(VERSION 3.20.0) +target_sources(vgraph_test + PRIVATE + args_test.cpp +) + add_subdirectory(logging) diff --git a/test/utils/args_test.cpp b/test/utils/args_test.cpp new file mode 100644 index 0000000..e69de29 From 8be6182a772bbbb1d3f17cc08e2b7ec1c1cc02e3 Mon Sep 17 00:00:00 2001 From: Przemyslaw Rokosz Date: Sun, 11 Aug 2024 11:17:44 +0200 Subject: [PATCH 2/2] working args parsing --- src/main.cpp | 16 +++++++- src/utils/args.cpp | 29 ++++++++++++++ src/utils/args.h | 4 +- test/testdata/CMakeLists.txt | 1 - test/utils/args_test.cpp | 77 ++++++++++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4de98cb..ecfe708 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,20 @@ #include +#include "utils/args.h" -int main() +int main(int argc, char* argv[]) { - std::cout << "Hello World!" << std::endl; + vgraph::utils::args a = vgraph::utils::args::parse(argc, argv); + + if (a.help) { + vgraph::utils::args::print_help(); + return 0; + } + + if (a.debug) + std::cout << "DEBUG" << std::endl; + else + std::cout << "NO DEBUG" << std::endl; + return 0; } diff --git a/src/utils/args.cpp b/src/utils/args.cpp index 29e7be0..0b6d722 100644 --- a/src/utils/args.cpp +++ b/src/utils/args.cpp @@ -1,5 +1,34 @@ #include "args.h" +#include + +#include + +#include + +namespace vgraph { +namespace utils { args args::parse(int argc, char* argv[]) { + args a; + + for (int i = 1; i < argc; i++) { + if (std::strcmp(argv[i], "-h") == 0 || std::strcmp(argv[i], "--help") == 0) + a.help = true; + else if (std::strcmp(argv[i], "-d") == 0 || std::strcmp(argv[i], "--debug") == 0) + a.debug = true; + } + + return std::move(a); } + +void args::print_help() +{ + std::cout << "Usage: vgraph [flags]" << std::endl; + std::cout << "Flags:" << std::endl; + std::cout << " -h, --help Print this help message" << std::endl; + std::cout << " -d, --debug Enable debug logs" << std::endl; +} + +} // namespace utils +} // namespace vgraph diff --git a/src/utils/args.h b/src/utils/args.h index e8d7878..51f6e41 100644 --- a/src/utils/args.h +++ b/src/utils/args.h @@ -5,9 +5,11 @@ namespace vgraph { namespace utils { struct args { - bool debug; + bool help = false; + bool debug = false; static args parse(int argc, char* argv[]); + static void print_help(); }; } // namespace utils diff --git a/test/testdata/CMakeLists.txt b/test/testdata/CMakeLists.txt index fc2f93d..edebff8 100644 --- a/test/testdata/CMakeLists.txt +++ b/test/testdata/CMakeLists.txt @@ -1,4 +1,3 @@ cmake_minimum_required(VERSION 3.20.0) target_compile_definitions(vgraph_test PRIVATE TESTDATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}") -target_compile_definitions(vgraph_test PRIVATE CHECK="abc") diff --git a/test/utils/args_test.cpp b/test/utils/args_test.cpp index e69de29..bdac114 100644 --- a/test/utils/args_test.cpp +++ b/test/utils/args_test.cpp @@ -0,0 +1,77 @@ +#include +#include + +#include "utils/args.h" + +namespace vgraph { +namespace utils { + +class args_base_test { +protected: + args call_parse(std::vector vargs) + { + int argc = vargs.size() + 1; + char* argv[argc]; + + argv[0] = new char[6]; + argv[0] = std::strcpy(argv[0], "dummy"); + + int i = 1; + for (const std::string& arg : vargs) { + argv[i] = new char[arg.size() + 1]; + std::strcpy(argv[i], arg.c_str()); + i++; + } + + args a = args::parse(argc, argv); + + for (int i = 0; i < argc; i++) { + delete[] argv[i]; + } + + return a; + } +}; + +class args_test: public args_base_test, public ::testing::Test {}; + +TEST_F(args_test, default_args) +{ + args a = call_parse({}); + + EXPECT_EQ(false, a.help); + EXPECT_EQ(false, a.debug); +} + + +class args_flag_test: public args_base_test, public ::testing::TestWithParam> {}; + +TEST_P(args_flag_test, boolean_flag_present) +{ + auto [flag, field] = GetParam(); + + args a = call_parse({flag}); + EXPECT_EQ(true, a.*field); +} + +TEST_P(args_flag_test, boolean_flag_missing) +{ + auto [flag, field] = GetParam(); + + args a = call_parse({}); + EXPECT_EQ(false, a.*field); +} + +INSTANTIATE_TEST_SUITE_P( + , + args_flag_test, + ::testing::Values( + std::make_tuple("-h", &args::help), + std::make_tuple("--help", &args::help), + std::make_tuple("-d", &args::debug), + std::make_tuple("--debug", &args::debug) + ) +); + +} // namespace utils +} // namespace vgraph