mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 20:23:55 +00:00
Adding examples to tests
This commit is contained in:
parent
0959430e57
commit
232c792bae
@ -15,11 +15,71 @@ function(add_cli_exe T)
|
|||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
add_cli_exe(simple simple.cpp)
|
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_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_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_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_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_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_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_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!")
|
||||||
|
@ -5,15 +5,15 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
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;
|
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;
|
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)
|
// Standard parsing lines (copy and paste in, or use CLI11_PARSE)
|
||||||
try {
|
try {
|
||||||
@ -22,7 +22,7 @@ int main(int argc, char **argv) {
|
|||||||
return app.exit(e);
|
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(foos), std::end(foos));
|
||||||
std::reverse(std::begin(bars), std::end(bars));
|
std::reverse(std::begin(bars), std::end(bars));
|
||||||
|
|
||||||
|
@ -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 "CLI/CLI.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
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
|
// Remove help flag because it shortcuts all processing
|
||||||
test.set_help_flag();
|
test.set_help_flag();
|
||||||
@ -22,10 +21,10 @@ int main(int argc, char **argv) {
|
|||||||
if(*help)
|
if(*help)
|
||||||
throw CLI::CallForHelp();
|
throw CLI::CallForHelp();
|
||||||
} catch(const CLI::Error &e) {
|
} 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);
|
return test.exit(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Option string:" << some_option << std::endl;
|
std::cout << "Option -a string: " << some_option << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -6,20 +6,18 @@ int main(int argc, char **argv) {
|
|||||||
app.prefix_command();
|
app.prefix_command();
|
||||||
|
|
||||||
std::vector<int> vals;
|
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);
|
CLI11_PARSE(app, argc, argv);
|
||||||
|
|
||||||
std::vector<std::string> more_comms = app.remaining();
|
std::vector<std::string> more_comms = app.remaining();
|
||||||
|
|
||||||
std::cout << "Prefix:";
|
std::cout << "Prefix";
|
||||||
for(int v : vals)
|
for(int v : vals)
|
||||||
std::cout << v << ":";
|
std::cout << ": " << v << " ";
|
||||||
|
|
||||||
std::cout << std::endl << "Remaining commands: ";
|
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)
|
for(auto com : more_comms)
|
||||||
std::cout << com << " ";
|
std::cout << com << " ";
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
@ -22,7 +22,7 @@ int main(int argc, char **argv) {
|
|||||||
<< ", opt count: " << opt->count() << std::endl;
|
<< ", opt count: " << opt->count() << std::endl;
|
||||||
std::cout << "Working on count: " << count << ", direct count: " << app.count("--count")
|
std::cout << "Working on count: " << count << ", direct count: " << app.count("--count")
|
||||||
<< ", opt count: " << copt->count() << std::endl;
|
<< ", 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;
|
std::cout << "Some value: " << value << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -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)
|
||||||
|
@ -6,6 +6,7 @@ int main(int argc, char **argv) {
|
|||||||
app.add_flag("--random", "Some random flag");
|
app.add_flag("--random", "Some random flag");
|
||||||
CLI::App *start = app.add_subcommand("start", "A great subcommand");
|
CLI::App *start = app.add_subcommand("start", "A great subcommand");
|
||||||
CLI::App *stop = app.add_subcommand("stop", "Do you really want to stop?");
|
CLI::App *stop = app.add_subcommand("stop", "Do you really want to stop?");
|
||||||
|
app.require_subcommand(); // 1 or more
|
||||||
|
|
||||||
std::string file;
|
std::string file;
|
||||||
start->add_option("-f,--file", file, "File name");
|
start->add_option("-f,--file", file, "File name");
|
||||||
@ -14,10 +15,12 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
CLI11_PARSE(app, argc, argv);
|
CLI11_PARSE(app, argc, argv);
|
||||||
|
|
||||||
std::cout << "Working on file: " << file << ", direct count: " << start->count("--file") << std::endl;
|
std::cout << "Working on --file from start: " << file << std::endl;
|
||||||
std::cout << "Working on count: " << s->count() << ", direct count: " << stop->count("--count") << 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())
|
for(auto subcom : app.get_subcommands())
|
||||||
std::cout << "Subcommand:" << subcom->get_name() << std::endl;
|
std::cout << "Subcommand: " << subcom->get_name() << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user