Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
kamaki
Commits
e0da0f90
Commit
e0da0f90
authored
Dec 06, 2012
by
Stavros Sachtouris
Browse files
rid of init_parser
parent
1d329d27
Changes
2
Hide whitespace changes
Inline
Side-by-side
kamaki/cli/__init__.py
View file @
e0da0f90
...
...
@@ -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
)
...
...
kamaki/cli/argument.py
View file @
e0da0f90
...
...
@@ -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
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment