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.
This commit is contained in:
Scott Armitage 2018-07-09 15:49:30 -07:00
parent 15904d24c0
commit 3fe338a35a

View file

@ -3,6 +3,7 @@
from __future__ import print_function
import argparse
import copy
import functools
import glob
import logging
import logging.handlers
@ -263,13 +264,22 @@ def option_was_set(option, value):
return set_by_cli(option) or not has_default_value(option, value)
def argparse_list(cast, values):
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"):