Skip to content
Open
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
17 changes: 17 additions & 0 deletions tests/module_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,21 @@ if(CLI11_MODULES)
add_test(NAME module-test1 COMMAND module-test one)
set_property(TEST module-test1 PROPERTY PASS_REGULAR_EXPRESSION
"OK: import cli11 module\nvalue = one")

# Long options through a subcommand (see #1318)
add_test(NAME module-test-long-opts COMMAND module-test one configure --script-path foo
--build-dir bar)
set_property(
TEST module-test-long-opts
PROPERTY PASS_REGULAR_EXPRESSION "subcommand = configure\nscript_path = foo\nbuild_dir = bar")

# Short options through a subcommand
add_test(NAME module-test-short-opts COMMAND module-test one install -B bld -I prefix)
set_property(
TEST module-test-short-opts
PROPERTY PASS_REGULAR_EXPRESSION "subcommand = install\nbuild_dir = bld\ninstall_dir = prefix")

# Long option in an option group
add_test(NAME module-test-option-group COMMAND module-test one --output json)
set_property(TEST module-test-option-group PROPERTY PASS_REGULAR_EXPRESSION "output = json")
endif()
41 changes: 41 additions & 0 deletions tests/module_test/module_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,33 @@ using CLI::App;
using CLI::ParseError;

int main(int argc, char *argv[]) {
std::string script_path{};
std::string toolchain_path{};
std::string build_dir{};
std::string install_dir{};
std::string output{};
App app{"Module test"};

string value;
app.add_option("value", value, "A test value")->required();
// test with long and short options and subcommands
auto config = app.add_subcommand("configure", "Configure the project");
config->add_option("-S,--script-path", script_path, "Path to build script");
config->add_option("-T,--toolchain-path", toolchain_path, "Compiler toolchain to use");
config->add_option("-B,--build-dir", build_dir, "Build directory");
// Build Subcommand
auto build = app.add_subcommand("build", "Execute build targets");
build->add_option("-B,--build-dir", build_dir, "Build directory");
// Install Subcommand
auto install = app.add_subcommand("install", "Install build artifacts");
install->add_option("-B,--build-dir", build_dir, "Source build directory");
install->add_option("-I,--install-dir", install_dir, "Installation prefix");
// Scan Subcommand
auto scan = app.add_subcommand("scan", "Scan for source changes/dependencies");
scan->add_option("-B,--build-dir", build_dir, "Build directory");
// test with option group
auto group = app.add_option_group("output group", "type of output");
group->add_option("-o,--output", output, "output type");

try {
app.parse(argc, argv);
Expand All @@ -27,6 +50,24 @@ int main(int argc, char *argv[]) {
}

std::println("OK: import cli11 module\nvalue = {}", value);
for(const auto *sub : app.get_subcommands()) {
std::println("subcommand = {}", sub->get_name());
}
if(!script_path.empty()) {
std::println("script_path = {}", script_path);
}
if(!toolchain_path.empty()) {
std::println("toolchain_path = {}", toolchain_path);
}
if(!build_dir.empty()) {
std::println("build_dir = {}", build_dir);
}
if(!install_dir.empty()) {
std::println("install_dir = {}", install_dir);
}
if(!output.empty()) {
std::println("output = {}", output);
}

return 0;
}
Loading