diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2020-01-09 22:45:53 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2020-01-09 22:45:53 -0500 |
commit | 591bd29cb4a3bcb9b1f40ffc1f30429c6501ebdb (patch) | |
tree | 46c1e3af762c9cb222c6ae90bf0446d6eca4b388 /examples | |
parent | 10b844809e3a9500274dc4af4e780708975ba905 (diff) | |
parent | d4556962799e68ea4d54ff86186428d17edcaef9 (diff) | |
download | cmd2-git-591bd29cb4a3bcb9b1f40ffc1f30429c6501ebdb.tar.gz |
Merge branch 'master' into generating_output_docs
# Conflicts:
# docs/features/generating_output.rst
# docs/features/settings.rst
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/colors.py | 14 | ||||
-rwxr-xr-x | examples/dynamic_commands.py | 26 | ||||
-rwxr-xr-x | examples/plumbum_colors.py | 14 | ||||
-rw-r--r-- | examples/transcripts/exampleSession.txt | 2 | ||||
-rw-r--r-- | examples/transcripts/transcript_regex.txt | 2 |
5 files changed, 36 insertions, 22 deletions
diff --git a/examples/colors.py b/examples/colors.py index 7a4d15e6..bbb3b2ad 100755 --- a/examples/colors.py +++ b/examples/colors.py @@ -6,21 +6,21 @@ A sample application for cmd2. Demonstrating colorized output. Experiment with the command line options on the `speak` command to see how different output colors ca -The allow_ansi setting has three possible values: +The allow_style setting has three possible values: Never - poutput(), pfeedback(), and ppaged() strip all ANSI escape sequences + poutput(), pfeedback(), and ppaged() strip all ANSI style sequences which instruct the terminal to colorize output Terminal (the default value) poutput(), pfeedback(), and ppaged() do not strip any - ANSI escape sequences when the output is a terminal, but if the output is - a pipe or a file the escape sequences are stripped. If you want colorized - output you must add ANSI escape sequences using either cmd2's internal ansi + ANSI style sequences when the output is a terminal, but if the output is + a pipe or a file the style sequences are stripped. If you want colorized + output you must add ANSI style sequences using either cmd2's internal ansi module or another color library such as `plumbum.colors` or `colorama`. Always - poutput(), pfeedback(), and ppaged() never strip ANSI escape sequences, + poutput(), pfeedback(), and ppaged() never strip ANSI style sequences, regardless of the output destination """ import argparse @@ -42,7 +42,7 @@ class CmdLineApp(cmd2.Cmd): self.settable['maxrepeats'] = 'max repetitions for speak command' # Should ANSI color output be allowed - self.allow_ansi = ansi.ANSI_TERMINAL + self.allow_style = ansi.STYLE_TERMINAL speak_parser = argparse.ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') diff --git a/examples/dynamic_commands.py b/examples/dynamic_commands.py index 69816d40..620acb7f 100755 --- a/examples/dynamic_commands.py +++ b/examples/dynamic_commands.py @@ -3,13 +3,32 @@ """A simple example demonstrating how do_* commands can be created in a loop. """ import functools + import cmd2 -COMMAND_LIST = ['foo', 'bar', 'baz'] +from cmd2.constants import COMMAND_FUNC_PREFIX, HELP_FUNC_PREFIX + +COMMAND_LIST = ['foo', 'bar'] +CATEGORY = 'Dynamic Commands' class CommandsInLoop(cmd2.Cmd): """Example of dynamically adding do_* commands.""" def __init__(self): + # Add dynamic commands before calling cmd2.Cmd's init since it validates command names + for command in COMMAND_LIST: + # Create command function and add help category to it + cmd_func = functools.partial(self.send_text, text=command) + cmd2.categorize(cmd_func, CATEGORY) + + # Add command function to CLI object + cmd_func_name = COMMAND_FUNC_PREFIX + command + setattr(self, cmd_func_name, cmd_func) + + # Add help function to CLI object + help_func = functools.partial(self.text_help, text=command) + help_func_name = HELP_FUNC_PREFIX + command + setattr(self, help_func_name, help_func) + super().__init__(use_ipython=True) def send_text(self, args: cmd2.Statement, *, text: str): @@ -21,11 +40,6 @@ class CommandsInLoop(cmd2.Cmd): self.poutput("Simulate sending {!r} to a server and printing the response".format(text)) -for command in COMMAND_LIST: - setattr(CommandsInLoop, 'do_{}'.format(command), functools.partialmethod(CommandsInLoop.send_text, text=command)) - setattr(CommandsInLoop, 'help_{}'.format(command), functools.partialmethod(CommandsInLoop.text_help, text=command)) - - if __name__ == '__main__': app = CommandsInLoop() app.cmdloop() diff --git a/examples/plumbum_colors.py b/examples/plumbum_colors.py index b4f0ad1c..fe692805 100755 --- a/examples/plumbum_colors.py +++ b/examples/plumbum_colors.py @@ -6,21 +6,21 @@ A sample application for cmd2. Demonstrating colorized output using the plumbum Experiment with the command line options on the `speak` command to see how different output colors ca -The allow_ansi setting has three possible values: +The allow_style setting has three possible values: Never - poutput(), pfeedback(), and ppaged() strip all ANSI escape sequences + poutput(), pfeedback(), and ppaged() strip all ANSI style sequences which instruct the terminal to colorize output Terminal (the default value) poutput(), pfeedback(), and ppaged() do not strip any - ANSI escape sequences when the output is a terminal, but if the output is - a pipe or a file the escape sequences are stripped. If you want colorized - output you must add ANSI escape sequences using either cmd2's internal ansi + ANSI style sequences when the output is a terminal, but if the output is + a pipe or a file the style sequences are stripped. If you want colorized + output you must add ANSI style sequences using either cmd2's internal ansi module or another color library such as `plumbum.colors` or `colorama`. Always - poutput(), pfeedback(), and ppaged() never strip ANSI escape sequences, + poutput(), pfeedback(), and ppaged() never strip ANSI style sequences, regardless of the output destination WARNING: This example requires the plumbum package, which isn't normally required by cmd2. @@ -78,7 +78,7 @@ class CmdLineApp(cmd2.Cmd): self.settable['maxrepeats'] = 'max repetitions for speak command' # Should ANSI color output be allowed - self.allow_ansi = ansi.ANSI_TERMINAL + self.allow_style = ansi.STYLE_TERMINAL speak_parser = argparse.ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') diff --git a/examples/transcripts/exampleSession.txt b/examples/transcripts/exampleSession.txt index 3e579c28..5a75235b 100644 --- a/examples/transcripts/exampleSession.txt +++ b/examples/transcripts/exampleSession.txt @@ -3,7 +3,7 @@ # The regex for editor will match whatever program you use. # regexes on prompts just make the trailing space obvious (Cmd) set -allow_ansi: /(Terminal|Always|Never)/ +allow_style: /(Terminal|Always|Never)/ continuation_prompt: >/ / debug: False echo: False diff --git a/examples/transcripts/transcript_regex.txt b/examples/transcripts/transcript_regex.txt index d94c442b..276f7d22 100644 --- a/examples/transcripts/transcript_regex.txt +++ b/examples/transcripts/transcript_regex.txt @@ -3,7 +3,7 @@ # The regex for editor will match whatever program you use. # regexes on prompts just make the trailing space obvious (Cmd) set -allow_ansi: /(Terminal|Always|Never)/ +allow_style: /(Terminal|Always|Never)/ continuation_prompt: >/ / debug: False echo: False |