diff options
-rw-r--r-- | cmd2/cmd2.py | 17 | ||||
-rw-r--r-- | cmd2/parsing.py | 70 | ||||
-rw-r--r-- | tests/conftest.py | 12 |
3 files changed, 52 insertions, 47 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 993162d1..d197e75e 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3331,18 +3331,21 @@ class Cmd(cmd.Cmd): help='output commands to a script file, implies -s'), ACTION_ARG_CHOICES, ('path_complete',)) setattr(history_action_group.add_argument('-t', '--transcript', - help='output commands and results to a transcript file, implies -s'), + help='output commands and results to a transcript file,\n' + 'implies -s'), ACTION_ARG_CHOICES, ('path_complete',)) history_action_group.add_argument('-c', '--clear', action='store_true', help='clear all history') history_format_group = history_parser.add_argument_group(title='formatting') - history_script_help = 'output commands in script format, i.e. without command numbers' - history_format_group.add_argument('-s', '--script', action='store_true', help=history_script_help) - history_expand_help = 'output expanded commands instead of entered command' - history_format_group.add_argument('-x', '--expanded', action='store_true', help=history_expand_help) + history_format_group.add_argument('-s', '--script', action='store_true', + help='output commands in script format, i.e. without command\n' + 'numbers') + history_format_group.add_argument('-x', '--expanded', action='store_true', + help='output fully parsed commands with any aliases and\n' + 'macros expanded, instead of typed commands') history_format_group.add_argument('-v', '--verbose', action='store_true', - help='display history and include expanded commands if they' - ' differ from the typed command') + help='display history and include expanded commands if they\n' + 'differ from the typed command') history_arg_help = ("empty all history items\n" "a one history item by number\n" diff --git a/cmd2/parsing.py b/cmd2/parsing.py index 306d3bdd..067c9bf4 100644 --- a/cmd2/parsing.py +++ b/cmd2/parsing.py @@ -453,56 +453,54 @@ class StatementParser: arg_list = tokens[1:] tokens = [] - # check for a pipe to a shell process - # if there is a pipe, everything after the pipe needs to be passed - # to the shell, even redirected output - # this allows '(Cmd) say hello | wc > countit.txt' + pipe_to = '' + output = '' + output_to = '' + + # Find which redirector character appears first in the command + try: + pipe_index = tokens.index(constants.REDIRECTION_PIPE) + except ValueError: + pipe_index = len(tokens) + try: - # find the first pipe if it exists - pipe_pos = tokens.index(constants.REDIRECTION_PIPE) + redir_index = tokens.index(constants.REDIRECTION_OUTPUT) + except ValueError: + redir_index = len(tokens) + + try: + append_index = tokens.index(constants.REDIRECTION_APPEND) + except ValueError: + append_index = len(tokens) + + # Check if output should be piped to a shell command + if pipe_index < redir_index and pipe_index < append_index: # Get the tokens for the pipe command and expand ~ where needed - pipe_to_tokens = tokens[pipe_pos + 1:] + pipe_to_tokens = tokens[pipe_index + 1:] utils.expand_user_in_tokens(pipe_to_tokens) # Build the pipe command line string pipe_to = ' '.join(pipe_to_tokens) # remove all the tokens after the pipe - tokens = tokens[:pipe_pos] - except ValueError: - pipe_to = '' + tokens = tokens[:pipe_index] - # check for output redirect - output = '' - output_to = '' - try: - output_pos = tokens.index(constants.REDIRECTION_OUTPUT) - output = constants.REDIRECTION_OUTPUT + # Check for output redirect/append + elif redir_index != append_index: + if redir_index < append_index: + output = constants.REDIRECTION_OUTPUT + output_index = redir_index + else: + output = constants.REDIRECTION_APPEND + output_index = append_index - # Check if we are redirecting to a file - if len(tokens) > output_pos + 1: - unquoted_path = utils.strip_quotes(tokens[output_pos + 1]) + if len(tokens) > output_index + 1: + unquoted_path = utils.strip_quotes(tokens[output_index + 1]) output_to = os.path.expanduser(unquoted_path) # remove all the tokens after the output redirect - tokens = tokens[:output_pos] - except ValueError: - pass - - try: - output_pos = tokens.index(constants.REDIRECTION_APPEND) - output = constants.REDIRECTION_APPEND - - # Check if we are redirecting to a file - if len(tokens) > output_pos + 1: - unquoted_path = utils.strip_quotes(tokens[output_pos + 1]) - output_to = os.path.expanduser(unquoted_path) - - # remove all tokens after the output redirect - tokens = tokens[:output_pos] - except ValueError: - pass + tokens = tokens[:output_index] if terminator: # whatever is left is the suffix diff --git a/tests/conftest.py b/tests/conftest.py index 56538718..9d55eb4d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -77,13 +77,17 @@ optional arguments: -o, --output-file FILE output commands to a script file, implies -s -t, --transcript TRANSCRIPT - output commands and results to a transcript file, implies -s + output commands and results to a transcript file, + implies -s -c, --clear clear all history formatting: - -s, --script output commands in script format, i.e. without command numbers - -x, --expanded output expanded commands instead of entered command - -v, --verbose display history and include expanded commands if they differ from the typed command + -s, --script output commands in script format, i.e. without command + numbers + -x, --expanded output fully parsed commands with any aliases and + macros expanded, instead of typed commands + -v, --verbose display history and include expanded commands if they + differ from the typed command """ |