Commit e0da0f90 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

rid of init_parser

parent 1d329d27
......@@ -36,8 +36,8 @@ from sys import argv, exit, stdout
from os.path import basename
from inspect import getargspec
from kamaki.cli.argument import _arguments, parse_known_args, init_parser,\
update_arguments
from kamaki.cli.argument import _arguments, parse_known_args, update_arguments
# init_parser,
from kamaki.cli.history import History
from kamaki.cli.utils import print_dict, print_list, red, magenta, yellow
from kamaki.cli.errors import CLIError
......@@ -409,11 +409,15 @@ def run_shell(exe_string, arguments):
shell.run(arguments)
from kamaki.cli.argument import ArgumentParseManager
def main():
try:
exe = basename(argv[0])
parser = init_parser(exe, _arguments)
parsed, unparsed = parse_known_args(parser, _arguments)
#parser = init_parser(exe, _arguments)
parser = ArgumentParseManager(exe)
parsed, unparsed = parse_known_args(parser.parser, parser.arguments)
if _arguments['version'].value:
exit(0)
......@@ -423,9 +427,9 @@ def main():
if unparsed:
_history = History(_arguments['config'].get('history', 'file'))
_history.add(' '.join([exe] + argv[1:]))
one_cmd(parser, unparsed, _arguments)
one_cmd(parser.parser, unparsed, parser.arguments)
elif _help:
parser.print_help()
parser.parser.print_help()
_groups_help(_arguments)
else:
run_shell(exe, _arguments)
......
......@@ -353,12 +353,59 @@ Mechanism:
"""
class ArgumentParseManager():
"""Manage (initialize and update) an ArgumentParser object"""
parser = ArgumentParser(add_help=False)
arguments = None
def __init__(self, exe, arguments=None):
"""
:param exe: (str) the basic command (e.g. 'kamaki')
:param arguments: (dict) if given, overrides the global _argument as
the parsers arguments specification
"""
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):
return self.parser.prog
@syntax.setter
def syntax(self, new_syntax):
self.parser.prog = new_syntax
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
for name, arg in arguments.items():
try:
arg.update_parser(self.parser, name)
except ArgumentError:
pass
"""
def init_parser(exe, arguments):
"""Create and initialize an ArgumentParser object"""
""Create and initialize an ArgumentParser object""
parser = ArgumentParser(add_help=False)
parser.prog = '%s <cmd_group> [<cmd_subbroup> ...] <cmd>' % exe
update_arguments(parser, arguments)
return parser
"""
def parse_known_args(parser, arguments=None):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment