diff options
author | kmvanbrunt <kmvanbrunt@gmail.com> | 2018-12-03 13:15:24 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-03 13:15:24 -0500 |
commit | fb3486fae7df048c2ff9302ee9bf87fbe43461fb (patch) | |
tree | b18104ff9fb35bf46beba1b52a9af2fcb42c8826 /cmd2/cmd2.py | |
parent | c3a7380314067f63d62b85710cf5643fdb7347f9 (diff) | |
parent | a73a3c4020dbf02ec00ceb77c5f3677d8bc94d3c (diff) | |
download | cmd2-git-fb3486fae7df048c2ff9302ee9bf87fbe43461fb.tar.gz |
Merge pull request #604 from python-cmd2/tweaks
Made it so default_to_shell results in do_shell being called so that output can be captured
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 2a617a75..012b6f6b 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -632,7 +632,7 @@ class Cmd(cmd.Cmd): err_msg = err_color + err_msg + Fore.RESET self.decolorized_write(sys.stderr, err_msg) - if traceback_war: + if traceback_war and not self.debug: war = "To enable full traceback, run the following command: 'set debug true'\n" war = war_color + war + Fore.RESET self.decolorized_write(sys.stderr, war) @@ -1893,6 +1893,8 @@ class Cmd(cmd.Cmd): try: self.pipe_proc = subprocess.Popen(statement.pipe_to, stdin=subproc_stdin) except Exception as ex: + self.perror('Not piping because - {}'.format(ex), traceback_war=False) + # Restore stdout to what it was and close the pipe self.stdout.close() subproc_stdin.close() @@ -1901,8 +1903,6 @@ class Cmd(cmd.Cmd): self.kept_state = None self.redirecting = False - # Re-raise the exception - raise ex elif statement.output: import tempfile if (not statement.output_to) and (not self.can_clip): @@ -1920,7 +1920,7 @@ class Cmd(cmd.Cmd): try: sys.stdout = self.stdout = open(statement.output_to, mode) except OSError as ex: - self.perror('Not Redirecting because - {}'.format(ex), traceback_war=False) + self.perror('Not redirecting because - {}'.format(ex), traceback_war=False) self.redirecting = False else: # going to a paste buffer @@ -2007,8 +2007,10 @@ class Cmd(cmd.Cmd): stop = func(statement) else: - self.default(statement) - stop = False + stop = self.default(statement) + + if stop is None: + stop = False return stop @@ -2056,19 +2058,18 @@ class Cmd(cmd.Cmd): # Run the resolved command return self.onecmd_plus_hooks(resolved) - def default(self, statement: Statement) -> None: + def default(self, statement: Statement) -> Optional[bool]: """Executed when the command given isn't a recognized command implemented by a do_* method. :param statement: Statement object with parsed input """ if self.default_to_shell: - result = os.system(statement.command_and_args) - # If os.system() succeeded, then don't print warning about unknown command - if not result: - return + if 'shell' not in self.exclude_from_history: + self.history.append(statement.raw) - # Print out a message stating this is an unknown command - self.poutput('*** Unknown syntax: {}\n'.format(statement.command_and_args)) + return self.do_shell(statement.command_and_args) + else: + self.poutput('*** {} is not a recognized command, alias, or macro\n'.format(statement.command)) def pseudo_raw_input(self, prompt: str) -> str: """Began life as a copy of cmd's cmdloop; like raw_input but @@ -2803,7 +2804,7 @@ class Cmd(cmd.Cmd): :param args: argparse parsed arguments from the set command :param parameter: optional search parameter """ - param = parameter.strip().lower() + param = utils.norm_fold(parameter.strip()) result = {} maxlen = 0 @@ -2844,7 +2845,7 @@ class Cmd(cmd.Cmd): # Check if param was passed in if not args.param: return self.show(args) - param = args.param.strip().lower() + param = utils.norm_fold(args.param.strip()) # Check if value was passed in if not args.value: @@ -3156,15 +3157,19 @@ class Cmd(cmd.Cmd): history_parser_group.add_argument('-e', '--edit', action='store_true', help='edit and then run selected history items') history_parser_group.add_argument('-s', '--script', action='store_true', help='output commands in script format') - history_parser_group.add_argument('-o', '--output-file', metavar='FILE', help='output commands to a script file') - history_parser_group.add_argument('-t', '--transcript', help='output commands and results to a transcript file') + setattr(history_parser_group.add_argument('-o', '--output-file', metavar='FILE', + help='output commands to a script file'), + ACTION_ARG_CHOICES, ('path_complete',)) + setattr(history_parser_group.add_argument('-t', '--transcript', + help='output commands and results to a transcript file'), + ACTION_ARG_CHOICES, ('path_complete',)) history_parser_group.add_argument('-c', '--clear', action="store_true", help='clear all history') - _history_arg_help = """empty all history items -a one history item by number -a..b, a:b, a:, ..b items by indices (inclusive) -[string] items containing string -/regex/ items matching regular expression""" - history_parser.add_argument('arg', nargs='?', help=_history_arg_help) + history_arg_help = ("empty all history items\n" + "a one history item by number\n" + "a..b, a:b, a:, ..b items by indices (inclusive)\n" + "string items containing string\n" + "/regex/ items matching regular expression") + history_parser.add_argument('arg', nargs='?', help=history_arg_help) @with_argparser(history_parser) def do_history(self, args: argparse.Namespace) -> None: @@ -3851,7 +3856,6 @@ class History(list): end = int(end) return self[start:end] - # noinspection PyUnresolvedReferences getme = getme.strip() if getme.startswith(r'/') and getme.endswith(r'/'): @@ -3871,7 +3875,7 @@ class History(list): :param hi: HistoryItem :return: bool - True if search matches """ - return getme.lower() in hi.lowercase + return utils.norm_fold(getme) in utils.norm_fold(hi) return [itm for itm in self if isin(itm)] |