try:
exe = basename(argv[0])
parser = ArgumentParseManager(exe)
- parsed, unparsed = parse_known_args(parser.parser, parser.arguments)
+ arguments = parser.arguments
+ #parsed, unparsed = parse_known_args(parser.parser, parser.arguments)
- if _arguments['version'].value:
+ if arguments['version'].value:
exit(0)
- _init_session(_arguments)
+ _init_session(arguments)
- if unparsed:
- _history = History(_arguments['config'].get('history', 'file'))
+ if parser.unparsed:
+ _history = History(arguments['config'].get('history', 'file'))
_history.add(' '.join([exe] + argv[1:]))
- one_cmd(parser.parser, unparsed, parser.arguments)
+ #one_cmd(parser.parser, unparsed, parser.arguments)
+ one_cmd(parser)
elif _help:
parser.parser.print_help()
_groups_help(_arguments)
"""Manage (initialize and update) an ArgumentParser object"""
parser = ArgumentParser(add_help=False)
- arguments = None
+ _arguments = None
+ _parser_modified = False
+ _parsed = None
+ _unparsed = None
def __init__(self, exe, arguments=None):
"""
:param arguments: (dict) if given, overrides the global _argument as
the parsers arguments specification
"""
+ self.syntax = '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe
if arguments:
self.arguments = arguments
else:
global _arguments
self.arguments = _arguments
- self.syntax = '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe
- self.update_parser()
@property
def syntax(self):
+ """The command syntax (useful for help messages, descriptions, etc)"""
return self.parser.prog
@syntax.setter
def syntax(self, new_syntax):
self.parser.prog = new_syntax
+ @property
+ def arguments(self):
+ """(dict) arguments the parser should be aware of"""
+ return self._arguments
+
+ @arguments.setter
+ def arguments(self, new_arguments):
+ if new_arguments:
+ assert isinstance(new_arguments, dict)
+ self._arguments = new_arguments
+ self.update_parser()
+
+ @property
+ def parsed(self):
+ """(ArgumentList) parser-matched terms"""
+ if self._parser_modified:
+ self.parse()
+ return self._parsed
+
+ @property
+ def unparsed(self):
+ """(list) parser-unmatched terms"""
+ if self._parser_modified:
+ self.parse()
+ return self._unparsed
+
def update_parser(self, arguments=None):
"""Load argument specifications to parser
:param arguments: if not given, update self.arguments instead
"""
if not arguments:
- arguments = self.arguments
+ arguments = self._arguments
for name, arg in arguments.items():
try:
arg.update_parser(self.parser, name)
+ self._parser_modified = True
except ArgumentError:
pass
+ def parse(self):
+ self._parsed, unparsed = self.parser.parse_known_args()
+ for name, arg in self.arguments.items():
+ arg.value = getattr(self._parsed, name, arg.default)
+ self._unparsed = []
+ for term in unparsed:
+ self._unparsed += split_input(' \'%s\' ' % term)
+ self._parser_modified = False
+
def parse_known_args(parser, arguments=None):
"""Fill in arguments from user input"""