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

[WIP] Generalized flag values #229

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ add_cli_exe(enum enum.cpp)
add_test(NAME enum_pass COMMAND enum -l 1)
add_test(NAME enum_fail COMMAND enum -l 4)
set_property(TEST enum_fail PROPERTY PASS_REGULAR_EXPRESSION
"--level: 4 not in {0,1,2}")
"--level: 4 not in {")

add_cli_exe(digit_args digit_args.cpp)
add_test(NAME digit_args COMMAND digit_args -h)
set_property(TEST digit_args PROPERTY PASS_REGULAR_EXPRESSION
"-3{3}")

add_cli_exe(modhelp modhelp.cpp)
add_test(NAME modhelp COMMAND modhelp -a test -h)
Expand Down
15 changes: 15 additions & 0 deletions examples/digit_args.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <CLI/CLI.hpp>
#include <iostream>

int main(int argc, char **argv) {
CLI::App app;

int val;
// add a set of flags with default values associate with them
app.add_flag("-1{1},-2{2},-3{3},-4{4},-5{5},-6{6}, -7{7}, -8{8}, -9{9}", val, "compression level");

CLI11_PARSE(app, argc, argv);

std::cout << "value = " << val << std::endl;
return 0;
}
19 changes: 6 additions & 13 deletions examples/enum.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
#include <CLI/CLI.hpp>
#include <sstream>

enum class Level : int { High, Medium, Low };

std::istream &operator>>(std::istream &in, Level &level) {
int i;
in >> i;
level = static_cast<Level>(i);
return in;
}

std::ostream &operator<<(std::ostream &in, const Level &level) { return in << static_cast<int>(level); }

int main(int argc, char **argv) {
CLI::App app;

Level level;
app.add_option("-l,--level", level, "Level settings")
->check(CLI::IsMember({Level::High, Level::Medium, Level::Low}))
->type_name("enum/Level in {High=0, Medium=1, Low=2}");
// specify string->value mappings
std::vector<std::pair<std::string, Level>> map{
{"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}};
// checked Transform does the translation and checks the results are either in one of the strings or one of the
// translations already
app.add_option("-l,--level", level, "Level settings")->transform(CLI::CheckedTransformer(map, CLI::ignore_case));

CLI11_PARSE(app, argc, argv);

Expand Down
Loading