aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Belikov <pavel.fuchs.belikov@gmail.com>2017-11-09 21:31:58 +0300
committerPavel Belikov <pavel.fuchs.belikov@gmail.com>2017-11-09 21:31:58 +0300
commit34d613b02b69fdb1c66eb61184c6f660d98ff95d (patch)
treee28b72f3d9db0ce7d490650598d24b9422cdb7ee
parentadd documentation for Command::RequireCommand (diff)
downloadargs.hxx-34d613b02b69fdb1c66eb61184c6f660d98ff95d.tar.xz
change default value of RequireCommand
-rw-r--r--args.hxx4
-rw-r--r--test.cxx15
2 files changed, 9 insertions, 10 deletions
diff --git a/args.hxx b/args.hxx
index 21dd433..6ec4ab0 100644
--- a/args.hxx
+++ b/args.hxx
@@ -1261,7 +1261,7 @@ namespace args
std::string proglinePostfix;
std::function<void(Subparser&)> parserCoroutine;
- bool commandIsRequired = false;
+ bool commandIsRequired = true;
Command *selectedCommand = nullptr;
mutable std::vector<std::tuple<std::string, std::string, unsigned>> subparserDescription;
@@ -1375,6 +1375,8 @@ namespace args
{ return help; }
/** If value is true, parser will fail if no command was parsed.
+ *
+ * Default: true.
*/
void RequireCommand(bool value)
{ commandIsRequired = value; }
diff --git a/test.cxx b/test.cxx
index 0bb58d2..ba4d5f8 100644
--- a/test.cxx
+++ b/test.cxx
@@ -770,6 +770,7 @@ TEST_CASE("Subparser help works as expected", "[args]")
});
p.Prog("git");
+ p.RequireCommand(false);
std::ostringstream s;
@@ -877,15 +878,12 @@ TEST_CASE("Subparser validation works as expected", "[args]")
args::Command c(p, "c", "command c", [](args::Subparser&){});
- REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{}));
+ REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{}), args::ValidationError);
REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{"a"}), args::RequiredError);
REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"a", "-f", "F"}));
REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{"b"}), args::RequiredError);
REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"b", "-f", "F"}));
- p.RequireCommand(true);
- REQUIRE_THROWS_AS(p.ParseArgs(std::vector<std::string>{}), args::ValidationError);
-
p.RequireCommand(false);
REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{}));
@@ -904,6 +902,8 @@ TEST_CASE("Global options work as expected", "[args]")
args::Command a(p, "a", "command a");
args::Command b(p, "b", "command b");
+ p.RequireCommand(false);
+
REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"-f"}));
REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"a", "-f"}));
REQUIRE_NOTHROW(p.ParseArgs(std::vector<std::string>{"b", "-f"}));
@@ -1012,7 +1012,7 @@ TEST_CASE("Subparser validation works as expected in noexcept mode", "[args]")
argstest::Command c(p, "c", "command c", [](argstest::Subparser&){});
p.ParseArgs(std::vector<std::string>{});
- REQUIRE(p.GetError() == argstest::Error::None);
+ REQUIRE(p.GetError() == argstest::Error::Validation);
p.ParseArgs(std::vector<std::string>{"a"});
REQUIRE((size_t)p.GetError() == (size_t)argstest::Error::Required);
@@ -1026,10 +1026,6 @@ TEST_CASE("Subparser validation works as expected in noexcept mode", "[args]")
p.ParseArgs(std::vector<std::string>{"b", "-f", "F"});
REQUIRE(p.GetError() == argstest::Error::None);
- p.RequireCommand(true);
- p.ParseArgs(std::vector<std::string>{});
- REQUIRE(p.GetError() == argstest::Error::Validation);
-
p.RequireCommand(false);
p.ParseArgs(std::vector<std::string>{});
REQUIRE(p.GetError() == argstest::Error::None);
@@ -1046,3 +1042,4 @@ TEST_CASE("Nargs work as expected in noexcept mode", "[args]")
parser.ParseArgs(std::vector<std::string>{"-a", "1", "2"});
REQUIRE(parser.GetError() == argstest::Error::Usage);
}
+