mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-05-01 13:13:53 +00:00
Adding tests and shorter Error defs
This commit is contained in:
parent
4b0f6dbfd6
commit
a20efe069c
@ -62,52 +62,42 @@ struct Combiner {
|
|||||||
|
|
||||||
namespace CLI {
|
namespace CLI {
|
||||||
|
|
||||||
class Error : public std::runtime_error {
|
struct Error : public std::runtime_error {
|
||||||
public:
|
|
||||||
Error(std::string parent, std::string name) : runtime_error(parent + ": " + name) {}
|
Error(std::string parent, std::string name) : runtime_error(parent + ": " + name) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class BadNameString : public Error {
|
struct BadNameString : public Error {
|
||||||
public:
|
|
||||||
BadNameString(std::string name) : Error("BadNameString", name) {}
|
BadNameString(std::string name) : Error("BadNameString", name) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class CallForHelp : public Error {
|
struct CallForHelp : public Error {
|
||||||
public:
|
|
||||||
CallForHelp() : Error("CallForHelp","") {}
|
CallForHelp() : Error("CallForHelp","") {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ParseError : public Error {
|
struct ParseError : public Error {
|
||||||
public:
|
|
||||||
ParseError(std::string name) : Error("ParseError", name) {}
|
ParseError(std::string name) : Error("ParseError", name) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class OptionAlreadyAdded : public Error {
|
struct OptionAlreadyAdded : public Error {
|
||||||
public:
|
|
||||||
OptionAlreadyAdded(std::string name) : Error("OptionAlreadyAdded", name) {}
|
OptionAlreadyAdded(std::string name) : Error("OptionAlreadyAdded", name) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class OptionNotFound : public Error {
|
struct OptionNotFound : public Error {
|
||||||
public:
|
|
||||||
OptionNotFound(std::string name) : Error("OptionNotFound", name) {}
|
OptionNotFound(std::string name) : Error("OptionNotFound", name) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class RequiredError : public Error {
|
struct RequiredError : public Error {
|
||||||
public:
|
|
||||||
RequiredError(std::string name) : Error("RequiredError", name) {}
|
RequiredError(std::string name) : Error("RequiredError", name) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ExtraPositionalsError : public Error {
|
struct ExtraPositionalsError : public Error {
|
||||||
public:
|
|
||||||
ExtraPositionalsError(std::string name) : Error("ExtraPositionalsError", name) {}
|
ExtraPositionalsError(std::string name) : Error("ExtraPositionalsError", name) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class HorribleError : public Error {
|
struct HorribleError : public Error {
|
||||||
public:
|
|
||||||
HorribleError(std::string name) : Error("HorribleError", "(You should never see this error) " + name) {}
|
HorribleError(std::string name) : Error("HorribleError", "(You should never see this error) " + name) {}
|
||||||
};
|
};
|
||||||
class IncorrectConstruction : public Error {
|
struct IncorrectConstruction : public Error {
|
||||||
public:
|
|
||||||
IncorrectConstruction(std::string name) : Error("IncorrectConstruction", name) {}
|
IncorrectConstruction(std::string name) : Error("IncorrectConstruction", name) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -170,10 +170,8 @@ TEST_F(TApp, Reset) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TSubcom : public TApp {
|
|
||||||
};
|
|
||||||
|
|
||||||
TEST_F(TSubcom, Basic) {
|
TEST_F(TApp, Basic) {
|
||||||
auto sub1 = app.add_subcommand("sub1");
|
auto sub1 = app.add_subcommand("sub1");
|
||||||
auto sub2 = app.add_subcommand("sub2");
|
auto sub2 = app.add_subcommand("sub2");
|
||||||
|
|
||||||
@ -192,3 +190,55 @@ TEST_F(TSubcom, Basic) {
|
|||||||
run();
|
run();
|
||||||
EXPECT_EQ(sub2, app.get_subcommand());
|
EXPECT_EQ(sub2, app.get_subcommand());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct SubcommandProgram : public TApp {
|
||||||
|
|
||||||
|
CLI::App* start;
|
||||||
|
CLI::App* stop;
|
||||||
|
|
||||||
|
int dummy;
|
||||||
|
std::string file;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
SubcommandProgram() {
|
||||||
|
start = app.add_subcommand("start", "Start prog");
|
||||||
|
stop = app.add_subcommand("stop", "Stop prog");
|
||||||
|
|
||||||
|
app.add_flag("d", dummy, "My dummy var");
|
||||||
|
start->add_option("f,file", file, "File name");
|
||||||
|
stop->add_flag("c,count", count, "Some flag opt");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(SubcommandProgram, Working) {
|
||||||
|
args = {"-d", "start", "-ffilename"};
|
||||||
|
|
||||||
|
run();
|
||||||
|
|
||||||
|
EXPECT_EQ(1, dummy);
|
||||||
|
EXPECT_EQ(start, app.get_subcommand());
|
||||||
|
EXPECT_EQ("filename", file);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_F(SubcommandProgram, Spare) {
|
||||||
|
args = {"extra", "-d", "start", "-ffilename"};
|
||||||
|
|
||||||
|
EXPECT_THROW(run(), CLI::ExtraPositionalsError);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SubcommandProgram, SpareSub) {
|
||||||
|
args = {"-d", "start", "spare", "-ffilename"};
|
||||||
|
|
||||||
|
EXPECT_THROW(run(), CLI::ExtraPositionalsError);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Add positionals
|
||||||
|
// TODO: Add vector arguments
|
||||||
|
// TODO: Maybe add function to call on subcommand parse?
|
||||||
|
// TODO: Check help output
|
||||||
|
// TODO: Add default/type info to help
|
||||||
|
// TODO: Add set checking
|
||||||
|
// TODO: Try all of the options together
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user