diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 68124dbc..72a5f55b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -20,3 +20,5 @@ add_cli_exe(groups groups.cpp) add_cli_exe(inter_argument_order inter_argument_order.cpp) add_cli_exe(prefix_command prefix_command.cpp) add_cli_exe(enum enum.cpp) + +add_subdirectory(subcom_in_files) diff --git a/examples/subcom_in_files/CMakeLists.txt b/examples/subcom_in_files/CMakeLists.txt new file mode 100644 index 00000000..be283a6b --- /dev/null +++ b/examples/subcom_in_files/CMakeLists.txt @@ -0,0 +1 @@ +add_cli_exe(main main.cpp subcommand_a.cpp subcommand_a.hpp) diff --git a/examples/subcom_in_files/main.cpp b/examples/subcom_in_files/main.cpp new file mode 100644 index 00000000..4bbf191c --- /dev/null +++ b/examples/subcom_in_files/main.cpp @@ -0,0 +1,20 @@ +// =================================================================== +// main.cpp +// =================================================================== + +#include "subcommand_a.hpp" + +int main( int argc, char** argv ) { + CLI::App app{"..."}; + + // Call the setup functions for the subcommands. + // They return their option structs, so that the variables to which the options bind + // are still alive when `app.parse()` is called. + auto subcommand_a_opt = setup_subcommand_a( app ); + + // More setup if needed, i.e., other subcommands etc. + + CLI11_PARSE(app, argc, argv); + + return 0; +} diff --git a/examples/subcom_in_files/subcommand_a.cpp b/examples/subcom_in_files/subcommand_a.cpp new file mode 100644 index 00000000..d104ee34 --- /dev/null +++ b/examples/subcom_in_files/subcommand_a.cpp @@ -0,0 +1,39 @@ +// =================================================================== +// subcommand_a.cpp +// =================================================================== + +#include "subcommand_a.hpp" + +/// Set up a subcommand and return a unique ptr to a struct that holds all its options. +/// The variables of the struct are bound to the CLI options. +/// We use a unique ptr so that the addresses of the variables are stable for binding, +/// and return it to the caller (main), so that the object itself stays alive. +std::unique_ptr setup_subcommand_a( CLI::App& app ) +{ + // Create the option and subcommand objects. + auto opt = std::unique_ptr(new SubcommandAOptions()); + auto sub = app.add_subcommand( "subcommand_a", "performs subcommand a", true ); + + // Add options to sub, binding them to opt. + sub->add_option("-f,--file", opt->file, "File name"); + sub->add_flag("--with-foo", opt->with_foo, "Counter"); + + // Set the run function as callback to be called when this subcommand is issued. + sub->set_callback( [&]() { + run_subcommand_a( *opt ); + }); + + return opt; +} + +/// The function that runs our code. +/// This could also simply be in the callback lambda itself, +/// but having a separate function is cleaner. +void run_subcommand_a( SubcommandAOptions const& opt ) +{ + // Do stuff... + std::cout << "Working on file: " << opt.file << std::endl; + if( opt.with_foo ) { + std::cout << "Using foo!" << std::endl; + } +} diff --git a/examples/subcom_in_files/subcommand_a.hpp b/examples/subcom_in_files/subcommand_a.hpp new file mode 100644 index 00000000..afb4bee0 --- /dev/null +++ b/examples/subcom_in_files/subcommand_a.hpp @@ -0,0 +1,18 @@ +// =================================================================== +// subcommand_a.hpp +// =================================================================== + +#include "CLI/CLI.hpp" +#include +#include + +/// Collection of all options of Subcommand A. +struct SubcommandAOptions +{ + std::string file; + bool with_foo; +}; + +// Function declarations. +std::unique_ptr setup_subcommand_a( CLI::App& app ); +void run_subcommand_a( SubcommandAOptions const& opt );