From 499eb088b45d481857e2a0ae80d15ce4886710bd Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Thu, 29 Dec 2011 16:18:11 +0100 Subject: [PATCH] Add a new CLI option type 'list' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This simply splits the value in the option parser, instead of needing to do it in the client code. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: RenΓ© Nussbaumer <rn@google.com> --- lib/cli.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/cli.py b/lib/cli.py index ebf91c819..be68a4e09 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -582,6 +582,18 @@ def check_bool(option, opt, value): # pylint: disable=W0613 raise errors.ParameterError("Invalid boolean value '%s'" % value) +def check_list(option, opt, value): # pylint: disable=W0613 + """Custom parser for comma-separated lists. + + """ + # we have to make this explicit check since "".split(",") is [""], + # not an empty list :( + if not value: + return [] + else: + return utils.UnescapeAndSplit(value) + + # completion_suggestion is normally a list. Using numeric values not evaluating # to False for dynamic completion. (OPT_COMPL_MANY_NODES, @@ -615,12 +627,14 @@ class CliOption(Option): "keyval", "unit", "bool", + "list", ) TYPE_CHECKER = Option.TYPE_CHECKER.copy() TYPE_CHECKER["identkeyval"] = check_ident_key_val TYPE_CHECKER["keyval"] = check_key_val TYPE_CHECKER["unit"] = check_unit TYPE_CHECKER["bool"] = check_bool + TYPE_CHECKER["list"] = check_list # optparse.py sets make_option, so we do it for our own option class, too -- GitLab