Revision b3dd8f4b kamaki/cli/argument.py
b/kamaki/cli/argument.py | ||
---|---|---|
357 | 357 |
"""Manage (initialize and update) an ArgumentParser object""" |
358 | 358 |
|
359 | 359 |
parser = ArgumentParser(add_help=False) |
360 |
arguments = None |
|
360 |
_arguments = None |
|
361 |
_parser_modified = False |
|
362 |
_parsed = None |
|
363 |
_unparsed = None |
|
361 | 364 |
|
362 | 365 |
def __init__(self, exe, arguments=None): |
363 | 366 |
""" |
... | ... | |
366 | 369 |
:param arguments: (dict) if given, overrides the global _argument as |
367 | 370 |
the parsers arguments specification |
368 | 371 |
""" |
372 |
self.syntax = '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe |
|
369 | 373 |
if arguments: |
370 | 374 |
self.arguments = arguments |
371 | 375 |
else: |
372 | 376 |
global _arguments |
373 | 377 |
self.arguments = _arguments |
374 | 378 |
|
375 |
self.syntax = '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe |
|
376 |
self.update_parser() |
|
377 | 379 |
|
378 | 380 |
@property |
379 | 381 |
def syntax(self): |
382 |
"""The command syntax (useful for help messages, descriptions, etc)""" |
|
380 | 383 |
return self.parser.prog |
381 | 384 |
|
382 | 385 |
@syntax.setter |
383 | 386 |
def syntax(self, new_syntax): |
384 | 387 |
self.parser.prog = new_syntax |
385 | 388 |
|
389 |
@property |
|
390 |
def arguments(self): |
|
391 |
"""(dict) arguments the parser should be aware of""" |
|
392 |
return self._arguments |
|
393 |
|
|
394 |
@arguments.setter |
|
395 |
def arguments(self, new_arguments): |
|
396 |
if new_arguments: |
|
397 |
assert isinstance(new_arguments, dict) |
|
398 |
self._arguments = new_arguments |
|
399 |
self.update_parser() |
|
400 |
|
|
401 |
@property |
|
402 |
def parsed(self): |
|
403 |
"""(ArgumentList) parser-matched terms""" |
|
404 |
if self._parser_modified: |
|
405 |
self.parse() |
|
406 |
return self._parsed |
|
407 |
|
|
408 |
@property |
|
409 |
def unparsed(self): |
|
410 |
"""(list) parser-unmatched terms""" |
|
411 |
if self._parser_modified: |
|
412 |
self.parse() |
|
413 |
return self._unparsed |
|
414 |
|
|
386 | 415 |
def update_parser(self, arguments=None): |
387 | 416 |
"""Load argument specifications to parser |
388 | 417 |
|
389 | 418 |
:param arguments: if not given, update self.arguments instead |
390 | 419 |
""" |
391 | 420 |
if not arguments: |
392 |
arguments = self.arguments |
|
421 |
arguments = self._arguments
|
|
393 | 422 |
|
394 | 423 |
for name, arg in arguments.items(): |
395 | 424 |
try: |
396 | 425 |
arg.update_parser(self.parser, name) |
426 |
self._parser_modified = True |
|
397 | 427 |
except ArgumentError: |
398 | 428 |
pass |
399 | 429 |
|
430 |
def parse(self): |
|
431 |
self._parsed, unparsed = self.parser.parse_known_args() |
|
432 |
for name, arg in self.arguments.items(): |
|
433 |
arg.value = getattr(self._parsed, name, arg.default) |
|
434 |
self._unparsed = [] |
|
435 |
for term in unparsed: |
|
436 |
self._unparsed += split_input(' \'%s\' ' % term) |
|
437 |
self._parser_modified = False |
|
438 |
|
|
400 | 439 |
|
401 | 440 |
def parse_known_args(parser, arguments=None): |
402 | 441 |
"""Fill in arguments from user input""" |
Also available in: Unified diff