Commit db950b10 authored by Stavros Sachtouris's avatar Stavros Sachtouris

Use CLIErrors in shell

parent 87565d2c
......@@ -36,10 +36,11 @@ from os import popen
from sys import stdout
from argparse import ArgumentParser
from kamaki.cli import _exec_cmd
from kamaki.cli import _exec_cmd, _print_error_message
from kamaki.cli.argument import _arguments, update_arguments
from kamaki.cli.utils import print_dict
from kamaki.cli.history import History
from kamaki.cli.errors import CLIError
def _fix_arguments():
......@@ -137,7 +138,10 @@ class Shell(Cmd):
for name, arg in instance.arguments.items():
arg.value = getattr(parsed, name, arg.default)
_exec_cmd(instance, unparsed, cmd_parser.print_help)
try:
_exec_cmd(instance, unparsed, cmd_parser.print_help)
except CLIError as err:
_print_error_message(err)
elif ('-h' in cmd_args or '--help' in cmd_args) \
or len(cmd_args): # print options
print('%s: %s' % (subname, subcmd.help))
......@@ -190,7 +194,7 @@ class Shell(Cmd):
def run(self, path=''):
self._history = History(_arguments['config'].get('history', 'file'))
if len(path):
if path:
cmd = self.cmd_tree.get_command(path)
intro = cmd.path.replace('_', ' ')
else:
......@@ -200,4 +204,5 @@ class Shell(Cmd):
self._push_in_command(subcmd.path)
self.set_prompt(intro)
self.cmdloop()
......@@ -41,6 +41,7 @@ from kamaki.cli.commands import _command_init
history_cmds = CommandTree('history', 'Command history')
_commands = [history_cmds]
class _init_history(_command_init):
......
......@@ -73,8 +73,9 @@ def print_dict(d, exclude=(), ident=0):
if not isinstance(d, dict):
raise CLIError(message='Cannot dict_print a non-dict object')
margin = max(len(unicode(key).strip())\
for key in d.keys() if key not in exclude)
if d:
margin = max(len(unicode(key).strip())\
for key in d.keys() if key not in exclude)
for key, val in sorted(d.items()):
if key in exclude:
......@@ -97,8 +98,9 @@ def print_list(l, exclude=(), ident=0):
if not isinstance(l, list):
raise CLIError(message='Cannot list_print a non-list object')
margin = max(len(unicode(item).strip())\
for item in l if item not in exclude)
if l:
margin = max(len(unicode(item).strip())\
for item in l if item not in exclude)
for item in sorted(l):
if item in exclude:
......
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