From 6a3c6e06e6fd4988170718f78308cf14864fe9d4 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Wed, 21 Sep 2022 05:54:21 -0700 Subject: [PATCH] Added 'Parse Known Args' section --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 853cff2..2ac64c7 100644 --- a/README.md +++ b/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("")` 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 +#include + +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("--foo") == true); + assert(program.get("bar") == std::string{"BAR"}); + assert((unknown_args == std::vector{"--badger", "spam"})); +} +``` + ## Further Examples ### Construct a JSON object from a filename argument