From 392e3730cac793be6ef82535b022a6b24ae113bb Mon Sep 17 00:00:00 2001 From: Lvov Maxim Date: Thu, 9 Jun 2011 22:02:01 +0400 Subject: [PATCH] parse options with optparse, options prepended '--' --- bin/nova-manage | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bin/nova-manage b/bin/nova-manage index 8faf072b..76bed16f 100755 --- a/bin/nova-manage +++ b/bin/nova-manage @@ -62,6 +62,8 @@ import sys import time import IPy +from inspect import getargspec +from optparse import OptionParser # If ../nova/__init__.py exists, add ../ to Python search path, so that # it will override what happens to be installed in /usr/(local/)lib/python... @@ -1133,14 +1135,18 @@ def main(): matches = lazy_match(action, actions) action, fn = matches[0] - fn_args, fn_kwargs = [], {} - for arg in argv: - if '=' in arg: - key, value = arg.split('=') - fn_kwargs[key] = value - else: - fn_args.append(arg) - + func_args = getargspec(fn).args + parser = OptionParser() + for arg in func_args: + dasharg = "--%s" % arg + parser.add_option(dasharg) + + (opts, fn_args) = parser.parse_args(argv) + fn_kwargs = vars(opts) + for k, v in fn_kwargs.items(): + if v is None: + del fn_kwargs[k] + # call the action with the remaining arguments try: fn(*fn_args, **fn_kwargs)