class ArgumentParseManager(object):
"""Manage (initialize and update) an ArgumentParser object"""
- def __init__(self, exe, arguments=None, required=None):
+ def __init__(
+ self, exe,
+ arguments=None, required=None, syntax=None, description=None):
"""
:param exe: (str) the basic command (e.g. 'kamaki')
['b', 'c']] means that the command required either 'a' and 'b' or
'a' and 'c' or at least one of 'b', 'c' and could be written as
[('a', ['b', 'c']), ['b', 'c']]
+ :param syntax: (str) The basic syntax of the arguments. Default:
+ exe <cmd_group> [<cmd_subbroup> ...] <cmd>
+ :param description: (str) The description of the commands or ''
"""
self.parser = ArgumentParser(
add_help=False, formatter_class=RawDescriptionHelpFormatter)
self._exe = exe
- self.syntax = '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe
+ self.syntax = syntax or (
+ '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe)
self.required = required
+ self.parser.description = description or ''
if arguments:
self.arguments = arguments
else:
pkargs = (new_args,) if new_args else ()
self._parsed, unparsed = self.parser.parse_known_args(*pkargs)
pdict = vars(self._parsed)
- diff = set(self.required or []).difference([
- k for k in pdict if pdict[k] != None])
+ diff = set(self.required or []).difference(
+ [k for k in pdict if pdict[k] not in (None, )])
if diff:
self.print_help()
miss = ['/'.join(self.arguments[k].parsed_name) for k in diff]
tmp_args.pop('verbose', None)
tmp_args.pop('silent', None)
tmp_args.pop('config', None)
- help_parser = ArgumentParseManager(cmd_name, tmp_args, required)
- help_parser.parser.description = descr
- help_parser.syntax = syntax
- #return help_parser.parser.print_help
+ help_parser = ArgumentParseManager(
+ cmd_name, tmp_args, required, syntax=syntax, description=descr)
return help_parser.print_help
def _register_command(self, cmd_path):
self.auth_base, self.cloud)
cmd_parser.update_arguments(instance.arguments)
cmd_parser.arguments = instance.arguments
+ subpath = subcmd.path.split('_')[
+ (len(cmd.path.split('_')) - 1):]
cmd_parser.syntax = '%s %s' % (
- subcmd.path.replace('_', ' '), cls.syntax)
+ ' '.join(subpath), instance.syntax)
help_method = self._create_help_method(
- cmd.name, cmd_parser.arguments, cmd_parser.required,
- subcmd.help, cmd_parser.syntax)
+ cmd.name, cmd_parser.arguments,
+ cmd_parser.required, subcmd.help, cmd_parser.syntax)
if '-h' in cmd_args or '--help' in cmd_args:
help_method()
if ldescr.strip():