Skip to content

Commit af06c06

Browse files
authored
Merge pull request #235 from python-cmd2/command_aliases
Updated docs to make updating shortcuts more clear
2 parents 24af59e + 5fa0916 commit af06c06

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

docs/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ Resources
4848
* `project bug tracker`_
4949
* Florida PyCon 2017: `slides <https://docs.google.com/presentation/d/1LRmpfBt3V-pYQfgQHdczf16F3hcXmhK83tl77R6IJtE>`_
5050
* PyOhio 2011: `video <https://archive.org/details/pyvideo_541___pyohio-2011-interactive-command-line-interpreters-with-cmd-and-cmd2>`_
51-
* PyCon 2010: `video <http://pyvideo.org/pycon-us-2010/pycon-2010--easy-command-line-applications-with-c.html>`_
5251

5352
These docs will refer to ``App`` as your ``cmd2.Cmd``
5453
subclass, and ``app`` as an instance of ``App``. Of

docs/settingchanges.rst

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Whether or not you set ``case_insensitive``, *please do not* define
1919
command method names with any uppercase letters. ``cmd2`` expects all command methods
2020
to be lowercase.
2121

22-
Shortcuts
23-
=========
22+
Shortcuts (command aliases)
23+
===========================
2424

25-
Special-character shortcuts for common commands can make life more convenient for your
26-
users. Shortcuts are used without a space separating them from their arguments,
25+
Command aliases for long command names such as special-character shortcuts for common commands can make life more
26+
convenient for your users. Shortcuts are used without a space separating them from their arguments,
2727
like ``!ls``. By default, the following shortcuts are defined:
2828

2929
``?``
@@ -42,7 +42,20 @@ To define more shortcuts, update the dict ``App.shortcuts`` with the
4242
{'shortcut': 'command_name'} (omit ``do_``)::
4343

4444
class App(Cmd2):
45-
Cmd2.shortcuts.update({'*': 'sneeze', '~': 'squirm'})
45+
def __init__(self):
46+
# Make sure you update the shortcuts attribute before calling the super class __init__
47+
self.shortcuts.update({'*': 'sneeze', '~': 'squirm'})
48+
49+
# Make sure to call this super class __init__ after updating shortcuts
50+
cmd2.Cmd.__init__(self)
51+
52+
.. warning::
53+
54+
Command aliases needed to be created by updating the ``shortcuts`` dictionary attribute prior to calling the
55+
``cmd2.Cmd`` super class ``__init__()`` method. Moreover, that super class init method needs to be called after
56+
updating the ``shortcuts`` attribute This warning applies in general to many other attributes which are not
57+
settable at runtime such as ``commentGrammars``, ``multilineCommands``, etc.
58+
4659

4760
Default to shell
4861
================

examples/arg_print.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
77
This is intended to serve as a live demonstration so that developers can experiment with and understand how command
88
and argument parsing is intended to work.
9+
10+
It also serves as an example of how to create command aliases (shortcuts).
911
"""
1012
import pyparsing
1113
import cmd2
@@ -19,8 +21,13 @@ def __init__(self):
1921
# Uncomment this line to disable Python-style comments but still allow C-style comments
2022
# self.commentGrammars = pyparsing.Or([pyparsing.cStyleComment])
2123

22-
# Make sure to call this super class __init__ after setting commentGrammars and not before
24+
# Create command aliases which are shorter
25+
self.shortcuts.update({'ap': 'aprint', 'op': 'oprint'})
26+
27+
# Make sure to call this super class __init__ *after* setting commentGrammars and/or updating shortcuts
2328
cmd2.Cmd.__init__(self)
29+
# NOTE: It is critical that the super class __init__ method be called AFTER updating certain parameters which
30+
# are not settable at runtime. This includes the commentGrammars, shortcuts, multilineCommands, etc.
2431

2532
def do_aprint(self, arg):
2633
"""Print the argument string this basic command is called with."""

0 commit comments

Comments
 (0)