Commit 8e59df0590195d6fda7dd4fb74262ac2ea53c565

Authored by Henry Fredrick Schreiner
Committed by Henry Schreiner
1 parent 8e675ae7

Original version from @lczech

examples/CMakeLists.txt
@@ -20,3 +20,5 @@ add_cli_exe(groups groups.cpp) @@ -20,3 +20,5 @@ add_cli_exe(groups groups.cpp)
20 add_cli_exe(inter_argument_order inter_argument_order.cpp) 20 add_cli_exe(inter_argument_order inter_argument_order.cpp)
21 add_cli_exe(prefix_command prefix_command.cpp) 21 add_cli_exe(prefix_command prefix_command.cpp)
22 add_cli_exe(enum enum.cpp) 22 add_cli_exe(enum enum.cpp)
  23 +
  24 +add_subdirectory(subcom_in_files)
examples/subcom_in_files/CMakeLists.txt 0 → 100644
  1 +add_cli_exe(main main.cpp subcommand_a.cpp subcommand_a.hpp)
examples/subcom_in_files/main.cpp 0 → 100644
  1 +// ===================================================================
  2 +// main.cpp
  3 +// ===================================================================
  4 +
  5 +#include "subcommand_a.hpp"
  6 +
  7 +int main( int argc, char** argv ) {
  8 + CLI::App app{"..."};
  9 +
  10 + // Call the setup functions for the subcommands.
  11 + // They return their option structs, so that the variables to which the options bind
  12 + // are still alive when `app.parse()` is called.
  13 + auto subcommand_a_opt = setup_subcommand_a( app );
  14 +
  15 + // More setup if needed, i.e., other subcommands etc.
  16 +
  17 + CLI11_PARSE(app, argc, argv);
  18 +
  19 + return 0;
  20 +}
examples/subcom_in_files/subcommand_a.cpp 0 → 100644
  1 +// ===================================================================
  2 +// subcommand_a.cpp
  3 +// ===================================================================
  4 +
  5 +#include "subcommand_a.hpp"
  6 +
  7 +/// Set up a subcommand and return a unique ptr to a struct that holds all its options.
  8 +/// The variables of the struct are bound to the CLI options.
  9 +/// We use a unique ptr so that the addresses of the variables are stable for binding,
  10 +/// and return it to the caller (main), so that the object itself stays alive.
  11 +std::unique_ptr<SubcommandAOptions> setup_subcommand_a( CLI::App& app )
  12 +{
  13 + // Create the option and subcommand objects.
  14 + auto opt = std::unique_ptr<SubcommandAOptions>(new SubcommandAOptions());
  15 + auto sub = app.add_subcommand( "subcommand_a", "performs subcommand a", true );
  16 +
  17 + // Add options to sub, binding them to opt.
  18 + sub->add_option("-f,--file", opt->file, "File name");
  19 + sub->add_flag("--with-foo", opt->with_foo, "Counter");
  20 +
  21 + // Set the run function as callback to be called when this subcommand is issued.
  22 + sub->set_callback( [&]() {
  23 + run_subcommand_a( *opt );
  24 + });
  25 +
  26 + return opt;
  27 +}
  28 +
  29 +/// The function that runs our code.
  30 +/// This could also simply be in the callback lambda itself,
  31 +/// but having a separate function is cleaner.
  32 +void run_subcommand_a( SubcommandAOptions const& opt )
  33 +{
  34 + // Do stuff...
  35 + std::cout << "Working on file: " << opt.file << std::endl;
  36 + if( opt.with_foo ) {
  37 + std::cout << "Using foo!" << std::endl;
  38 + }
  39 +}
examples/subcom_in_files/subcommand_a.hpp 0 → 100644
  1 +// ===================================================================
  2 +// subcommand_a.hpp
  3 +// ===================================================================
  4 +
  5 +#include "CLI/CLI.hpp"
  6 +#include <memory>
  7 +#include <string>
  8 +
  9 +/// Collection of all options of Subcommand A.
  10 +struct SubcommandAOptions
  11 +{
  12 + std::string file;
  13 + bool with_foo;
  14 +};
  15 +
  16 +// Function declarations.
  17 +std::unique_ptr<SubcommandAOptions> setup_subcommand_a( CLI::App& app );
  18 +void run_subcommand_a( SubcommandAOptions const& opt );