|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Simple example on how to use ProgramOptions |
| 4 | + * @copyright 2022, Max Planck Gesellschaft. All rights reserved. |
| 5 | + * @license BSD 3-clause |
| 6 | + * |
| 7 | + * To use it, one needs to implement a custom class which is derived from |
| 8 | + * cli_utils::ProgramOptions. In this class, member variables for the desired |
| 9 | + * arguments are declared (potentially with default values), as well as methods |
| 10 | + * for providing a help text and to define the actual command line arguments. |
| 11 | + * |
| 12 | + * An instance of this custom class is then used to parse the command line |
| 13 | + * arguments and to access the values. |
| 14 | + * |
| 15 | + * See ``demo_program_options --help`` for the auto-generated help. |
| 16 | + */ |
| 17 | +#include <cli_utils/program_options.hpp> |
| 18 | + |
| 19 | +class Args : public cli_utils::ProgramOptions |
| 20 | +{ |
| 21 | +public: |
| 22 | + // declare a variable for each argument |
| 23 | + std::string message; |
| 24 | + int repetitions; |
| 25 | + int optional_arg = 42; |
| 26 | + bool flag; |
| 27 | + |
| 28 | + // help() should return the text shown when called with `--help` |
| 29 | + std::string help() const override |
| 30 | + { |
| 31 | + return R"(Simple demo on how to use cli_utils::ProgramOptions. |
| 32 | +
|
| 33 | + It prints a given message multiple times. |
| 34 | +
|
| 35 | +Usage: demo_program_options [options] <message> |
| 36 | +
|
| 37 | +)"; |
| 38 | + } |
| 39 | + |
| 40 | + // in add_options the arguments are defined |
| 41 | + void add_options(boost::program_options::options_description &options, |
| 42 | + boost::program_options::positional_options_description |
| 43 | + &positional) override |
| 44 | + { |
| 45 | + namespace po = boost::program_options; |
| 46 | + |
| 47 | + // The chaining of parentheses calls does not go well with clang-format, |
| 48 | + // so better disable auto-formatting for this block. |
| 49 | + |
| 50 | + // clang-format off |
| 51 | + options.add_options() |
| 52 | + ("message", |
| 53 | + po::value<std::string>(&message)->required(), |
| 54 | + "Message that is printed.") |
| 55 | + ("repetitions,n", // Can use either "--repetitions 5" or "-n 5" |
| 56 | + po::value<int>(&repetitions)->required(), |
| 57 | + "How often to print the message") |
| 58 | + ("optional", |
| 59 | + po::value<int>(&optional_arg), |
| 60 | + "This is an optional argument with default value.") |
| 61 | + ("flag", |
| 62 | + "A flag without value.") |
| 63 | + |
| 64 | + ; |
| 65 | + // clang-format on |
| 66 | + |
| 67 | + // mark 'message' as positional argument |
| 68 | + positional.add("message", 1); |
| 69 | + } |
| 70 | + |
| 71 | + // for boolean flags without values, some post-processing is needed |
| 72 | + void postprocess(const boost::program_options::variables_map &args) override |
| 73 | + { |
| 74 | + // check if --flag has been set |
| 75 | + flag = args.count("flag") > 0; |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +int main(int argc, char **argv) |
| 80 | +{ |
| 81 | + // read command line arguments |
| 82 | + Args args; |
| 83 | + if (!args.parse_args(argc, argv)) |
| 84 | + { |
| 85 | + return 1; |
| 86 | + } |
| 87 | + |
| 88 | + for (int i = 0; i < args.repetitions; ++i) |
| 89 | + { |
| 90 | + std::cout << args.message << std::endl; |
| 91 | + } |
| 92 | + |
| 93 | + std::cout << "\n\nValue of the optional argument: " << args.optional_arg |
| 94 | + << std::endl; |
| 95 | + if (args.flag) |
| 96 | + { |
| 97 | + std::cout << "--flag has been set" << std::endl; |
| 98 | + } |
| 99 | +} |
0 commit comments