mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
145: Added new CLI.parse() method; added corresponding unit tests
This commit is contained in:
@@ -97,7 +97,9 @@ class CLI(object):
|
|||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
self.run_cmd(cmd, sys.argv[2:])
|
self.run_cmd(cmd, sys.argv[2:])
|
||||||
|
|
||||||
def run_cmd(self, cmd, args):
|
def run_cmd(self, cmd, given):
|
||||||
|
print self.parse(given)
|
||||||
|
sys.exit(0)
|
||||||
kw = dict(self.parse_kw(args))
|
kw = dict(self.parse_kw(args))
|
||||||
self[cmd](**kw)
|
self[cmd](**kw)
|
||||||
|
|
||||||
@@ -110,6 +112,18 @@ class CLI(object):
|
|||||||
m.group(2),
|
m.group(2),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def parse(self, given):
|
||||||
|
args = []
|
||||||
|
kw = {}
|
||||||
|
for g in given:
|
||||||
|
m = re.match(r'^--([a-z][-a-z0-9]*)=(.+)$', g)
|
||||||
|
if m:
|
||||||
|
kw[from_cli(m.group(1))] = m.group(2)
|
||||||
|
else:
|
||||||
|
args.append(g)
|
||||||
|
return (args, kw)
|
||||||
|
|
||||||
|
|
||||||
def __get_mcl(self):
|
def __get_mcl(self):
|
||||||
"""
|
"""
|
||||||
Returns the Max Command Length.
|
Returns the Max Command Length.
|
||||||
|
|||||||
@@ -98,6 +98,23 @@ class test_CLI(ClassChecker):
|
|||||||
args = tuple('--%s=%s' % (cli.to_cli(k), v) for (k,v) in kw.items())
|
args = tuple('--%s=%s' % (cli.to_cli(k), v) for (k,v) in kw.items())
|
||||||
assert dict(o.parse_kw(args)) == kw
|
assert dict(o.parse_kw(args)) == kw
|
||||||
|
|
||||||
|
def test_parse(self):
|
||||||
|
"""
|
||||||
|
Tests the `parse` method.
|
||||||
|
"""
|
||||||
|
o = self.cls(None)
|
||||||
|
args = ['hello', 'naughty', 'nurse']
|
||||||
|
kw = dict(
|
||||||
|
first_name='Naughty',
|
||||||
|
last_name='Nurse',
|
||||||
|
)
|
||||||
|
opts = ['--%s=%s' % (k.replace('_', '-'), v) for (k, v) in kw.items()]
|
||||||
|
assert o.parse(args + []) == (args, {})
|
||||||
|
assert o.parse(opts + []) == ([], kw)
|
||||||
|
assert o.parse(args + opts) == (args, kw)
|
||||||
|
assert o.parse(opts + args) == (args, kw)
|
||||||
|
|
||||||
|
|
||||||
def test_mcl(self):
|
def test_mcl(self):
|
||||||
"""
|
"""
|
||||||
Tests the `mcl` (Max Command Length) property .
|
Tests the `mcl` (Max Command Length) property .
|
||||||
|
|||||||
Reference in New Issue
Block a user