From 4b47ec9c9676b15bf0261c0bbadf9662df7a8239 Mon Sep 17 00:00:00 2001 From: Shani Armon Date: Tue, 5 May 2020 14:21:09 +0300 Subject: [PATCH] Enable REMAINDER argparse arguments to be used in mutually exclusive groups --- Lib/argparse.py | 6 +++++- .../next/Library/2020-05-06-14-31-07.bpo-40509.PgYRgH.rst | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-05-06-14-31-07.bpo-40509.PgYRgH.rst diff --git a/Lib/argparse.py b/Lib/argparse.py index 9c710cef5b6aaa..0370e244bae6da 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1516,7 +1516,7 @@ def _get_positional_kwargs(self, dest, **kwargs): # mark positional arguments as required if at least one is # always required - if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE]: + if kwargs.get('nargs') not in [OPTIONAL, ZERO_OR_MORE, REMAINDER]: kwargs['required'] = True if kwargs.get('nargs') == ZERO_OR_MORE and 'default' not in kwargs: kwargs['required'] = True @@ -1915,6 +1915,10 @@ def take_action(action, argument_strings, option_string=None): # error if this argument is not allowed with other previously # seen arguments, assuming that actions that use the default # value don't really count as "present" + if action.nargs in [REMAINDER]: + if argument_values == []: + action.default = argument_values + if argument_values is not action.default: seen_non_default_actions.add(action) for conflict_action in action_conflicts.get(action, []): diff --git a/Misc/NEWS.d/next/Library/2020-05-06-14-31-07.bpo-40509.PgYRgH.rst b/Misc/NEWS.d/next/Library/2020-05-06-14-31-07.bpo-40509.PgYRgH.rst new file mode 100644 index 00000000000000..3b27661e239069 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-06-14-31-07.bpo-40509.PgYRgH.rst @@ -0,0 +1 @@ +Allow REMAINDER positional arguments in argparse mutually exclusive groups.