Commit 232c792bae2b494493f346cdab0890368b300412

Authored by Henry Fredrick Schreiner
Committed by Henry Schreiner
1 parent 0959430e

Adding examples to tests

examples/CMakeLists.txt
@@ -15,11 +15,71 @@ function(add_cli_exe T) @@ -15,11 +15,71 @@ function(add_cli_exe T)
15 endfunction() 15 endfunction()
16 16
17 add_cli_exe(simple simple.cpp) 17 add_cli_exe(simple simple.cpp)
  18 +add_test(NAME simple_basic COMMAND simple)
  19 +add_test(NAME simple_all COMMAND simple -f filename.txt -c 12 --flag --flag -d 1.2)
  20 +set_property(TEST simple_all PROPERTY PASS_REGULAR_EXPRESSION
  21 + "Working on file: filename.txt, direct count: 1, opt count: 1"
  22 + "Working on count: 12, direct count: 1, opt count: 1"
  23 + "Received flag: 2 (2) times"
  24 + "Some value: 1.2")
  25 +
  26 +
18 add_cli_exe(subcommands subcommands.cpp) 27 add_cli_exe(subcommands subcommands.cpp)
  28 +add_test(NAME subcommands_none COMMAND subcommands)
  29 +set_property(TEST subcommands_none PROPERTY
  30 + PASS_REGULAR_EXPRESSION "A subcommand is required")
  31 +add_test(NAME subcommands_all COMMAND subcommands --random start --file name stop --count)
  32 +set_property(TEST subcommands_all PROPERTY PASS_REGULAR_EXPRESSION
  33 + "Working on --file from start: name"
  34 + "Working on --count from stop: 1, direct count: 1"
  35 + "Count of --random flag: 1"
  36 + "Subcommand: start"
  37 + "Subcommand: stop")
  38 +
19 add_cli_exe(groups groups.cpp) 39 add_cli_exe(groups groups.cpp)
  40 +add_test(NAME groups_none COMMAND groups)
  41 +set_property(TEST groups_none PROPERTY PASS_REGULAR_EXPRESSION
  42 + "This is a timer:"
  43 + "--file is required"
  44 + "Run with --help for more information.")
  45 +add_test(NAME groups_all COMMAND groups --file this --count --count -d 1.2)
  46 +set_property(TEST groups_all PROPERTY PASS_REGULAR_EXPRESSION
  47 + "This is a timer:"
  48 + "Working on file: this, direct count: 1, opt count: 1"
  49 + "Working on count: 2, direct count: 2, opt count: 2"
  50 + "Some value: 1.2")
  51 +
20 add_cli_exe(inter_argument_order inter_argument_order.cpp) 52 add_cli_exe(inter_argument_order inter_argument_order.cpp)
  53 +add_test(NAME inter_argument_order COMMAND inter_argument_order --foo 1 2 3 --x --bar 4 5 6 --z --foo 7 8)
  54 +set_property(TEST inter_argument_order PROPERTY PASS_REGULAR_EXPRESSION
  55 + [=[foo : 1
  56 +foo : 2
  57 +foo : 3
  58 +bar : 4
  59 +bar : 5
  60 +bar : 6
  61 +foo : 7
  62 +foo : 8]=])
  63 +
21 add_cli_exe(prefix_command prefix_command.cpp) 64 add_cli_exe(prefix_command prefix_command.cpp)
  65 +add_test(NAME prefix_command COMMAND prefix_command -v 3 2 1 -- other one two 3)
  66 +set_property(TEST prefix_command PROPERTY PASS_REGULAR_EXPRESSION
  67 + "Prefix: 3 : 2 : 1"
  68 + "Remaining commands: -- other one two 3")
  69 +
22 add_cli_exe(enum enum.cpp) 70 add_cli_exe(enum enum.cpp)
  71 +add_test(NAME enum_pass COMMAND enum -l 1)
  72 +add_test(NAME enum_fail COMMAND enum -l 4)
  73 +set_property(TEST enum_fail PROPERTY PASS_REGULAR_EXPRESSION
  74 + "Could not convert: -l,--level = 4")
  75 +
23 add_cli_exe(modhelp modhelp.cpp) 76 add_cli_exe(modhelp modhelp.cpp)
  77 +add_test(NAME modhelp COMMAND modhelp -a test -h)
  78 +set_property(TEST modhelp PROPERTY PASS_REGULAR_EXPRESSION
  79 + "Option -a string in help: test")
24 80
25 add_subdirectory(subcom_in_files) 81 add_subdirectory(subcom_in_files)
  82 +add_test(NAME subcom_in_files COMMAND subcommand_main subcommand_a -f this.txt --with-foo)
  83 +set_property(TEST subcom_in_files PROPERTY PASS_REGULAR_EXPRESSION
  84 + "Working on file: this\.txt"
  85 + "Using foo!")
examples/inter_argument_order.cpp
@@ -5,15 +5,15 @@ @@ -5,15 +5,15 @@
5 #include <algorithm> 5 #include <algorithm>
6 6
7 int main(int argc, char **argv) { 7 int main(int argc, char **argv) {
8 - CLI::App app; 8 + CLI::App app{"An app to practice mixing unlimited arguments, but still recover the original order."};
9 9
10 std::vector<int> foos; 10 std::vector<int> foos;
11 - auto foo = app.add_option("--foo,-f", foos); 11 + auto foo = app.add_option("--foo,-f", foos, "Some unlimited argument");
12 12
13 std::vector<int> bars; 13 std::vector<int> bars;
14 - auto bar = app.add_option("--bar", bars); 14 + auto bar = app.add_option("--bar", bars, "Some unlimited arggument");
15 15
16 - app.add_flag("--z,--x"); // Random other flags 16 + app.add_flag("--z,--x", "Random other flags");
17 17
18 // Standard parsing lines (copy and paste in, or use CLI11_PARSE) 18 // Standard parsing lines (copy and paste in, or use CLI11_PARSE)
19 try { 19 try {
@@ -22,7 +22,7 @@ int main(int argc, char **argv) { @@ -22,7 +22,7 @@ int main(int argc, char **argv) {
22 return app.exit(e); 22 return app.exit(e);
23 } 23 }
24 24
25 - // I perfer using the back and popping 25 + // I prefer using the back and popping
26 std::reverse(std::begin(foos), std::end(foos)); 26 std::reverse(std::begin(foos), std::end(foos));
27 std::reverse(std::begin(bars), std::end(bars)); 27 std::reverse(std::begin(bars), std::end(bars));
28 28
examples/modhelp.cpp
1 -// Modify the help print so that argument values are accessible  
2 -// Note that this will not shortcut `->required` and other similar options  
3 1
4 #include "CLI/CLI.hpp" 2 #include "CLI/CLI.hpp"
5 3
6 #include <iostream> 4 #include <iostream>
7 5
8 int main(int argc, char **argv) { 6 int main(int argc, char **argv) {
9 - CLI::App test; 7 + CLI::App test{R"raw(Modify the help print so that argument values are accessible.
  8 +Note that this will not shortcut `->required` and other similar options.)raw"};
10 9
11 // Remove help flag because it shortcuts all processing 10 // Remove help flag because it shortcuts all processing
12 test.set_help_flag(); 11 test.set_help_flag();
@@ -22,10 +21,10 @@ int main(int argc, char **argv) { @@ -22,10 +21,10 @@ int main(int argc, char **argv) {
22 if(*help) 21 if(*help)
23 throw CLI::CallForHelp(); 22 throw CLI::CallForHelp();
24 } catch(const CLI::Error &e) { 23 } catch(const CLI::Error &e) {
25 - std::cout << "Option string:" << some_option << std::endl; 24 + std::cout << "Option -a string in help: " << some_option << std::endl;
26 return test.exit(e); 25 return test.exit(e);
27 } 26 }
28 27
29 - std::cout << "Option string:" << some_option << std::endl; 28 + std::cout << "Option -a string: " << some_option << std::endl;
30 return 0; 29 return 0;
31 } 30 }
examples/prefix_command.cpp
@@ -6,20 +6,18 @@ int main(int argc, char **argv) { @@ -6,20 +6,18 @@ int main(int argc, char **argv) {
6 app.prefix_command(); 6 app.prefix_command();
7 7
8 std::vector<int> vals; 8 std::vector<int> vals;
9 - app.add_option("--vals,-v", vals)->expected(1); 9 + app.add_option("--vals,-v", vals)->expected(-1);
10 10
11 CLI11_PARSE(app, argc, argv); 11 CLI11_PARSE(app, argc, argv);
12 12
13 std::vector<std::string> more_comms = app.remaining(); 13 std::vector<std::string> more_comms = app.remaining();
14 14
15 - std::cout << "Prefix:"; 15 + std::cout << "Prefix";
16 for(int v : vals) 16 for(int v : vals)
17 - std::cout << v << ":"; 17 + std::cout << ": " << v << " ";
18 18
19 std::cout << std::endl << "Remaining commands: "; 19 std::cout << std::endl << "Remaining commands: ";
20 20
21 - // Perfer to loop over from beginning, not "pop" order  
22 - std::reverse(std::begin(more_comms), std::end(more_comms));  
23 for(auto com : more_comms) 21 for(auto com : more_comms)
24 std::cout << com << " "; 22 std::cout << com << " ";
25 std::cout << std::endl; 23 std::cout << std::endl;
examples/simple.cpp
@@ -22,7 +22,7 @@ int main(int argc, char **argv) { @@ -22,7 +22,7 @@ int main(int argc, char **argv) {
22 << ", opt count: " << opt->count() << std::endl; 22 << ", opt count: " << opt->count() << std::endl;
23 std::cout << "Working on count: " << count << ", direct count: " << app.count("--count") 23 std::cout << "Working on count: " << count << ", direct count: " << app.count("--count")
24 << ", opt count: " << copt->count() << std::endl; 24 << ", opt count: " << copt->count() << std::endl;
25 - std::cout << "Recieved flag: " << v << " (" << flag->count() << ") times\n"; 25 + std::cout << "Received flag: " << v << " (" << flag->count() << ") times\n";
26 std::cout << "Some value: " << value << std::endl; 26 std::cout << "Some value: " << value << std::endl;
27 27
28 return 0; 28 return 0;
examples/subcom_in_files/CMakeLists.txt
1 -add_cli_exe(main main.cpp subcommand_a.cpp subcommand_a.hpp) 1 +add_cli_exe(subcommand_main subcommand_main.cpp subcommand_a.cpp subcommand_a.hpp)
examples/subcom_in_files/main.cpp renamed to examples/subcom_in_files/subcommand_main.cpp
examples/subcommands.cpp
@@ -6,6 +6,7 @@ int main(int argc, char **argv) { @@ -6,6 +6,7 @@ int main(int argc, char **argv) {
6 app.add_flag("--random", "Some random flag"); 6 app.add_flag("--random", "Some random flag");
7 CLI::App *start = app.add_subcommand("start", "A great subcommand"); 7 CLI::App *start = app.add_subcommand("start", "A great subcommand");
8 CLI::App *stop = app.add_subcommand("stop", "Do you really want to stop?"); 8 CLI::App *stop = app.add_subcommand("stop", "Do you really want to stop?");
  9 + app.require_subcommand(); // 1 or more
9 10
10 std::string file; 11 std::string file;
11 start->add_option("-f,--file", file, "File name"); 12 start->add_option("-f,--file", file, "File name");
@@ -14,10 +15,12 @@ int main(int argc, char **argv) { @@ -14,10 +15,12 @@ int main(int argc, char **argv) {
14 15
15 CLI11_PARSE(app, argc, argv); 16 CLI11_PARSE(app, argc, argv);
16 17
17 - std::cout << "Working on file: " << file << ", direct count: " << start->count("--file") << std::endl;  
18 - std::cout << "Working on count: " << s->count() << ", direct count: " << stop->count("--count") << std::endl; 18 + std::cout << "Working on --file from start: " << file << std::endl;
  19 + std::cout << "Working on --count from stop: " << s->count() << ", direct count: " << stop->count("--count")
  20 + << std::endl;
  21 + std::cout << "Count of --random flag: " << app.count("--random") << std::endl;
19 for(auto subcom : app.get_subcommands()) 22 for(auto subcom : app.get_subcommands())
20 - std::cout << "Subcommand:" << subcom->get_name() << std::endl; 23 + std::cout << "Subcommand: " << subcom->get_name() << std::endl;
21 24
22 return 0; 25 return 0;
23 } 26 }