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

Update cmdline.h to support more parameters #471

Merged
merged 8 commits into from
Jan 4, 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
121 changes: 7 additions & 114 deletions include/cmdline/cmdline.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,126 +176,19 @@ struct oneof_reader {
throw cmdline_error("");
return ret;
}
template <typename... Args>
oneof_reader(Args &&...args) {
add(std::forward<Args>(args)...);
}
void add(const T &v) { alt.push_back(v); }

private:
std::vector<T> alt;
};

template <class T>
oneof_reader<T> oneof(T a1) {
oneof_reader<T> ret;
ret.add(a1);
return ret;
}

template <class T>
oneof_reader<T> oneof(T a1, T a2) {
oneof_reader<T> ret;
ret.add(a1);
ret.add(a2);
return ret;
}

template <class T>
oneof_reader<T> oneof(T a1, T a2, T a3) {
oneof_reader<T> ret;
ret.add(a1);
ret.add(a2);
ret.add(a3);
return ret;
}

template <class T>
oneof_reader<T> oneof(T a1, T a2, T a3, T a4) {
oneof_reader<T> ret;
ret.add(a1);
ret.add(a2);
ret.add(a3);
ret.add(a4);
return ret;
}

template <class T>
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5) {
oneof_reader<T> ret;
ret.add(a1);
ret.add(a2);
ret.add(a3);
ret.add(a4);
ret.add(a5);
return ret;
}

template <class T>
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6) {
oneof_reader<T> ret;
ret.add(a1);
ret.add(a2);
ret.add(a3);
ret.add(a4);
ret.add(a5);
ret.add(a6);
return ret;
}

template <class T>
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7) {
oneof_reader<T> ret;
ret.add(a1);
ret.add(a2);
ret.add(a3);
ret.add(a4);
ret.add(a5);
ret.add(a6);
ret.add(a7);
return ret;
}

template <class T>
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8) {
oneof_reader<T> ret;
ret.add(a1);
ret.add(a2);
ret.add(a3);
ret.add(a4);
ret.add(a5);
ret.add(a6);
ret.add(a7);
ret.add(a8);
return ret;
}

template <class T>
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8, T a9) {
oneof_reader<T> ret;
ret.add(a1);
ret.add(a2);
ret.add(a3);
ret.add(a4);
ret.add(a5);
ret.add(a6);
ret.add(a7);
ret.add(a8);
ret.add(a9);
return ret;
}

template <class T>
oneof_reader<T> oneof(T a1, T a2, T a3, T a4, T a5, T a6, T a7, T a8, T a9,
T a10) {
oneof_reader<T> ret;
ret.add(a1);
ret.add(a2);
ret.add(a3);
ret.add(a4);
ret.add(a5);
ret.add(a6);
ret.add(a7);
ret.add(a8);
ret.add(a9);
ret.add(a10);
return ret;
template <class T, typename... Args>
oneof_reader<T> oneof(Args &&...args) {
return oneof_reader<T>(std::forward<Args>(args)...);
}

//-----
Expand Down
37 changes: 36 additions & 1 deletion tests/test_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,41 @@ TEST_CASE("simple test cmd line options") {
CHECK(p.get<int>("threads") == 4);
}

// ./test_gt10 -a A -b B -c C -d D -e E -f F -gh H -i I -j J -k 8
TEST_CASE("test cmd line options more than 10") {
const char* argv[] = {"test_gt10", "-a", "AA", "-b", "B", "-c", "C", "-d",
"D", "-e", "E", "-f", "F", "-g", "G", "-h",
"H", "-i", "I", "-j", "J", "-k8"};
int argc = sizeof(argv) / sizeof(argv[0]);
cmdline::parser p;

p.add<std::string>("arg_a", 'a', "argument vector a");
p.add<std::string>("arg_b", 'b', "argument vector b");
p.add<std::string>("arg_c", 'c', "argument vector c");
p.add<std::string>("arg_d", 'd', "argument vector d");
p.add<std::string>("arg_e", 'e', "argument vector e");
p.add<std::string>("arg_f", 'f', "argument vector f");
p.add<std::string>("arg_g", 'g', "argument vector g");
p.add<std::string>("arg_h", 'h', "argument vector h");
p.add<std::string>("arg_i", 'i', "argument vector i");
p.add<std::string>("arg_j", 'j', "argument vector j");
p.add<int>("arg_k", 'k', "argument vector k", false, 1);

p.parse_check(argc, const_cast<char**>(argv));

CHECK(p.get<std::string>("arg_a") == "AA");
CHECK(p.get<std::string>("arg_b") == "B");
CHECK(p.get<std::string>("arg_c") == "C");
CHECK(p.get<std::string>("arg_d") == "D");
CHECK(p.get<std::string>("arg_e") == "E");
CHECK(p.get<std::string>("arg_f") == "F");
CHECK(p.get<std::string>("arg_g") == "G");
CHECK(p.get<std::string>("arg_h") == "H");
CHECK(p.get<std::string>("arg_i") == "I");
CHECK(p.get<std::string>("arg_j") == "J");
CHECK(p.get<int>("arg_k") == 8);
}

// ./cinatra_press_tool -c100 -t4 -d10s --headers=HTTPheaders -r7
TEST_CASE("test cinatra_press_tool cmd line options without spaces") {
const char* argv[] = {"cinatra_press_tool", "-c100", "-vt4", "-d10s",
Expand Down Expand Up @@ -89,4 +124,4 @@ TEST_CASE("test cinatra_press_tool cmd line options with spaces") {
CHECK(p.get<std::string>("headers") == "HTTPheaders");
CHECK(p.get<int>("readfix") == 7);
CHECK(p.exist("version"));
}
}
Loading