|
15 | 15 | - [Example 2 (`fallback`, `was_set`, `string`, `multi`)](#example-2-fallback-was_set-string-multi)
|
16 | 16 | - [Example 3 (`description`, `callback`, unnamed parameter)](#example-3-description-callback-unnamed-parameter)
|
17 | 17 | - [Example 4 (more `callback`s, more `fallback`s, `f64`, `to_vector`)](#example-4-more-callbacks-more-fallbacks-f64-to_vector)
|
| 18 | + - [Example 5 (`bind`)](#example-5-bind) |
18 | 19 | - [Miscellaneous functions](#miscellaneous-functions)
|
19 | 20 | - [Defaults](#defaults)
|
20 | 21 | - [Flags](#flags)
|
@@ -104,6 +105,7 @@ optimization level set to 255
|
104 | 105 | $ ./optimization -O3 --optimization 1e2
|
105 | 106 | optimization level set to 100
|
106 | 107 | ```
|
| 108 | +
|
107 | 109 | ### Example 2 (`fallback`, `was_set`, `string`, `multi`)
|
108 | 110 | Let's expand on the previous code. We want it to assume a certain value for the option `optimization` even if the user sets none. This can be achieved through the `.fallback(...)` method. After parsing, the method `.was_set()` tells us whether the option was actually set by the user or fell back on the default value.
|
109 | 111 |
|
@@ -230,6 +232,7 @@ optimization level (manual) = 3
|
230 | 232 | include paths (1):
|
231 | 233 | ./include
|
232 | 234 | ```
|
| 235 | +
|
233 | 236 | ### Example 4 (more `callback`s, more `fallback`s, `f64`, `to_vector`)
|
234 | 237 | In this example, we will employ already known mechanics but lay the focus on their versatility.
|
235 | 238 |
|
@@ -303,6 +306,65 @@ successfully parsed 12 which equals 12
|
303 | 306 | successfully parsed NaN which equals nan
|
304 | 307 | ( + 12 nan ) = nan
|
305 | 308 | ```
|
| 309 | + |
| 310 | +### Example 5 (`bind`) |
| 311 | +Until now, we defined the type, plurality and fallback value of each option manually by invoking `.type`, `.single`/`.multi` and `.fallback`. If we just want to extract the values from the parser and store them in a variable, `.bind` offers a more convenient and safer way of achieving this and ought to be preferred. `.bind` internally sets the type and the plurality (`.single`/`.multi`) of the corresponding option (but not the fallback). |
| 312 | + |
| 313 | +We may bind options to variables of type `std::string`, to 32- or 64-bit signed integers, unsigned integer, floating point numbers or to STL containers consisting of elements of such type. If we want to use custom container types and/or custom insertion routines, we have to use `.bind_container( container&, inserter )` which accepts a binary function with the signature `inserter( container_t&, container_t::value_type const& )` (up to implicit conversion). |
| 314 | + |
| 315 | +```cpp |
| 316 | +#include <ProgramOptions.hxx> |
| 317 | +#include <cstdint> |
| 318 | +#include <vector> |
| 319 | +#include <string> |
| 320 | +#include <iostream> |
| 321 | +#include <string_view> |
| 322 | + |
| 323 | +int main( int argc, char** argv ) { |
| 324 | + po::parser parser; |
| 325 | + |
| 326 | + std::uint32_t optimization = 0; // the value we set here acts as an implicit fallback |
| 327 | + parser[ "optimization" ] |
| 328 | + .abbreviation( 'O' ) |
| 329 | + .description( "set the optimization level (default: -O0)" ) |
| 330 | + .bind( optimization ); // write the parsed value to the variable 'optimization' |
| 331 | + // .bind( optimization ) automatically calls .type( po::u32 ) and .single() |
| 332 | + |
| 333 | + std::vector< std::string > include_paths; |
| 334 | + parser[ "include-path" ] |
| 335 | + .abbreviation( 'I' ) |
| 336 | + .description( "add an include path" ) |
| 337 | + .bind( include_paths ); // append paths to the vector 'include_paths' |
| 338 | + |
| 339 | + parser[ "help" ] |
| 340 | + .abbreviation( '?' ) |
| 341 | + .description( "print this help screen" ); |
| 342 | + |
| 343 | + std::deque< std::string > files; |
| 344 | + parser[ "" ] |
| 345 | + .bind( files ); // append paths to the deque 'include_paths |
| 346 | + |
| 347 | + if( !parser( argc, argv ) ) |
| 348 | + return -1; |
| 349 | + |
| 350 | + // we don't want to print anything else if the help screen has been displayed |
| 351 | + if( parser[ "help" ].was_set() ) { |
| 352 | + std::cout << parser << '\n'; |
| 353 | + return 0; |
| 354 | + } |
| 355 | + |
| 356 | + // print the parsed values |
| 357 | + // note that we don't need to access parser anymore; all data is stored in the bound variables |
| 358 | + std::cout << "optimization level = " << optimization << '\n'; |
| 359 | + std::cout << "include files (" << files.size() << "):\n"; |
| 360 | + for( auto&& i : files ) |
| 361 | + std::cout << '\t' << i << '\n'; |
| 362 | + std::cout << "include paths (" << include_paths.size() << "):\n"; |
| 363 | + for( auto&& i : include_paths ) |
| 364 | + std::cout << '\t' << i << '\n'; |
| 365 | +} |
| 366 | +``` |
| 367 | +
|
306 | 368 | ### Miscellaneous functions
|
307 | 369 |
|
308 | 370 | #### `void po::parser::silent()`
|
|
0 commit comments