Skip to content

Commit

Permalink
Adding examples to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Apr 9, 2018
1 parent 0959430 commit e766f00
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 20 deletions.
60 changes: 60 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
10 changes: 5 additions & 5 deletions examples/inter_argument_order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#include <algorithm>

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<int> foos;
auto foo = app.add_option("--foo,-f", foos);
auto foo = app.add_option("--foo,-f", foos, "Some unlimited argument");

std::vector<int> 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 {
Expand All @@ -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));

Expand Down
9 changes: 4 additions & 5 deletions examples/modhelp.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>

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();
Expand All @@ -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;
}
8 changes: 3 additions & 5 deletions examples/prefix_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ int main(int argc, char **argv) {
app.prefix_command();

std::vector<int> vals;
app.add_option("--vals,-v", vals)->expected(1);
app.add_option("--vals,-v", vals)->expected(-1);

CLI11_PARSE(app, argc, argv);

std::vector<std::string> 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;
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/subcom_in_files/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
8 changes: 5 additions & 3 deletions examples/subcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
}

0 comments on commit e766f00

Please sign in to comment.