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():
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))
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:
self._push_in_command(subcmd.path)
self.set_prompt(intro)
+
self.cmdloop()
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:
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: