On Mon, Aug 24, 2009 at 11:56:34AM +0200, Michael Hanselmann wrote:
>
> This can be used to generate the bash completion script automatically.
> In the future it may allow for better command line validation as well.
> ---
> lib/cli.py | 82
> ++++++++++++++++++++++++++++++++++----------------
> scripts/gnt-backup | 10 ++++--
> scripts/gnt-cluster | 48 ++++++++++++++++-------------
> scripts/gnt-debug | 8 ++---
> scripts/gnt-instance | 52 +++++++++++++++++--------------
> scripts/gnt-job | 29 ++++++++---------
> scripts/gnt-node | 70 +++++++++++++++++++++++++++----------------
> scripts/gnt-os | 4 +-
> 8 files changed, 180 insertions(+), 123 deletions(-)
>
> diff --git a/lib/cli.py b/lib/cli.py
> index 8065f3d..5745a56 100644
> --- a/lib/cli.py
> +++ b/lib/cli.py
> @@ -46,7 +46,6 @@ __all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT",
> "GenericMain",
> "SubmitOpCode", "GetClient",
> "cli_option",
> "GenerateTable", "AskUser",
> - "ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE",
> "USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT", "SUBMIT_OPT",
> "ListTags", "AddTags", "RemoveTags", "TAG_SRC_OPT",
> "FormatError", "SplitNodeOption", "SubmitOrSend",
> @@ -279,21 +278,6 @@ _DRY_RUN_OPT = make_option("--dry-run", default=False,
> " check steps and verify it it could be executed")
>
>
> -def ARGS_FIXED(val):
> - """Macro-like function denoting a fixed number of arguments"""
> - return -val
> -
> -
> -def ARGS_ATLEAST(val):
> - """Macro-like function denoting a minimum number of arguments"""
> - return val
> -
> -
> -ARGS_NONE = None
> -ARGS_ONE = ARGS_FIXED(1)
> -ARGS_ANY = ARGS_ATLEAST(0)
Please do not remove these - at least not ARGS_NONE, which should become
[] and thus a lot of the code churn could be eliminated.
As for the others, I think (looking at the changes in scripts/gnt-*)
that having usual arguments for ARGS_INSTANCE_ONE and ARGS_NODE_ONE
would be really helpful.
> -
> -
> def check_unit(option, opt, value):
> """OptParsers custom converter for units.
>
> @@ -464,27 +448,73 @@ def _ParseArgs(argv, commands, aliases):
>
> cmd = aliases[cmd]
>
> - func, nargs, parser_opts, usage, description = commands[cmd]
> + func, args_def, parser_opts, usage, description = commands[cmd]
> parser = OptionParser(option_list=parser_opts + [_DRY_RUN_OPT],
> description=description,
> formatter=TitledHelpFormatter(),
> usage="%%prog %s %s" % (cmd, usage))
> parser.disable_interspersed_args()
> options, args = parser.parse_args()
> - if nargs is None:
> - if len(args) != 0:
> - ToStderr("Error: Command %s expects no arguments", cmd)
> - return None, None, None
> - elif nargs < 0 and len(args) != -nargs:
> - ToStderr("Error: Command %s expects %d argument(s)", cmd, -nargs)
> - return None, None, None
> - elif nargs >= 0 and len(args) < nargs:
> - ToStderr("Error: Command %s expects at least %d argument(s)", cmd, nargs)
> +
> + if not _CheckArguments(cmd, args_def, args):
> return None, None, None
>
> return func, options, args
>
>
> +def _CheckArguments(cmd, args_def, args):
> + """Verifies the arguments using the argument definition.
> +
> + """
Would you mind please expanding the docstring to detail the algorithm
you use below?
iustin