mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
Merge pull request #218 from skrobinson/feat-bool-argparser
Allow check if ArgumentParser has parsed values
This commit is contained in:
commit
ed2953aa3d
@ -38,6 +38,7 @@
|
|||||||
* [Parent Parsers](#parent-parsers)
|
* [Parent Parsers](#parent-parsers)
|
||||||
* [Subcommands](#subcommands)
|
* [Subcommands](#subcommands)
|
||||||
* [Parse Known Args](#parse-known-args)
|
* [Parse Known Args](#parse-known-args)
|
||||||
|
* [ArgumentParser in bool Context](#argumentparser-in-bool-context)
|
||||||
* [Custom Prefix Characters](#custom-prefix-characters)
|
* [Custom Prefix Characters](#custom-prefix-characters)
|
||||||
* [Custom Assignment Characters](#custom-assignment-characters)
|
* [Custom Assignment Characters](#custom-assignment-characters)
|
||||||
* [Further Examples](#further-examples)
|
* [Further Examples](#further-examples)
|
||||||
@ -848,7 +849,13 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Custom Prefix Characters
|
### ArgumentParser in bool Context
|
||||||
|
|
||||||
|
An `ArgumentParser` is `false` until it (or one of its subparsers) have extracted
|
||||||
|
known value(s) with `.parse_args` or `.parse_known_args`. When using `.parse_known_args`,
|
||||||
|
unknown arguments will not make a parser `true`.
|
||||||
|
|
||||||
|
### Custom Prefix Characters
|
||||||
|
|
||||||
Most command-line options will use `-` as the prefix, e.g. `-f/--foo`. Parsers that need to support different or additional prefix characters, e.g. for options like `+f` or `/foo`, may specify them using the `set_prefix_chars()`.
|
Most command-line options will use `-` as the prefix, e.g. `-f/--foo`. Parsers that need to support different or additional prefix characters, e.g. for options like `+f` or `/foo`, may specify them using the `set_prefix_chars()`.
|
||||||
|
|
||||||
|
@ -1104,6 +1104,21 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
explicit operator bool() const {
|
||||||
|
auto arg_used = std::any_of(m_argument_map.cbegin(),
|
||||||
|
m_argument_map.cend(),
|
||||||
|
[](auto &it) {
|
||||||
|
return it.second->m_is_used;
|
||||||
|
});
|
||||||
|
auto subparser_used = std::any_of(m_subparser_used.cbegin(),
|
||||||
|
m_subparser_used.cend(),
|
||||||
|
[](auto &it) {
|
||||||
|
return it.second;
|
||||||
|
});
|
||||||
|
|
||||||
|
return m_is_parsed && (arg_used || subparser_used);
|
||||||
|
}
|
||||||
|
|
||||||
// Parameter packing
|
// Parameter packing
|
||||||
// Call add_argument with variadic number of string arguments
|
// Call add_argument with variadic number of string arguments
|
||||||
template <typename... Targs> Argument &add_argument(Targs... f_args) {
|
template <typename... Targs> Argument &add_argument(Targs... f_args) {
|
||||||
|
@ -27,6 +27,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
|
|||||||
main.cpp
|
main.cpp
|
||||||
test_actions.cpp
|
test_actions.cpp
|
||||||
test_append.cpp
|
test_append.cpp
|
||||||
|
test_bool_operator.cpp
|
||||||
test_compound_arguments.cpp
|
test_compound_arguments.cpp
|
||||||
test_container_arguments.cpp
|
test_container_arguments.cpp
|
||||||
test_const_correct.cpp
|
test_const_correct.cpp
|
||||||
|
85
test/test_bool_operator.cpp
Normal file
85
test/test_bool_operator.cpp
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#include <argparse/argparse.hpp>
|
||||||
|
#include <doctest.hpp>
|
||||||
|
|
||||||
|
using doctest::test_suite;
|
||||||
|
|
||||||
|
TEST_CASE("ArgumentParser in bool context" *
|
||||||
|
test_suite("argument_parser")) {
|
||||||
|
argparse::ArgumentParser program("test");
|
||||||
|
program.add_argument("cases").remaining();
|
||||||
|
|
||||||
|
program.parse_args({"test"});
|
||||||
|
REQUIRE_FALSE(program);
|
||||||
|
|
||||||
|
program.parse_args({"test", "one", "two"});
|
||||||
|
REQUIRE(program);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("With subparsers in bool context" * test_suite("argument_parser")) {
|
||||||
|
argparse::ArgumentParser program("test");
|
||||||
|
|
||||||
|
argparse::ArgumentParser cmd_fly("fly");
|
||||||
|
cmd_fly.add_argument("plane");
|
||||||
|
|
||||||
|
argparse::ArgumentParser cmd_soar("soar");
|
||||||
|
cmd_soar.add_argument("direction");
|
||||||
|
|
||||||
|
program.add_subparser(cmd_fly);
|
||||||
|
program.add_subparser(cmd_soar);
|
||||||
|
|
||||||
|
program.parse_args({"test", "fly", "glider"});
|
||||||
|
REQUIRE(program);
|
||||||
|
REQUIRE(cmd_fly);
|
||||||
|
REQUIRE_FALSE(cmd_soar);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Parsers remain false with unknown arguments" *
|
||||||
|
test_suite("argument_parser")) {
|
||||||
|
argparse::ArgumentParser program("test");
|
||||||
|
|
||||||
|
argparse::ArgumentParser cmd_build("build");
|
||||||
|
cmd_build.add_argument("--file").nargs(1);
|
||||||
|
|
||||||
|
argparse::ArgumentParser cmd_run("run");
|
||||||
|
cmd_run.add_argument("--file").nargs(1);
|
||||||
|
|
||||||
|
program.add_subparser(cmd_build);
|
||||||
|
program.add_subparser(cmd_run);
|
||||||
|
|
||||||
|
auto unknowns =
|
||||||
|
program.parse_known_args({"test", "badger", "--add-meal", "grubs"});
|
||||||
|
REQUIRE_FALSE(program);
|
||||||
|
REQUIRE_FALSE(cmd_build);
|
||||||
|
REQUIRE_FALSE(cmd_run);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Multi-level parsers match subparser bool" *
|
||||||
|
test_suite("argument_parser")) {
|
||||||
|
argparse::ArgumentParser program("test");
|
||||||
|
|
||||||
|
argparse::ArgumentParser cmd_cook("cook");
|
||||||
|
cmd_cook.add_argument("--temperature");
|
||||||
|
|
||||||
|
argparse::ArgumentParser cmd_cook_boil("boil");
|
||||||
|
cmd_cook_boil.add_argument("--rate");
|
||||||
|
|
||||||
|
argparse::ArgumentParser cmd_cook_boil_stir("stir");
|
||||||
|
cmd_cook_boil_stir.add_argument("--rate");
|
||||||
|
|
||||||
|
argparse::ArgumentParser cmd_wash("wash");
|
||||||
|
|
||||||
|
program.add_subparser(cmd_cook);
|
||||||
|
cmd_cook.add_subparser(cmd_cook_boil);
|
||||||
|
cmd_cook_boil.add_subparser(cmd_cook_boil_stir);
|
||||||
|
|
||||||
|
program.add_subparser(cmd_wash);
|
||||||
|
|
||||||
|
auto unknowns = program.parse_known_args(
|
||||||
|
{"test", "cook", "boil", "stir", "--rate", "fast"});
|
||||||
|
|
||||||
|
REQUIRE(program);
|
||||||
|
REQUIRE(cmd_cook);
|
||||||
|
REQUIRE(cmd_cook_boil);
|
||||||
|
REQUIRE(cmd_cook_boil_stir);
|
||||||
|
REQUIRE_FALSE(cmd_wash);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user