Commit 5019f1030b1676eed24a4c07fb066bbd2082dfa7

Authored by Henry Fredrick Schreiner
1 parent 4067f55c

Adding more standard method for exiting

examples/try.cpp
@@ -11,9 +11,11 @@ int main (int argc, char** argv) { @@ -11,9 +11,11 @@ int main (int argc, char** argv) {
11 int count; 11 int count;
12 app.add_flag("c,count", count, "File name"); 12 app.add_flag("c,count", count, "File name");
13 13
14 - CLI::Return ret = app.start(argc, argv);  
15 - if(ret != CLI::Return::Continue)  
16 - return (int) ret; 14 + try {
  15 + app.run(argc, argv);
  16 + } catch (const CLI::Error &e) {
  17 + return app.exit(e);
  18 + }
17 19
18 std::cout << "Working on file: " << file << ", direct count: " << app.count("file") << std::endl; 20 std::cout << "Working on file: " << file << ", direct count: " << app.count("file") << std::endl;
19 std::cout << "Working on count: " << count << ", direct count: " << app.count("count") << std::endl; 21 std::cout << "Working on count: " << count << ", direct count: " << app.count("count") << std::endl;
examples/try1.cpp
@@ -14,9 +14,11 @@ int main (int argc, char** argv) { @@ -14,9 +14,11 @@ int main (int argc, char** argv) {
14 int count; 14 int count;
15 stop->add_flag("c,count", count, "File name"); 15 stop->add_flag("c,count", count, "File name");
16 16
17 - CLI::Return ret = app.start(argc, argv);  
18 - if(ret != CLI::Return::Continue)  
19 - return (int) ret; 17 + try {
  18 + app.run(argc, argv);
  19 + } catch (const CLI::Error &e) {
  20 + return app.exit(e);
  21 + }
20 22
21 std::cout << "Working on file: " << file << ", direct count: " << start->count("file") << std::endl; 23 std::cout << "Working on file: " << file << ", direct count: " << start->count("file") << std::endl;
22 std::cout << "Working on count: " << count << ", direct count: " << stop->count("count") << std::endl; 24 std::cout << "Working on count: " << count << ", direct count: " << stop->count("count") << std::endl;
include/CLI.hpp
@@ -62,15 +62,6 @@ using std::enable_if_t; @@ -62,15 +62,6 @@ using std::enable_if_t;
62 #endif 62 #endif
63 // If your compiler supports C++14, you can use that definition instead 63 // If your compiler supports C++14, you can use that definition instead
64 64
65 -enum class Return {  
66 - Continue = -1,  
67 - Success = 0,  
68 - ParseError = 1,  
69 - PositionalError = 2,  
70 - RequiredError = 3,  
71 - GeneralError = 4  
72 -};  
73 -  
74 struct Combiner { 65 struct Combiner {
75 int num; 66 int num;
76 bool positional; 67 bool positional;
@@ -129,42 +120,44 @@ bool _NonexistentPath(std::string filename) { @@ -129,42 +120,44 @@ bool _NonexistentPath(std::string filename) {
129 } 120 }
130 121
131 struct Error : public std::runtime_error { 122 struct Error : public std::runtime_error {
132 - Error(std::string parent, std::string name) : runtime_error(parent + ": " + name) {} 123 + int exit_code;
  124 + Error(std::string parent, std::string name, int exit_code=255) : runtime_error(parent + ": " + name), exit_code(exit_code) {}
133 }; 125 };
134 126
135 -struct BadNameString : public Error {  
136 - BadNameString(std::string name) : Error("BadNameString", name) {} 127 +struct CallForHelp : public Error {
  128 + CallForHelp() : Error("CallForHelp","", 0) {}
137 }; 129 };
138 130
139 -struct CallForHelp : public Error {  
140 - CallForHelp() : Error("CallForHelp","") {} 131 +struct BadNameString : public Error {
  132 + BadNameString(std::string name) : Error("BadNameString", name, 1) {}
141 }; 133 };
142 134
  135 +
143 struct ParseError : public Error { 136 struct ParseError : public Error {
144 - ParseError(std::string name) : Error("ParseError", name) {} 137 + ParseError(std::string name) : Error("ParseError", name, 2) {}
145 }; 138 };
146 139
147 struct OptionAlreadyAdded : public Error { 140 struct OptionAlreadyAdded : public Error {
148 - OptionAlreadyAdded(std::string name) : Error("OptionAlreadyAdded", name) {} 141 + OptionAlreadyAdded(std::string name) : Error("OptionAlreadyAdded", name, 3) {}
149 }; 142 };
150 143
151 struct OptionNotFound : public Error { 144 struct OptionNotFound : public Error {
152 - OptionNotFound(std::string name) : Error("OptionNotFound", name) {} 145 + OptionNotFound(std::string name) : Error("OptionNotFound", name, 4) {}
153 }; 146 };
154 147
155 struct RequiredError : public Error { 148 struct RequiredError : public Error {
156 - RequiredError(std::string name) : Error("RequiredError", name) {} 149 + RequiredError(std::string name) : Error("RequiredError", name, 5) {}
157 }; 150 };
158 151
159 struct PositionalError : public Error { 152 struct PositionalError : public Error {
160 - PositionalError(std::string name) : Error("PositionalError", name) {} 153 + PositionalError(std::string name) : Error("PositionalError", name, 6) {}
161 }; 154 };
162 155
163 struct HorribleError : public Error { 156 struct HorribleError : public Error {
164 - HorribleError(std::string name) : Error("HorribleError", "(You should never see this error) " + name) {} 157 + HorribleError(std::string name) : Error("HorribleError", "(You should never see this error) " + name, 7) {}
165 }; 158 };
166 struct IncorrectConstruction : public Error { 159 struct IncorrectConstruction : public Error {
167 - IncorrectConstruction(std::string name) : Error("IncorrectConstruction", name) {} 160 + IncorrectConstruction(std::string name) : Error("IncorrectConstruction", name, 8) {}
168 }; 161 };
169 162
170 const std::regex reg_split{R"regex((?:([a-zA-Z0-9]?)(?:,|$)|^)([a-zA-Z0-9][a-zA-Z0-9_\-]*)?)regex"}; 163 const std::regex reg_split{R"regex((?:([a-zA-Z0-9]?)(?:,|$)|^)([a-zA-Z0-9][a-zA-Z0-9_\-]*)?)regex"};
@@ -773,30 +766,19 @@ public: @@ -773,30 +766,19 @@ public:
773 /// This must be called after the options are in but before the rest of the program. 766 /// This must be called after the options are in but before the rest of the program.
774 /** Instead of throwing erros, this gives an error code 767 /** Instead of throwing erros, this gives an error code
775 * if -h or an invalid option is passed. Continue with your program if returns -1 */ 768 * if -h or an invalid option is passed. Continue with your program if returns -1 */
776 - Return start(int argc, char** argv) { 769 + void run(int argc, char** argv) {
  770 + parse(argc, argv);
  771 + }
777 772
778 - std::function<void(const Error&)> msg_fun{[this](const Error &e){ 773 + int exit(const Error& e) const {
  774 + if(e.exit_code != 0) {
779 std::cerr << "ERROR: "; 775 std::cerr << "ERROR: ";
780 std::cerr << e.what() << std::endl; 776 std::cerr << e.what() << std::endl;
781 std::cerr << help() << std::endl; 777 std::cerr << help() << std::endl;
782 - }};  
783 -  
784 - try {  
785 - parse(argc, argv);  
786 - return Return::Continue;  
787 - } catch(const CallForHelp &e) { 778 + } else {
788 std::cout << help() << std::endl; 779 std::cout << help() << std::endl;
789 - return Return::Success;  
790 - } catch(const ParseError &e) {  
791 - msg_fun(e);  
792 - return Return::ParseError;  
793 - } catch(const PositionalError &e) {  
794 - msg_fun(e);  
795 - return Return::RequiredError;  
796 - } catch(const Error &e) {  
797 - msg_fun(e);  
798 - return Return::GeneralError;  
799 } 780 }
  781 + return e.exit_code;
800 } 782 }
801 783
802 /// Counts the number of times the given option was passed. 784 /// Counts the number of times the given option was passed.