|
| 1 | +#include <ProgramOptions.hxx> |
| 2 | +#include <cstdint> |
| 3 | +#include <vector> |
| 4 | +#include <string> |
| 5 | +#include <iostream> |
| 6 | +#include <string_view> |
| 7 | + |
| 8 | +int main( int argc, char** argv ) { |
| 9 | + po::parser parser; |
| 10 | + |
| 11 | + std::uint32_t optimization = 0; // the value we set here acts as an implicit fallback |
| 12 | + parser[ "optimization" ] // corresponds to --optimization |
| 13 | + .abbreviation( 'O' ) // corresponds to -O |
| 14 | + .description( "set the optimization level (default: -O0)" ) |
| 15 | + .bind( optimization ); // write the parsed value to the variable 'optimization' |
| 16 | + // .bind( optimization ) automatically calls .type( po::u32 ) and .single() |
| 17 | + |
| 18 | + std::vector< std::string > include_paths; |
| 19 | + parser[ "include-path" ] // corresponds to --include-path |
| 20 | + .abbreviation( 'I' ) // corresponds to -I |
| 21 | + .description( "add an include path" ) |
| 22 | + .bind( include_paths ); // append paths to the vector 'include_paths' |
| 23 | + |
| 24 | + parser[ "help" ] // corresponds to --help |
| 25 | + .abbreviation( '?' ) // corresponds to -? |
| 26 | + .description( "print this help screen" ); |
| 27 | + |
| 28 | + std::deque< std::string > files; |
| 29 | + parser[ "" ] // the unnamed parameter is used for non-option arguments, i.e. gcc a.c b.c |
| 30 | + .bind( files ); // append paths to the deque 'include_paths |
| 31 | + |
| 32 | + // parsing returns false if at least one error has occurred |
| 33 | + if( !parser( argc, argv ) ) |
| 34 | + return -1; |
| 35 | + |
| 36 | + // we don't want to print anything else if the help screen has been displayed |
| 37 | + if( parser[ "help" ].was_set() ) { |
| 38 | + std::cout << parser << '\n'; |
| 39 | + return 0; |
| 40 | + } |
| 41 | + |
| 42 | + // print the parsed values |
| 43 | + // note that we don't need to access parser anymore; all data is stored in the bound variables |
| 44 | + std::cout << "optimization level = " << optimization << '\n'; |
| 45 | + |
| 46 | + std::cout << "include files (" << files.size() << "):\n"; |
| 47 | + for( auto&& i : files ) |
| 48 | + std::cout << '\t' << i << '\n'; |
| 49 | + |
| 50 | + std::cout << "include paths (" << include_paths.size() << "):\n"; |
| 51 | + for( auto&& i : include_paths ) |
| 52 | + std::cout << '\t' << i << '\n'; |
| 53 | +} |
0 commit comments