Skip to content

Commit d2ebee4

Browse files
committed
Fixed how shortcuts are set in examples
1 parent 88b45e1 commit d2ebee4

File tree

8 files changed

+30
-42
lines changed

8 files changed

+30
-42
lines changed

examples/arg_print.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ class ArgumentAndOptionPrinter(cmd2.Cmd):
1919

2020
def __init__(self):
2121
# Create command shortcuts which are typically 1 character abbreviations which can be used in place of a command
22-
self.shortcuts.update({'$': 'aprint', '%': 'oprint'})
23-
24-
# Make sure to call this super class __init__ *after* setting and/or updating shortcuts
25-
super().__init__()
26-
# NOTE: It is critical that the super class __init__ method be called AFTER updating certain parameters which
27-
# are not settable at runtime. This includes the shortcuts, etc.
22+
shortcuts = dict(self.DEFAULT_SHORTCUTS)
23+
shortcuts.update({'$': 'aprint', '%': 'oprint'})
24+
super().__init__(shortcuts=shortcuts)
2825

2926
def do_aprint(self, statement):
3027
"""Print the argument string this basic command is called with."""

examples/async_printing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class AlerterApp(cmd2.Cmd):
3232

3333
def __init__(self, *args, **kwargs) -> None:
3434
""" Initializer """
35-
3635
super().__init__(*args, **kwargs)
3736

3837
self.prompt = "(APR)> "

examples/cmd_as_argument.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ class CmdLineApp(cmd2.Cmd):
2929
MUMBLE_LAST = ['right?']
3030

3131
def __init__(self):
32-
self.allow_cli_args = False
33-
self.maxrepeats = 3
34-
35-
# Add stuff to shortcuts before calling base class initializer
36-
self.shortcuts.update({'&': 'speak'})
37-
32+
shortcuts = dict(self.DEFAULT_SHORTCUTS)
33+
shortcuts.update({'&': 'speak'})
3834
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
39-
super().__init__(use_ipython=False, multiline_commands=['orate'])
35+
super().__init__(use_ipython=False, multiline_commands=['orate'], shortcuts=shortcuts)
4036

37+
self.allow_cli_args = False
38+
self.maxrepeats = 3
4139
# Make maxrepeats settable at runtime
4240
self.settable['maxrepeats'] = 'max repetitions for speak command'
4341

examples/colors.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ class CmdLineApp(cmd2.Cmd):
6363
MUMBLE_LAST = ['right?']
6464

6565
def __init__(self):
66-
self.maxrepeats = 3
67-
68-
# Add stuff to shortcuts before calling base class initializer
69-
self.shortcuts.update({'&': 'speak'})
70-
66+
shortcuts = dict(self.DEFAULT_SHORTCUTS)
67+
shortcuts.update({'&': 'speak'})
7168
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
72-
super().__init__(use_ipython=True, multiline_commands=['orate'])
69+
super().__init__(use_ipython=True, multiline_commands=['orate'], shortcuts=shortcuts)
7370

71+
self.maxrepeats = 3
7472
# Make maxrepeats settable at runtime
7573
self.settable['maxrepeats'] = 'max repetitions for speak command'
7674

examples/decorator_example.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
class CmdLineApp(cmd2.Cmd):
2020
""" Example cmd2 application. """
2121
def __init__(self, ip_addr=None, port=None, transcript_files=None):
22-
self.shortcuts.update({'&': 'speak'})
23-
self.maxrepeats = 3
24-
22+
shortcuts = dict(self.DEFAULT_SHORTCUTS)
23+
shortcuts.update({'&': 'speak'})
2524
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
26-
super().__init__(use_ipython=False, transcript_files=transcript_files, multiline_commands=['orate'])
25+
super().__init__(use_ipython=False, transcript_files=transcript_files, multiline_commands=['orate'],
26+
shortcuts=shortcuts)
2727

28+
self.maxrepeats = 3
2829
# Make maxrepeats settable at runtime
2930
self.settable['maxrepeats'] = 'Max number of `--repeat`s allowed'
3031

examples/example.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ class CmdLineApp(cmd2.Cmd):
2727
MUMBLE_LAST = ['right?']
2828

2929
def __init__(self):
30-
self.maxrepeats = 3
31-
32-
# Add stuff to shortcuts before calling base class initializer
33-
self.shortcuts.update({'&': 'speak'})
34-
30+
shortcuts = dict(self.DEFAULT_SHORTCUTS)
31+
shortcuts.update({'&': 'speak'})
3532
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
36-
super().__init__(use_ipython=False, multiline_commands=['orate'])
33+
super().__init__(use_ipython=False, multiline_commands=['orate'], shortcuts=shortcuts)
3734

3835
# Make maxrepeats settable at runtime
36+
self.maxrepeats = 3
3937
self.settable['maxrepeats'] = 'max repetitions for speak command'
4038

4139
speak_parser = argparse.ArgumentParser()

examples/pirate.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@
2828
class Pirate(cmd2.Cmd):
2929
"""A piratical example cmd2 application involving looting and drinking."""
3030
def __init__(self):
31+
"""Initialize the base class as well as this one"""
32+
shortcuts = dict(self.DEFAULT_SHORTCUTS)
33+
shortcuts.update({'~': 'sing'})
34+
super().__init__(multiline_commands=['sing'], terminators=[MULTILINE_TERMINATOR, '...'], shortcuts=shortcuts)
35+
3136
self.default_to_shell = True
3237
self.songcolor = Fore.BLUE
3338

34-
# Add stuff to shortcuts before calling base class initializer
35-
self.shortcuts.update({'~': 'sing'})
36-
37-
"""Initialize the base class as well as this one"""
38-
super().__init__(multiline_commands=['sing'], terminators=[MULTILINE_TERMINATOR, '...'])
39-
4039
# Make songcolor settable at runtime
4140
self.settable['songcolor'] = 'Color to ``sing`` in (black/red/green/yellow/blue/magenta/cyan/white)'
4241

examples/plumbum_colors.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ class CmdLineApp(cmd2.Cmd):
6666
MUMBLE_LAST = ['right?']
6767

6868
def __init__(self):
69-
self.maxrepeats = 3
70-
71-
# Add stuff to shortcuts before calling base class initializer
72-
self.shortcuts.update({'&': 'speak'})
73-
69+
shortcuts = dict(self.DEFAULT_SHORTCUTS)
70+
shortcuts.update({'&': 'speak'})
7471
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
75-
super().__init__(use_ipython=True, multiline_commands=['orate'])
72+
super().__init__(use_ipython=True, multiline_commands=['orate'], shortcuts=shortcuts)
7673

74+
self.maxrepeats = 3
7775
# Make maxrepeats settable at runtime
7876
self.settable['maxrepeats'] = 'max repetitions for speak command'
7977

0 commit comments

Comments
 (0)