mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-02 13:43:52 +00:00
Style updates
This commit is contained in:
parent
6d26440b6e
commit
8cdf1c8651
@ -4,13 +4,13 @@
|
|||||||
|
|
||||||
#include "subcommand_a.hpp"
|
#include "subcommand_a.hpp"
|
||||||
|
|
||||||
int main( int argc, char** argv ) {
|
int main(int argc, char **argv) {
|
||||||
CLI::App app{"..."};
|
CLI::App app{"..."};
|
||||||
|
|
||||||
// Call the setup functions for the subcommands.
|
// Call the setup functions for the subcommands.
|
||||||
// They are kept alive by a shared pointer in the
|
// They are kept alive by a shared pointer in the
|
||||||
// lambda function held by CLI11
|
// lambda function held by CLI11
|
||||||
setup_subcommand_a( app );
|
setup_subcommand_a(app);
|
||||||
|
|
||||||
// Make sure we get at least one subcommand
|
// Make sure we get at least one subcommand
|
||||||
app.require_subcommand();
|
app.require_subcommand();
|
||||||
|
@ -8,19 +8,17 @@
|
|||||||
/// The variables of the struct are bound to the CLI options.
|
/// The variables of the struct are bound to the CLI options.
|
||||||
/// We use a shared ptr so that the addresses of the variables remain for binding,
|
/// We use a shared ptr so that the addresses of the variables remain for binding,
|
||||||
/// You could return the shared pointer if you wanted to access the values in main.
|
/// You could return the shared pointer if you wanted to access the values in main.
|
||||||
void setup_subcommand_a( CLI::App& app ) {
|
void setup_subcommand_a(CLI::App &app) {
|
||||||
// Create the option and subcommand objects.
|
// Create the option and subcommand objects.
|
||||||
auto opt = std::make_shared<SubcommandAOptions>();
|
auto opt = std::make_shared<SubcommandAOptions>();
|
||||||
auto sub = app.add_subcommand( "subcommand_a", "performs subcommand a", true );
|
auto sub = app.add_subcommand("subcommand_a", "performs subcommand a", true);
|
||||||
|
|
||||||
// Add options to sub, binding them to opt.
|
// Add options to sub, binding them to opt.
|
||||||
sub->add_option("-f,--file", opt->file, "File name")->required();
|
sub->add_option("-f,--file", opt->file, "File name")->required();
|
||||||
sub->add_flag("--with-foo", opt->with_foo, "Counter");
|
sub->add_flag("--with-foo", opt->with_foo, "Counter");
|
||||||
|
|
||||||
// Set the run function as callback to be called when this subcommand is issued.
|
// Set the run function as callback to be called when this subcommand is issued.
|
||||||
sub->set_callback([opt]() {
|
sub->set_callback([opt]() { run_subcommand_a(*opt); });
|
||||||
run_subcommand_a( *opt );
|
|
||||||
});
|
|
||||||
|
|
||||||
// Note: In C++14, you could make a unique pointer, then pass it into the lambda function via
|
// Note: In C++14, you could make a unique pointer, then pass it into the lambda function via
|
||||||
// a move. That's slightly more elegant, but you won't be able to see the runtime difference
|
// a move. That's slightly more elegant, but you won't be able to see the runtime difference
|
||||||
@ -30,11 +28,10 @@ void setup_subcommand_a( CLI::App& app ) {
|
|||||||
/// The function that runs our code.
|
/// The function that runs our code.
|
||||||
/// This could also simply be in the callback lambda itself,
|
/// This could also simply be in the callback lambda itself,
|
||||||
/// but having a separate function is cleaner.
|
/// but having a separate function is cleaner.
|
||||||
void run_subcommand_a( SubcommandAOptions const& opt )
|
void run_subcommand_a(SubcommandAOptions const &opt) {
|
||||||
{
|
|
||||||
// Do stuff...
|
// Do stuff...
|
||||||
std::cout << "Working on file: " << opt.file << std::endl;
|
std::cout << "Working on file: " << opt.file << std::endl;
|
||||||
if( opt.with_foo ) {
|
if(opt.with_foo) {
|
||||||
std::cout << "Using foo!" << std::endl;
|
std::cout << "Using foo!" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,5 +16,5 @@ struct SubcommandAOptions {
|
|||||||
// is just done this way to be nicely organized
|
// is just done this way to be nicely organized
|
||||||
|
|
||||||
// Function declarations.
|
// Function declarations.
|
||||||
void setup_subcommand_a( CLI::App& app );
|
void setup_subcommand_a(CLI::App &app);
|
||||||
void run_subcommand_a( SubcommandAOptions const& opt );
|
void run_subcommand_a(SubcommandAOptions const &opt);
|
||||||
|
@ -45,8 +45,8 @@ using App_p = std::unique_ptr<App>;
|
|||||||
|
|
||||||
/// Creates a command line program, with very few defaults.
|
/// Creates a command line program, with very few defaults.
|
||||||
/** To use, create a new `Program()` instance with `argc`, `argv`, and a help description. The templated
|
/** To use, create a new `Program()` instance with `argc`, `argv`, and a help description. The templated
|
||||||
* add_option methods make it easy to prepare options. Remember to call `.start` before starting your
|
* add_option methods make it easy to prepare options. Remember to call `.start` before starting your
|
||||||
* program, so that the options can be evaluated and the help option doesn't accidentally run your program. */
|
* program, so that the options can be evaluated and the help option doesn't accidentally run your program. */
|
||||||
class App {
|
class App {
|
||||||
friend Option;
|
friend Option;
|
||||||
friend detail::AppFriend;
|
friend detail::AppFriend;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user