Commit b2376bd3590074c3109d27f69779ea6e45ddf221
1 parent
fc69345f
Adding example of completely custom help option
Showing
2 changed files
with
32 additions
and
0 deletions
examples/CMakeLists.txt
examples/modhelp.cpp
0 → 100644
| 1 | +// Modify the help print so that argument values are accessible | |
| 2 | +// Note that this will not shortcut `->required` and other similar options | |
| 3 | + | |
| 4 | +#include "CLI/CLI.hpp" | |
| 5 | + | |
| 6 | +#include <iostream> | |
| 7 | + | |
| 8 | +int main(int argc, char **argv) { | |
| 9 | + CLI::App test; | |
| 10 | + | |
| 11 | + // Remove help flag because it shortcuts all processing | |
| 12 | + test.set_help_flag(); | |
| 13 | + | |
| 14 | + // Add custom flag that activates help | |
| 15 | + auto help = test.add_flag("-h,--help", "Request help"); | |
| 16 | + | |
| 17 | + std::string some_option; | |
| 18 | + test.add_option("-a", some_option, "Some description"); | |
| 19 | + | |
| 20 | + try { | |
| 21 | + test.parse(argc, argv); | |
| 22 | + if(*help) | |
| 23 | + throw CLI::CallForHelp(); | |
| 24 | + } catch (const CLI::Error &e) { | |
| 25 | + std::cout << "Option string:" << some_option << std::endl; | |
| 26 | + return test.exit(e); | |
| 27 | + } | |
| 28 | + | |
| 29 | + std::cout << "Option string:" << some_option << std::endl; | |
| 30 | + return 0; | |
| 31 | +} | ... | ... |