cmd2
cmd2 copied to clipboard
Completely ignore redirection characters leaving the line "as is"
Hi all,
I am wondering how disable any parsing of redirection commands.
I am aware of the feature allow_redirection. If set to False, redirection symbols and whatever follows are ignored.
However, here, I'd like to achieve another behavior that is "a redirection symbols (and follow-ups) are passed over as-is" to the do_command line.
Any strategy?
Thanks
You can quote the redirection characters on the command line.
Are you using argparse-decorated command functions?
I currently use a combination of "cmd1" and paramiko to have distributed ssh shell.
The commands in the cmd shell are forwarded to the ssh client pool.
Hence, in my use-case, redirection characters are meant for the ssh client pool rather than for the current host.
I'd like to migrate from cmd to cmd2.
Having the possibility to just forward redirection symbols is what bottlenecks the migration.
Hope this clarifies my context.
Our alias command does this. Any redirection characters intended for the alias value are quoted on the command line when the alias is created.
alias create my_alias help ">" out.txt
Then the alias creation function unquotes all redirection characters with the following code.
# Unquote redirection and terminator tokens
tokens_to_unquote = constants.REDIRECTION_TOKENS
tokens_to_unquote.extend(self.statement_parser.terminators)
utils.unquote_specific_tokens(args.command_args, tokens_to_unquote)
# Build the alias value string
value = args.command
if args.command_args:
value += ' ' + ' '.join(args.command_args)
The final alias value is:
help > out.txt
You can use similar code to forward redirection characters to the ssh shell they're intended for.
constants.REDIRECTION_TOKENS and utils.unquote_specific_tokens() are in the cmd2 API for you to use.