Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arguments parsing implementation #1

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#include <iostream>

#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;
}
7 changes: 7 additions & 0 deletions src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
cmake_minimum_required(VERSION 3.20.0)

target_sources(vgraph_lib
PRIVATE
args.cpp
PUBLIC
args.h
)

add_subdirectory(logging)
34 changes: 34 additions & 0 deletions src/utils/args.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "args.h"
#include <utility>

#include <cstring>

#include <iostream>

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
18 changes: 18 additions & 0 deletions src/utils/args.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef ARGS_H
#define ARGS_H

namespace vgraph {
namespace utils {

struct args {
bool help = false;
bool debug = false;

static args parse(int argc, char* argv[]);
static void print_help();
};

} // namespace utils
} // namespace vgraph

#endif // ARGS_H
1 change: 0 additions & 1 deletion test/testdata/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
5 changes: 5 additions & 0 deletions test/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
cmake_minimum_required(VERSION 3.20.0)

target_sources(vgraph_test
PRIVATE
args_test.cpp
)

add_subdirectory(logging)
77 changes: 77 additions & 0 deletions test/utils/args_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <gtest/gtest.h>
#include <cstring>

#include "utils/args.h"

namespace vgraph {
namespace utils {

class args_base_test {
protected:
args call_parse(std::vector<std::string> 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<std::tuple<const char*, bool args::*>> {};

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