From da634ec856a397a5c458b55890849a785ea55a29 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Wed, 20 Apr 2022 09:47:06 -0700 Subject: [PATCH 1/2] single dash --- parlai/core/params.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/parlai/core/params.py b/parlai/core/params.py index bfd982c16aa..82267fc94f8 100644 --- a/parlai/core/params.py +++ b/parlai/core/params.py @@ -1099,7 +1099,9 @@ def _process_args_to_opts(self, args_that_override: Optional[List[str]] = None): if args_that_override is None: args_that_override = _sys.argv[1:] - args_that_override = fix_underscores(args_that_override) + args_that_override = self._handle_single_dash_addarg( + fix_underscores(args_that_override) + ) for i in range(len(args_that_override)): if args_that_override[i] in option_strings_dict: From 39086e1fbbd128b36753e34a9a8ab5d8a05f720e Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Wed, 20 Apr 2022 11:47:17 -0700 Subject: [PATCH 2/2] handle args during parsing --- parlai/core/params.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/parlai/core/params.py b/parlai/core/params.py index 82267fc94f8..38075c5747a 100644 --- a/parlai/core/params.py +++ b/parlai/core/params.py @@ -974,6 +974,19 @@ def add_extra_args(self, args=None): 'got an attribute error when parsing.' ) + def _handle_single_dash_parsearg(self, args, actions): + if _sys.version_info >= (3, 8, 0): + newargs = [] + for arg in args: + darg = f'-{arg}' + if arg.startswith('-') and not arg.startswith('--') and darg in actions: + newargs.append(darg) + else: + newargs.append(arg) + return newargs + else: + return args + def parse_known_args(self, args=None, namespace=None, nohelp=False): """ Parse known args to ignore help flag. @@ -987,16 +1000,7 @@ def parse_known_args(self, args=None, namespace=None, nohelp=False): actions = set() for action in self._actions: actions.update(action.option_strings) - if _sys.version_info >= (3, 8, 0): - newargs = [] - for arg in args: - darg = f'-{arg}' - if arg.startswith('-') and not arg.startswith('--') and darg in actions: - newargs.append(darg) - else: - newargs.append(arg) - args = newargs - + args = self._handle_single_dash_parsearg(args, actions) if nohelp: # ignore help args = [ @@ -1099,8 +1103,8 @@ def _process_args_to_opts(self, args_that_override: Optional[List[str]] = None): if args_that_override is None: args_that_override = _sys.argv[1:] - args_that_override = self._handle_single_dash_addarg( - fix_underscores(args_that_override) + args_that_override = self._handle_single_dash_parsearg( + fix_underscores(args_that_override), option_strings_dict.keys() ) for i in range(len(args_that_override)):