Skip to content

Commit 796cb2e

Browse files
committed
Add demo_program_options
1 parent 415cee0 commit 796cb2e

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ target_link_libraries(program_options PUBLIC
2828
)
2929

3030

31+
## Executables
32+
33+
add_executable(demo_program_options src/demo_program_options.cpp)
34+
target_link_libraries(demo_program_options PUBLIC
35+
program_options
36+
)
37+
3138
## Tests
3239

3340
include(CTest)
@@ -55,6 +62,8 @@ install(DIRECTORY include/${PROJECT_NAME}/
5562
install(
5663
TARGETS
5764
program_options
65+
demo_program_options
66+
5867
EXPORT ${PROJECT_NAME}Targets
5968
)
6069

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ Utility package providing tools for CLI applications.
44

55
## ProgramOptions
66

7-
TODO: Add usage example of the ProgramOptions class (and how to link in cmake)
7+
`ProgramOptions` is a wrapper around
8+
[Boost.Program_options](https://www.boost.org/doc/libs/1_80_0/doc/html/program_options.html)
9+
to make it more convenient to use. For an example see
10+
`src/demo_program_options.cpp`.
811

912

1013
## Links
1114

12-
- [Documentation](https://open-dynamic-robot-initiative.github.io/cli_utils/)
1315
- [Code on GitHub](https://github.com/open-dynamic-robot-initiative/cli_utils)

src/demo_program_options.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)