make default values for optional_multiple parameters work

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2013-12-19 23:33:43 +01:00
parent 7d46156fd6
commit 67f61eb7ec

View file

@ -37,6 +37,21 @@ class MissingRequiredEnvironmentVariableError(cdist.Error):
return self.message 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): class Emulator(object):
def __init__(self, argv, stdin=sys.stdin.buffer, env=os.environ): def __init__(self, argv, stdin=sys.stdin.buffer, env=os.environ):
self.argv = argv self.argv = argv
@ -101,7 +116,7 @@ class Emulator(object):
for parameter in self.cdist_type.optional_multiple_parameters: for parameter in self.cdist_type.optional_multiple_parameters:
argument = "--" + parameter argument = "--" + parameter
parser.add_argument(argument, dest=parameter, action='append', required=False, 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: for parameter in self.cdist_type.boolean_parameters:
argument = "--" + parameter argument = "--" + parameter
parser.add_argument(argument, dest=parameter, action='store_const', const='') parser.add_argument(argument, dest=parameter, action='store_const', const='')