Commit 6ed2899741d3d24914c19700b8c6cda0a0c66cbb

Authored by henryiii
1 parent f6b1bc13

Virtual options list, overridable help flag.

Showing 1 changed file with 11 additions and 6 deletions
include/CLI/App.hpp
@@ -36,12 +36,13 @@ protected: @@ -36,12 +36,13 @@ protected:
36 std::string name; 36 std::string name;
37 std::string prog_description; 37 std::string prog_description;
38 std::vector<Option> options; 38 std::vector<Option> options;
  39 + Option* help_flag {nullptr};
39 std::vector<std::string> missing_options; 40 std::vector<std::string> missing_options;
40 std::deque<std::string> positionals; 41 std::deque<std::string> positionals;
41 std::vector<std::unique_ptr<App>> subcommands; 42 std::vector<std::unique_ptr<App>> subcommands;
42 bool parsed{false}; 43 bool parsed{false};
43 - App* subcommand = nullptr;  
44 - std::string progname = "program"; 44 + App* subcommand{nullptr};
  45 + std::string progname{"program"};
45 46
46 std::function<void()> app_callback; 47 std::function<void()> app_callback;
47 48
@@ -51,9 +52,8 @@ public: @@ -51,9 +52,8 @@ public:
51 /// it is not possible to overload on std::function (fixed in c++14 52 /// it is not possible to overload on std::function (fixed in c++14
52 /// and backported to c++11 on newer compilers). Use capture by reference 53 /// and backported to c++11 on newer compilers). Use capture by reference
53 /// to get a pointer to App if needed. 54 /// to get a pointer to App if needed.
54 - App* set_callback(std::function<void()> callback) { 55 + void set_callback(std::function<void()> callback) {
55 app_callback = callback; 56 app_callback = callback;
56 - return this;  
57 } 57 }
58 58
59 void run_callback() { 59 void run_callback() {
@@ -79,10 +79,15 @@ public: @@ -79,10 +79,15 @@ public:
79 App(std::string prog_description="") 79 App(std::string prog_description="")
80 : prog_description(prog_description) { 80 : prog_description(prog_description) {
81 81
82 - add_flag("-h,--help", "Print this help message and exit"); 82 + setup();
83 83
84 } 84 }
85 85
  86 + /// Setup help flag. Virtual to allow customization.
  87 + virtual void setup() {
  88 + help_flag = add_flag("-h,--help", "Print this help message and exit");
  89 + }
  90 +
86 App* add_subcommand(std::string name, std::string description="") { 91 App* add_subcommand(std::string name, std::string description="") {
87 subcommands.emplace_back(new App(description)); 92 subcommands.emplace_back(new App(description));
88 subcommands.back()->name = name; 93 subcommands.back()->name = name;
@@ -317,7 +322,7 @@ public: @@ -317,7 +322,7 @@ public:
317 } 322 }
318 } 323 }
319 324
320 - if (count("--help") > 0) { 325 + if (help_flag != nullptr && help_flag->count() > 0) {
321 throw CallForHelp(); 326 throw CallForHelp();
322 } 327 }
323 328