From e766f00b074fe48357e2ae52649b77a082e55b99 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Mon, 9 Apr 2018 10:15:25 +0200 Subject: [PATCH] Adding examples to tests --- examples/CMakeLists.txt | 60 +++++++++++++++++++ examples/inter_argument_order.cpp | 10 ++-- examples/modhelp.cpp | 9 ++- examples/prefix_command.cpp | 8 +-- examples/simple.cpp | 2 +- examples/subcom_in_files/CMakeLists.txt | 2 +- .../{main.cpp => subcommand_main.cpp} | 0 examples/subcommands.cpp | 8 ++- 8 files changed, 79 insertions(+), 20 deletions(-) rename examples/subcom_in_files/{main.cpp => subcommand_main.cpp} (100%) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8c2f13b3c..f3911d0b7 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -15,11 +15,71 @@ function(add_cli_exe T) endfunction() add_cli_exe(simple simple.cpp) +add_test(NAME simple_basic COMMAND simple) +add_test(NAME simple_all COMMAND simple -f filename.txt -c 12 --flag --flag -d 1.2) +set_property(TEST simple_all PROPERTY PASS_REGULAR_EXPRESSION + "Working on file: filename.txt, direct count: 1, opt count: 1" + "Working on count: 12, direct count: 1, opt count: 1" + "Received flag: 2 (2) times" + "Some value: 1.2") + + add_cli_exe(subcommands subcommands.cpp) +add_test(NAME subcommands_none COMMAND subcommands) +set_property(TEST subcommands_none PROPERTY + PASS_REGULAR_EXPRESSION "A subcommand is required") +add_test(NAME subcommands_all COMMAND subcommands --random start --file name stop --count) +set_property(TEST subcommands_all PROPERTY PASS_REGULAR_EXPRESSION + "Working on --file from start: name" + "Working on --count from stop: 1, direct count: 1" + "Count of --random flag: 1" + "Subcommand: start" + "Subcommand: stop") + add_cli_exe(groups groups.cpp) +add_test(NAME groups_none COMMAND groups) +set_property(TEST groups_none PROPERTY PASS_REGULAR_EXPRESSION + "This is a timer:" + "--file is required" + "Run with --help for more information.") +add_test(NAME groups_all COMMAND groups --file this --count --count -d 1.2) +set_property(TEST groups_all PROPERTY PASS_REGULAR_EXPRESSION + "This is a timer:" + "Working on file: this, direct count: 1, opt count: 1" + "Working on count: 2, direct count: 2, opt count: 2" + "Some value: 1.2") + add_cli_exe(inter_argument_order inter_argument_order.cpp) +add_test(NAME inter_argument_order COMMAND inter_argument_order --foo 1 2 3 --x --bar 4 5 6 --z --foo 7 8) +set_property(TEST inter_argument_order PROPERTY PASS_REGULAR_EXPRESSION + [=[foo : 1 +foo : 2 +foo : 3 +bar : 4 +bar : 5 +bar : 6 +foo : 7 +foo : 8]=]) + add_cli_exe(prefix_command prefix_command.cpp) +add_test(NAME prefix_command COMMAND prefix_command -v 3 2 1 -- other one two 3) +set_property(TEST prefix_command PROPERTY PASS_REGULAR_EXPRESSION + "Prefix: 3 : 2 : 1" + "Remaining commands: -- other one two 3") + 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 + "Could not convert: -l,--level = 4") + add_cli_exe(modhelp modhelp.cpp) +add_test(NAME modhelp COMMAND modhelp -a test -h) +set_property(TEST modhelp PROPERTY PASS_REGULAR_EXPRESSION + "Option -a string in help: test") add_subdirectory(subcom_in_files) +add_test(NAME subcom_in_files COMMAND subcommand_main subcommand_a -f this.txt --with-foo) +set_property(TEST subcom_in_files PROPERTY PASS_REGULAR_EXPRESSION + "Working on file: this\.txt" + "Using foo!") diff --git a/examples/inter_argument_order.cpp b/examples/inter_argument_order.cpp index 6609fd8be..9e3c246b9 100644 --- a/examples/inter_argument_order.cpp +++ b/examples/inter_argument_order.cpp @@ -5,15 +5,15 @@ #include int main(int argc, char **argv) { - CLI::App app; + CLI::App app{"An app to practice mixing unlimited arguments, but still recover the original order."}; std::vector foos; - auto foo = app.add_option("--foo,-f", foos); + auto foo = app.add_option("--foo,-f", foos, "Some unlimited argument"); std::vector bars; - auto bar = app.add_option("--bar", bars); + auto bar = app.add_option("--bar", bars, "Some unlimited arggument"); - app.add_flag("--z,--x"); // Random other flags + app.add_flag("--z,--x", "Random other flags"); // Standard parsing lines (copy and paste in, or use CLI11_PARSE) try { @@ -22,7 +22,7 @@ int main(int argc, char **argv) { return app.exit(e); } - // I perfer using the back and popping + // I prefer using the back and popping std::reverse(std::begin(foos), std::end(foos)); std::reverse(std::begin(bars), std::end(bars)); diff --git a/examples/modhelp.cpp b/examples/modhelp.cpp index 7533bf69c..a2568322a 100644 --- a/examples/modhelp.cpp +++ b/examples/modhelp.cpp @@ -1,12 +1,11 @@ -// Modify the help print so that argument values are accessible -// Note that this will not shortcut `->required` and other similar options #include "CLI/CLI.hpp" #include int main(int argc, char **argv) { - CLI::App test; + CLI::App test{R"raw(Modify the help print so that argument values are accessible. +Note that this will not shortcut `->required` and other similar options.)raw"}; // Remove help flag because it shortcuts all processing test.set_help_flag(); @@ -22,10 +21,10 @@ int main(int argc, char **argv) { if(*help) throw CLI::CallForHelp(); } catch(const CLI::Error &e) { - std::cout << "Option string:" << some_option << std::endl; + std::cout << "Option -a string in help: " << some_option << std::endl; return test.exit(e); } - std::cout << "Option string:" << some_option << std::endl; + std::cout << "Option -a string: " << some_option << std::endl; return 0; } diff --git a/examples/prefix_command.cpp b/examples/prefix_command.cpp index 3a3a6f092..edd7a0d68 100644 --- a/examples/prefix_command.cpp +++ b/examples/prefix_command.cpp @@ -6,20 +6,18 @@ int main(int argc, char **argv) { app.prefix_command(); std::vector vals; - app.add_option("--vals,-v", vals)->expected(1); + app.add_option("--vals,-v", vals)->expected(-1); CLI11_PARSE(app, argc, argv); std::vector more_comms = app.remaining(); - std::cout << "Prefix:"; + std::cout << "Prefix"; for(int v : vals) - std::cout << v << ":"; + std::cout << ": " << v << " "; std::cout << std::endl << "Remaining commands: "; - // Perfer to loop over from beginning, not "pop" order - std::reverse(std::begin(more_comms), std::end(more_comms)); for(auto com : more_comms) std::cout << com << " "; std::cout << std::endl; diff --git a/examples/simple.cpp b/examples/simple.cpp index 612fd9e02..d1ae4716b 100644 --- a/examples/simple.cpp +++ b/examples/simple.cpp @@ -22,7 +22,7 @@ int main(int argc, char **argv) { << ", opt count: " << opt->count() << std::endl; std::cout << "Working on count: " << count << ", direct count: " << app.count("--count") << ", opt count: " << copt->count() << std::endl; - std::cout << "Recieved flag: " << v << " (" << flag->count() << ") times\n"; + std::cout << "Received flag: " << v << " (" << flag->count() << ") times\n"; std::cout << "Some value: " << value << std::endl; return 0; diff --git a/examples/subcom_in_files/CMakeLists.txt b/examples/subcom_in_files/CMakeLists.txt index be283a6bd..74ba5dd32 100644 --- a/examples/subcom_in_files/CMakeLists.txt +++ b/examples/subcom_in_files/CMakeLists.txt @@ -1 +1 @@ -add_cli_exe(main main.cpp subcommand_a.cpp subcommand_a.hpp) +add_cli_exe(subcommand_main subcommand_main.cpp subcommand_a.cpp subcommand_a.hpp) diff --git a/examples/subcom_in_files/main.cpp b/examples/subcom_in_files/subcommand_main.cpp similarity index 100% rename from examples/subcom_in_files/main.cpp rename to examples/subcom_in_files/subcommand_main.cpp diff --git a/examples/subcommands.cpp b/examples/subcommands.cpp index a28b2aac2..407aaaa5c 100644 --- a/examples/subcommands.cpp +++ b/examples/subcommands.cpp @@ -6,6 +6,7 @@ int main(int argc, char **argv) { app.add_flag("--random", "Some random flag"); CLI::App *start = app.add_subcommand("start", "A great subcommand"); CLI::App *stop = app.add_subcommand("stop", "Do you really want to stop?"); + app.require_subcommand(); // 1 or more std::string file; start->add_option("-f,--file", file, "File name"); @@ -14,10 +15,11 @@ int main(int argc, char **argv) { CLI11_PARSE(app, argc, argv); - std::cout << "Working on file: " << file << ", direct count: " << start->count("--file") << std::endl; - std::cout << "Working on count: " << s->count() << ", direct count: " << stop->count("--count") << std::endl; + std::cout << "Working on --file from start: " << file << std::endl; + std::cout << "Working on --count from stop: " << s->count() << ", direct count: " << stop->count("--count") << std::endl; + std::cout << "Count of --random flag: " << app.count("--random") << std::endl; for(auto subcom : app.get_subcommands()) - std::cout << "Subcommand:" << subcom->get_name() << std::endl; + std::cout << "Subcommand: " << subcom->get_name() << std::endl; return 0; }