From 67f61eb7ec12745b9ab7c87eb014ee7fae96a0a7 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Thu, 19 Dec 2013 23:33:43 +0100 Subject: [PATCH] make default values for optional_multiple parameters work Signed-off-by: Steven Armstrong --- cdist/emulator.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cdist/emulator.py b/cdist/emulator.py index b1cd8f2d..1e530fec 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -37,6 +37,21 @@ class MissingRequiredEnvironmentVariableError(cdist.Error): return self.message +class DefaultList(list): + """Helper class to allow default values for optional_multiple parameters. + + @see https://groups.google.com/forum/#!msg/comp.lang.python/sAUvkJEDpRc/RnRymrzJVDYJ + """ + def __copy__(self): + return [] + + @classmethod + def create(cls, initial=None): + if initial: + initial = initial.split('\n') + return cls(initial) + + class Emulator(object): def __init__(self, argv, stdin=sys.stdin.buffer, env=os.environ): self.argv = argv @@ -101,7 +116,7 @@ class Emulator(object): for parameter in self.cdist_type.optional_multiple_parameters: argument = "--" + parameter parser.add_argument(argument, dest=parameter, action='append', required=False, - default=self.cdist_type.parameter_defaults.get(parameter, None)) + default=DefaultList.create(self.cdist_type.parameter_defaults.get(parameter, None))) for parameter in self.cdist_type.boolean_parameters: argument = "--" + parameter parser.add_argument(argument, dest=parameter, action='store_const', const='')