diff --git a/lib/isg/argparser.py b/lib/isg/argparser.py index 0eef12537a0a4432f997931de8ece46b49b8e960..fdc18d02f007bf00e82637fe6482feed78a7145f 100644 --- a/lib/isg/argparser.py +++ b/lib/isg/argparser.py @@ -2,9 +2,10 @@ import argparse -def simple(description, arg=None, arg_help='', opts=dict(), noopts=dict()): + +def extendable(description, arg=None, arg_help='', opts=dict(), noopts=dict()): """ - Get cmd argument with simplified argparser + Get extendable simplified argparser """ parser = argparse.ArgumentParser( add_help=False, description=description) @@ -15,18 +16,39 @@ def simple(description, arg=None, arg_help='', opts=dict(), noopts=dict()): for k, v in opts.items(): if isinstance(v, list): v = ', '.join(v) - parser.add_argument('-' + k[:1], '--' + k, dest=k, - action='store_const', const=True, - help='Show ' + v) + + try: + parser.add_argument('-' + k[:1], '--' + k, dest=k, + action='store_const', const=True, + help='Show ' + v) + except argparse.ArgumentError: + parser.add_argument('--' + k, dest=k, + action='store_const', const=True, + help='Show ' + v) for k, v in noopts.items(): if isinstance(v, list): v = ', '.join(v) - parser.add_argument('-' + k[3:4].upper(), '--' + k, dest=k, - action='store_const', const=True, - help='Hide ' + v) + + try: + parser.add_argument('-' + k[3:4].upper(), '--' + k, dest=k, + action='store_const', const=True, + help='Hide ' + v) + except argparse.ArgumentError: + parser.add_argument('--' + k, dest=k, + action='store_const', const=True, + help='Hide ' + v) parser.add_argument('--help', action='help', help='Show this help message and exit') + return parser + + +def simple(description, arg=None, arg_help='', opts=dict(), noopts=dict()): + """ + Get cmd argument with simplified argparser + """ + parser = extendable(description, arg=arg, arg_help=arg_help, opts=opts, noopts=noopts) + return vars(parser.parse_args())