Compare commits

...

3 Commits

Author SHA1 Message Date
Scott Armitage
a09be984e1 Add function docstring to appease lint 2018-07-09 16:22:48 -07:00
Scott Armitage
3fe338a35a Wrap argparse_type in list if nargs produces one
In argparse, type casting is performed on individual arguments, which
may or may not be aggregated into a list depending on other factors,
normally the value given to `nargs`. When `nargs` is one of the values
that causes argparse to return a list, return a wrapping function that
will serialize an iterable, calling `action.type` (or default `str`)
on each element of the iterable as opposed to on the iterable itself.
2018-07-09 15:49:30 -07:00
Scott Armitage
15904d24c0 Fix detect defaults when nargs present
If a plugin adds a command line argument with the `nargs` parameter, e.g. `nargs='+'`, this parameter gets filtered from the `HelpfulArgumentParser` created/used in default detection. As a result, this parser does not parse the command line in the expected way, instead using the default value for `nargs`.

Leaving `nargs` in place resolves this issue.
2018-07-09 15:29:20 -07:00

View File

@@ -3,6 +3,7 @@
from __future__ import print_function
import argparse
import copy
import functools
import glob
import logging
import logging.handlers
@@ -120,7 +121,7 @@ More detailed help:
# These argparse parameters should be removed when detecting defaults.
ARGPARSE_PARAMS_TO_REMOVE = ("const", "nargs", "type",)
ARGPARSE_PARAMS_TO_REMOVE = ("const", "type",)
# These sets are used when to help detect options set by the user.
@@ -263,13 +264,23 @@ def option_was_set(option, value):
return set_by_cli(option) or not has_default_value(option, value)
def argparse_list(cast, values):
"""Wrap an argparse type function in a list (default: str)"""
if cast is None:
cast = str
return [cast(value) for value in values]
def argparse_type(variable):
"""Return our argparse type function for a config variable (default: str)"""
# pylint: disable=protected-access
if helpful_parser is not None:
for action in helpful_parser.parser._actions:
if action.type is not None and action.dest == variable:
return action.type
if action.dest == variable:
if action.nargs in ['N', '*', '+', argparse.REMAINDER]:
return functools.partial(argparse_list, action.type)
if action.type is not None:
return action.type
return str
def read_file(filename, mode="rb"):