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
b3dd8f4b
Commit
b3dd8f4b
authored
Dec 06, 2012
by
Stavros Sachtouris
Browse files
Design ArgumentParseManager, implement fnctionlity
parent
7dbd52f5
Changes
2
Hide whitespace changes
Inline
Side-by-side
kamaki/cli/__init__.py
View file @
b3dd8f4b
...
...
@@ -415,17 +415,19 @@ def main():
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
)
...
...
kamaki/cli/argument.py
View file @
b3dd8f4b
...
...
@@ -357,7 +357,10 @@ class ArgumentParseManager():
"""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
):
"""
...
...
@@ -366,37 +369,73 @@ class ArgumentParseManager():
: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"""
...
...
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