|
29 | 29 |
|
30 | 30 | using juzzlin::Argengine; |
31 | 31 |
|
| 32 | +// |
| 33 | +// Ex1: Demonstrate basic usage. |
| 34 | +// |
| 35 | + |
32 | 36 | int main(int argc, char ** argv) |
33 | 37 | { |
| 38 | + // Instantiate Argengine and give it the raw argument data. |
34 | 39 | Argengine ae(argc, argv); |
| 40 | + |
| 41 | + // Add option "-a" that, when set, prints all arguments given to the binary. |
35 | 42 | ae.addOption( |
36 | 43 | { "-a", "--arguments" }, [&] { |
37 | 44 | for (int i = 0; i < argc; i++) { |
38 | 45 | std::cout << argv[i] << std::endl; |
39 | 46 | } |
40 | 47 | }, |
41 | | - false, "Print arguments."); |
| 48 | + false, "Print arguments."); // Set the option non-required and set documentation string. |
| 49 | + |
| 50 | + // Add option "-p" that prints the length of the given string e.g. "-p FOO". |
42 | 51 | ae.addOption( |
43 | 52 | { "-p" }, [](std::string value) { |
44 | 53 | std::cout << value.size() << std::endl; |
45 | 54 | }, |
46 | | - true, "Print length of given text. This option is required.", "TEXT"); |
| 55 | + true, "Print length of given text. This option is required.", "TEXT"); // Set the option required and set documentation string and variable name shown in the documentation. |
47 | 56 |
|
| 57 | + // Parse the arguments and store possible error to variable error. |
48 | 58 | Argengine::Error error; |
49 | 59 | ae.parse(error); |
50 | 60 |
|
| 61 | + // Check error and print the possible error message. |
51 | 62 | if (error.code != Argengine::Error::Code::Ok) { |
52 | 63 | std::cerr << error.message << std::endl |
53 | 64 | << std::endl; |
54 | | - ae.printHelp(); |
| 65 | + ae.printHelp(); // Print the auto-generated help. |
55 | 66 | return EXIT_FAILURE; |
56 | 67 | } |
57 | 68 |
|
|
0 commit comments