From 3fe338a35a20fc809d07b72052d50f96e2b24f9e Mon Sep 17 00:00:00 2001 From: Scott Armitage Date: Mon, 9 Jul 2018 15:49:30 -0700 Subject: [PATCH] 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. --- certbot/cli.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/certbot/cli.py b/certbot/cli.py index 2a4dda360..498d2d73a 100644 --- a/certbot/cli.py +++ b/certbot/cli.py @@ -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"):