mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 14:44:40 +00:00
Added 'Parse Known Args' section
This commit is contained in:
parent
6e23d5b22e
commit
6a3c6e06e6
23
README.md
23
README.md
@ -37,6 +37,7 @@
|
||||
* [Gathering Remaining Arguments](#gathering-remaining-arguments)
|
||||
* [Parent Parsers](#parent-parsers)
|
||||
* [Subcommands](#subcommands)
|
||||
* [Parse Known Args](#parse-known-args)
|
||||
* [Further Examples](#further-examples)
|
||||
* [Construct a JSON object from a filename argument](#construct-a-json-object-from-a-filename-argument)
|
||||
* [Positional Arguments with Compound Toggle Arguments](#positional-arguments-with-compound-toggle-arguments)
|
||||
@ -799,6 +800,28 @@ When a help message is requested from a subparser, only the help for that partic
|
||||
|
||||
Additionally, every parser has a `.is_subcommand_used("<command_name>")` member function to check if a subcommand was used.
|
||||
|
||||
### Parse Known Args
|
||||
|
||||
Sometimes a program may only parse a few of the command-line arguments, passing the remaining arguments on to another script or program. In these cases, the `parse_known_args()` function can be useful. It works much like `parse_args()` except that it does not produce an error when extra arguments are present. Instead, it returns a list of remaining argument strings.
|
||||
|
||||
```cpp
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <cassert>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--foo").implicit_value(true).default_value(false);
|
||||
program.add_argument("bar");
|
||||
|
||||
auto unknown_args =
|
||||
program.parse_known_args({"test", "--foo", "--badger", "BAR", "spam"});
|
||||
|
||||
assert(program.get<bool>("--foo") == true);
|
||||
assert(program.get<std::string>("bar") == std::string{"BAR"});
|
||||
assert((unknown_args == std::vector<std::string>{"--badger", "spam"}));
|
||||
}
|
||||
```
|
||||
|
||||
## Further Examples
|
||||
|
||||
### Construct a JSON object from a filename argument
|
||||
|
Loading…
Reference in New Issue
Block a user