mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 14:44:40 +00:00
Added flag() shorthand function
This commit is contained in:
parent
5e7ce61ca7
commit
716ec60291
@ -459,6 +459,16 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
// This is shorthand for:
|
||||
// program.add_argument("foo")
|
||||
// .default_value(false)
|
||||
// .implicit_value(true)
|
||||
Argument &flag() {
|
||||
default_value(false);
|
||||
implicit_value(true);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class F, class... Args>
|
||||
auto action(F &&callable, Args &&...bound_args)
|
||||
-> std::enable_if_t<std::is_invocable_v<F, Args..., std::string const>,
|
||||
|
@ -5,9 +5,9 @@
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
program.add_argument("-a").default_value(false).implicit_value(true);
|
||||
program.add_argument("-a").flag();
|
||||
|
||||
program.add_argument("-b").default_value(false).implicit_value(true);
|
||||
program.add_argument("-b").flag();
|
||||
|
||||
program.add_argument("-c")
|
||||
.nargs(2)
|
||||
|
@ -8,7 +8,7 @@ int main(int argc, char *argv[]) {
|
||||
program.add_argument("--member")
|
||||
.help("The alias for the member to pass to.")
|
||||
.metavar("ALIAS");
|
||||
program.add_argument("--verbose").default_value(false).implicit_value(true);
|
||||
program.add_argument("--verbose").flag();
|
||||
|
||||
program.add_description("Forward a thing to the next member.");
|
||||
program.add_epilog("Possible things include betingalw, chiz, and res.");
|
||||
|
@ -9,7 +9,7 @@ int main(int argc, char *argv[]) {
|
||||
.help("display the square of a given number")
|
||||
.scan<'i', int>();
|
||||
|
||||
program.add_argument("--verbose").default_value(false).implicit_value(true);
|
||||
program.add_argument("--verbose").flag();
|
||||
|
||||
try {
|
||||
program.parse_args(argc, argv);
|
||||
|
@ -11,11 +11,11 @@ using doctest::test_suite;
|
||||
TEST_CASE("Parse compound toggle arguments with implicit values" *
|
||||
test_suite("compound_arguments")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("-a").default_value(false).implicit_value(true);
|
||||
program.add_argument("-a").flag();
|
||||
|
||||
program.add_argument("-u").default_value(false).implicit_value(true);
|
||||
program.add_argument("-u").flag();
|
||||
|
||||
program.add_argument("-x").default_value(false).implicit_value(true);
|
||||
program.add_argument("-x").flag();
|
||||
|
||||
program.parse_args({"./test.exe", "-aux"});
|
||||
REQUIRE(program.get<bool>("-a") == true);
|
||||
@ -26,9 +26,9 @@ TEST_CASE("Parse compound toggle arguments with implicit values" *
|
||||
TEST_CASE("Parse compound toggle arguments with implicit values and nargs" *
|
||||
test_suite("compound_arguments")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("-a").default_value(false).implicit_value(true);
|
||||
program.add_argument("-a").flag();
|
||||
|
||||
program.add_argument("-b").default_value(false).implicit_value(true);
|
||||
program.add_argument("-b").flag();
|
||||
|
||||
program.add_argument("-c").nargs(2).scan<'g', float>();
|
||||
|
||||
@ -56,9 +56,9 @@ TEST_CASE("Parse compound toggle arguments with implicit values and nargs and "
|
||||
|
||||
program.add_argument("numbers").nargs(3).scan<'i', int>();
|
||||
|
||||
program.add_argument("-a").default_value(false).implicit_value(true);
|
||||
program.add_argument("-a").flag();
|
||||
|
||||
program.add_argument("-b").default_value(false).implicit_value(true);
|
||||
program.add_argument("-b").flag();
|
||||
|
||||
program.add_argument("-c").nargs(2).scan<'g', float>();
|
||||
|
||||
@ -73,9 +73,9 @@ TEST_CASE("Parse out-of-order compound arguments" *
|
||||
test_suite("compound_arguments")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
program.add_argument("-a").default_value(false).implicit_value(true);
|
||||
program.add_argument("-a").flag();
|
||||
|
||||
program.add_argument("-b").default_value(false).implicit_value(true);
|
||||
program.add_argument("-b").flag();
|
||||
|
||||
program.add_argument("-c").nargs(2).scan<'g', float>();
|
||||
|
||||
@ -93,9 +93,9 @@ TEST_CASE("Parse out-of-order compound arguments. Second variation" *
|
||||
test_suite("compound_arguments")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
program.add_argument("-a").default_value(false).implicit_value(true);
|
||||
program.add_argument("-a").flag();
|
||||
|
||||
program.add_argument("-b").default_value(false).implicit_value(true);
|
||||
program.add_argument("-b").flag();
|
||||
|
||||
program.add_argument("-c")
|
||||
.nargs(2)
|
||||
|
@ -35,8 +35,8 @@ TEST_CASE("Use a default value with flag arguments" *
|
||||
"chromatin, used ',' to split."})
|
||||
.default_value("all");
|
||||
|
||||
program.add_argument("-l").default_value(false).implicit_value(true);
|
||||
program.add_argument("-o").default_value(false).implicit_value(true);
|
||||
program.add_argument("-l").flag();
|
||||
program.add_argument("-o").flag();
|
||||
|
||||
program.add_argument("filename");
|
||||
|
||||
|
@ -95,7 +95,7 @@ TEST_CASE("Multiline help message alignment") {
|
||||
R"(#Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
#Sed ut perspiciatis unde omnis iste natus error sit voluptatem
|
||||
#accusantium doloremque laudantium, totam rem aperiam...)");
|
||||
program.add_argument("--verbose").default_value(false).implicit_value(true);
|
||||
program.add_argument("--verbose").flag();
|
||||
|
||||
std::ostringstream stream;
|
||||
stream << program;
|
||||
|
@ -30,7 +30,7 @@ TEST_CASE("Argument '-' is not an optional argument" *
|
||||
TEST_CASE("Argument '-' is not an optional argument but '-l' is" *
|
||||
test_suite("optional_arguments")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("-l").default_value(false).implicit_value(true);
|
||||
program.add_argument("-l").flag();
|
||||
program.add_argument("input");
|
||||
program.parse_args({"./test.exe", "-l", "-"});
|
||||
REQUIRE(program.get<bool>("-l") == true);
|
||||
@ -40,7 +40,7 @@ TEST_CASE("Argument '-' is not an optional argument but '-l' is" *
|
||||
TEST_CASE("Argument '-l' is an optional argument but '-' is not" *
|
||||
test_suite("optional_arguments")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("-l").default_value(false).implicit_value(true);
|
||||
program.add_argument("-l").flag();
|
||||
program.add_argument("input");
|
||||
program.parse_args({"./test.exe", "-", "-l"});
|
||||
REQUIRE(program.get<bool>("-l") == true);
|
||||
@ -50,7 +50,7 @@ TEST_CASE("Argument '-l' is an optional argument but '-' is not" *
|
||||
TEST_CASE("Parse toggle arguments with implicit value" *
|
||||
test_suite("optional_arguments")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--verbose").default_value(false).implicit_value(true);
|
||||
program.add_argument("--verbose").flag();
|
||||
|
||||
program.parse_args({"./test.exe", "--verbose"});
|
||||
REQUIRE(program.get<bool>("--verbose") == true);
|
||||
@ -61,11 +61,11 @@ TEST_CASE("Parse toggle arguments with implicit value" *
|
||||
TEST_CASE("Parse multiple toggle arguments with implicit values" *
|
||||
test_suite("optional_arguments")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("-a").default_value(false).implicit_value(true);
|
||||
program.add_argument("-a").flag();
|
||||
|
||||
program.add_argument("-u").default_value(false).implicit_value(true);
|
||||
program.add_argument("-u").flag();
|
||||
|
||||
program.add_argument("-x").default_value(false).implicit_value(true);
|
||||
program.add_argument("-x").flag();
|
||||
|
||||
program.parse_args({"./test.exe", "-a", "-x"});
|
||||
REQUIRE(program.get<bool>("-a") == true);
|
||||
|
@ -23,8 +23,8 @@ TEST_CASE("Parse with custom Windows-style prefix chars" *
|
||||
argparse::ArgumentParser program("dir");
|
||||
program.set_prefix_chars("/");
|
||||
program.add_argument("/A").nargs(1);
|
||||
program.add_argument("/B").default_value(false).implicit_value(true);
|
||||
program.add_argument("/C").default_value(false).implicit_value(true);
|
||||
program.add_argument("/B").flag();
|
||||
program.add_argument("/C").flag();
|
||||
program.parse_args({"dir", "/A", "D", "/B", "/C"});
|
||||
REQUIRE(program.get("/A") == "D");
|
||||
REQUIRE(program.get<bool>("/B") == true);
|
||||
@ -37,7 +37,7 @@ TEST_CASE("Parse with custom Windows-style prefix chars and assign chars" *
|
||||
program.set_assign_chars(":=");
|
||||
program.add_argument("/A").nargs(1);
|
||||
program.add_argument("/B").nargs(1);
|
||||
program.add_argument("/C").default_value(false).implicit_value(true);
|
||||
program.add_argument("/C").flag();
|
||||
program.parse_args({"dir", "/A:D", "/B=Boo", "/C"});
|
||||
REQUIRE(program.get("/A") == "D");
|
||||
REQUIRE(program.get("/B") == "Boo");
|
||||
|
@ -61,7 +61,7 @@ TEST_CASE("Parse subparser command" * test_suite("subparsers")) {
|
||||
TEST_CASE("Parse subparser command with optional argument" *
|
||||
test_suite("subparsers")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--verbose").default_value(false).implicit_value(true);
|
||||
program.add_argument("--verbose").flag();
|
||||
|
||||
argparse::ArgumentParser command_1("add");
|
||||
command_1.add_argument("file");
|
||||
@ -93,7 +93,7 @@ TEST_CASE("Parse subparser command with parent parser" *
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
argparse::ArgumentParser parent("parent");
|
||||
parent.add_argument("--verbose").default_value(false).implicit_value(true);
|
||||
parent.add_argument("--verbose").flag();
|
||||
program.add_parents(parent);
|
||||
|
||||
argparse::ArgumentParser command_1("add");
|
||||
@ -128,7 +128,7 @@ TEST_CASE("Parse git commands" * test_suite("subparsers")) {
|
||||
add_command.add_argument("files").remaining();
|
||||
|
||||
argparse::ArgumentParser commit_command("commit");
|
||||
commit_command.add_argument("-a").default_value(false).implicit_value(true);
|
||||
commit_command.add_argument("-a").flag();
|
||||
|
||||
commit_command.add_argument("-m");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user