def loop(self, inhibit_exceptions=False, with_backtrace=False): """Repeatedly read and execute commands from the user. Arguments: ``inhibit_exceptions=True``: `boolean` Normally, ``interact_loop`` will pass exceptions back to the caller for handling. Setting this to ``True`` will cause an error message to be printed, but interaction will continue. ``with_backtrace=False``: `boolean` Whether to print a full backtrace when ``inhibit_exceptions=True``. """ try: while True: try: if not self.once(): break except Exception as e: if inhibit_exceptions: if with_backtrace: import traceback console.cerror(traceback.format_exc()) else: console.cerror('error: %s' % e) else: raise finally: self.write_history()
def _cli_help(key, count): try: command = readline.get_line_buffer()[:cly.rlext.cursor()] context = Interact._parser.parse(command) if context.remaining.strip(): print() candidates = [help[1] for help in context.help()] text = '%s^ invalid token (candidates are %s)' % \ (' ' * (context.cursor + len(Interact.prompt)), ', '.join(candidates)) console.cerror(text) cly.rlext.force_redisplay() return help = context.help() print() help.format(sys.stdout) cly.rlext.force_redisplay() return 0 except Exception as e: Interact._dump_traceback(e) cly.rlext.force_redisplay() return 0
def _show_help(self, key, count): try: command = readline.get_line_buffer()[:self.cursor] context = self.parser.parse(command) if context.remaining.strip(): print candidates = [help[1] for help in context.help()] text = '%s^ invalid token (candidates are %s)' % \ (' ' * (context.cursor + len(self.prompt)), ', '.join(candidates)) console.cerror(text) self._force_redisplay() return help = context.help() print console.cprint('\n'.join(help.format())) self._force_redisplay() return 0 except Exception, e: Interact.dump_traceback(e) self._force_redisplay() return 0
def error_at_cursor(self, context, text): """Attempt to intelligently print an error at the current cursor offset.""" text = str(text) term_width = console.termwidth() indent = ' ' * (context.cursor % term_width + len(self.prompt)) if len(indent + text) > term_width: console.cerror(indent + '^') console.cerror(text) else: console.cerror(indent + '^ ' + text)
def brief_exceptions(interact, context, completing, e): """Display the string summary for exceptions.""" if not completing: console.cerror(str(e))