Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse_known_args #201

Merged
merged 2 commits into from
Sep 21, 2022
Merged

parse_known_args #201

merged 2 commits into from
Sep 21, 2022

Conversation

p-ranav
Copy link
Owner

@p-ranav p-ranav commented Sep 21, 2022

Closes #181

This PR adds a parse_known_args support.

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.

#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"}));
}

@p-ranav p-ranav added the enhancement New feature or request label Sep 21, 2022
@p-ranav p-ranav self-assigned this Sep 21, 2022
@p-ranav p-ranav merged commit 4dbc910 into master Sep 21, 2022
@p-ranav p-ranav deleted the feature/parse_known_args branch September 21, 2022 12:59
@tt2468
Copy link

tt2468 commented Sep 30, 2022

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bypass Unknown Argument: exception?
2 participants