Commit a20efe069c3958229e15dfc6115c475d560b7a88
1 parent
4b0f6dbf
Adding tests and shorter Error defs
Showing
2 changed files
with
63 additions
and
23 deletions
include/CLI.hpp
| ... | ... | @@ -62,52 +62,42 @@ struct Combiner { |
| 62 | 62 | |
| 63 | 63 | namespace CLI { |
| 64 | 64 | |
| 65 | -class Error : public std::runtime_error { | |
| 66 | -public: | |
| 65 | +struct Error : public std::runtime_error { | |
| 67 | 66 | Error(std::string parent, std::string name) : runtime_error(parent + ": " + name) {} |
| 68 | 67 | }; |
| 69 | 68 | |
| 70 | -class BadNameString : public Error { | |
| 71 | -public: | |
| 69 | +struct BadNameString : public Error { | |
| 72 | 70 | BadNameString(std::string name) : Error("BadNameString", name) {} |
| 73 | 71 | }; |
| 74 | 72 | |
| 75 | -class CallForHelp : public Error { | |
| 76 | -public: | |
| 73 | +struct CallForHelp : public Error { | |
| 77 | 74 | CallForHelp() : Error("CallForHelp","") {} |
| 78 | 75 | }; |
| 79 | 76 | |
| 80 | -class ParseError : public Error { | |
| 81 | -public: | |
| 77 | +struct ParseError : public Error { | |
| 82 | 78 | ParseError(std::string name) : Error("ParseError", name) {} |
| 83 | 79 | }; |
| 84 | 80 | |
| 85 | -class OptionAlreadyAdded : public Error { | |
| 86 | -public: | |
| 81 | +struct OptionAlreadyAdded : public Error { | |
| 87 | 82 | OptionAlreadyAdded(std::string name) : Error("OptionAlreadyAdded", name) {} |
| 88 | 83 | }; |
| 89 | 84 | |
| 90 | -class OptionNotFound : public Error { | |
| 91 | -public: | |
| 85 | +struct OptionNotFound : public Error { | |
| 92 | 86 | OptionNotFound(std::string name) : Error("OptionNotFound", name) {} |
| 93 | 87 | }; |
| 94 | 88 | |
| 95 | -class RequiredError : public Error { | |
| 96 | -public: | |
| 89 | +struct RequiredError : public Error { | |
| 97 | 90 | RequiredError(std::string name) : Error("RequiredError", name) {} |
| 98 | 91 | }; |
| 99 | 92 | |
| 100 | -class ExtraPositionalsError : public Error { | |
| 101 | -public: | |
| 93 | +struct ExtraPositionalsError : public Error { | |
| 102 | 94 | ExtraPositionalsError(std::string name) : Error("ExtraPositionalsError", name) {} |
| 103 | 95 | }; |
| 104 | 96 | |
| 105 | -class HorribleError : public Error { | |
| 106 | -public: | |
| 97 | +struct HorribleError : public Error { | |
| 107 | 98 | HorribleError(std::string name) : Error("HorribleError", "(You should never see this error) " + name) {} |
| 108 | 99 | }; |
| 109 | -class IncorrectConstruction : public Error { | |
| 110 | -public: | |
| 100 | +struct IncorrectConstruction : public Error { | |
| 111 | 101 | IncorrectConstruction(std::string name) : Error("IncorrectConstruction", name) {} |
| 112 | 102 | }; |
| 113 | 103 | ... | ... |
tests/CLItest.cpp
| ... | ... | @@ -170,10 +170,8 @@ TEST_F(TApp, Reset) { |
| 170 | 170 | |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | -struct TSubcom : public TApp { | |
| 174 | -}; | |
| 175 | 173 | |
| 176 | -TEST_F(TSubcom, Basic) { | |
| 174 | +TEST_F(TApp, Basic) { | |
| 177 | 175 | auto sub1 = app.add_subcommand("sub1"); |
| 178 | 176 | auto sub2 = app.add_subcommand("sub2"); |
| 179 | 177 | |
| ... | ... | @@ -192,3 +190,55 @@ TEST_F(TSubcom, Basic) { |
| 192 | 190 | run(); |
| 193 | 191 | EXPECT_EQ(sub2, app.get_subcommand()); |
| 194 | 192 | } |
| 193 | + | |
| 194 | + | |
| 195 | +struct SubcommandProgram : public TApp { | |
| 196 | + | |
| 197 | + CLI::App* start; | |
| 198 | + CLI::App* stop; | |
| 199 | + | |
| 200 | + int dummy; | |
| 201 | + std::string file; | |
| 202 | + int count; | |
| 203 | + | |
| 204 | + SubcommandProgram() { | |
| 205 | + start = app.add_subcommand("start", "Start prog"); | |
| 206 | + stop = app.add_subcommand("stop", "Stop prog"); | |
| 207 | + | |
| 208 | + app.add_flag("d", dummy, "My dummy var"); | |
| 209 | + start->add_option("f,file", file, "File name"); | |
| 210 | + stop->add_flag("c,count", count, "Some flag opt"); | |
| 211 | + } | |
| 212 | +}; | |
| 213 | + | |
| 214 | +TEST_F(SubcommandProgram, Working) { | |
| 215 | + args = {"-d", "start", "-ffilename"}; | |
| 216 | + | |
| 217 | + run(); | |
| 218 | + | |
| 219 | + EXPECT_EQ(1, dummy); | |
| 220 | + EXPECT_EQ(start, app.get_subcommand()); | |
| 221 | + EXPECT_EQ("filename", file); | |
| 222 | +} | |
| 223 | + | |
| 224 | + | |
| 225 | +TEST_F(SubcommandProgram, Spare) { | |
| 226 | + args = {"extra", "-d", "start", "-ffilename"}; | |
| 227 | + | |
| 228 | + EXPECT_THROW(run(), CLI::ExtraPositionalsError); | |
| 229 | +} | |
| 230 | + | |
| 231 | +TEST_F(SubcommandProgram, SpareSub) { | |
| 232 | + args = {"-d", "start", "spare", "-ffilename"}; | |
| 233 | + | |
| 234 | + EXPECT_THROW(run(), CLI::ExtraPositionalsError); | |
| 235 | +} | |
| 236 | + | |
| 237 | +// TODO: Add positionals | |
| 238 | +// TODO: Add vector arguments | |
| 239 | +// TODO: Maybe add function to call on subcommand parse? | |
| 240 | +// TODO: Check help output | |
| 241 | +// TODO: Add default/type info to help | |
| 242 | +// TODO: Add set checking | |
| 243 | +// TODO: Try all of the options together | |
| 244 | + | ... | ... |