summaryrefslogtreecommitdiff
path: root/docs/features
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2019-07-06 05:09:46 -0600
committerkotfu <kotfu@kotfu.net>2019-07-06 05:09:46 -0600
commita19f5274325e9dd3f3f578f7a3e26b922e8f7ecd (patch)
tree8fe218c1ede5618c9964f3f20c988383df9f92be /docs/features
parent9e64f600b65d99ea5a83051372372512be698b1b (diff)
downloadcmd2-git-a19f5274325e9dd3f3f578f7a3e26b922e8f7ecd.tar.gz
Formatting cleanups
Diffstat (limited to 'docs/features')
-rw-r--r--docs/features/hooks.rst29
-rw-r--r--docs/features/transcripts.rst76
2 files changed, 57 insertions, 48 deletions
diff --git a/docs/features/hooks.rst b/docs/features/hooks.rst
index 5db97fe5..01d12425 100644
--- a/docs/features/hooks.rst
+++ b/docs/features/hooks.rst
@@ -1,7 +1,5 @@
-.. cmd2 documentation for application and command lifecycle and the available hooks
-
-cmd2 Application Lifecycle and Hooks
-====================================
+Hooks
+=====
The typical way of starting a cmd2 application is as follows::
@@ -17,6 +15,7 @@ There are several pre-existing methods and attributes which you can tweak to
control the overall behavior of your application before, during, and after the
command processing loop.
+
Application Lifecycle Hooks
---------------------------
@@ -114,8 +113,9 @@ If a hook raises any other exception:
Specific types of hook methods have additional options as described below.
+
Postparsing Hooks
-^^^^^^^^^^^^^^^^^
+-----------------
Postparsing hooks are called after the user input has been parsed but before
execution of the command. These hooks can be used to:
@@ -186,7 +186,7 @@ attribute set to ``True``:
Precommand Hooks
-^^^^^^^^^^^^^^^^^
+----------------
Precommand hooks can modify the user input, but can not request the application
terminate. If your hook needs to be able to exit the application, you should
@@ -224,7 +224,7 @@ with ``cmd.Cmd``, this method is passed a ``Statement``, not a
Postcommand Hooks
-^^^^^^^^^^^^^^^^^^
+-----------------
Once the command method has returned (i.e. the ``do_command(self, statement)
method`` has been called and returns, all postcommand hooks are called. If
@@ -254,10 +254,11 @@ After all registered postcommand hooks have been called,
``self.postcmd(statement)`` will be called to retain full backward compatibility
with ``cmd.Cmd``.
-If any postcommand hook (registered or ``self.postcmd()``) returns a ``PostcommandData`` object
-with the stop attribute set to ``True``, subsequent postcommand hooks will still be called, as
-will the command finalization hooks, but once those hooks have all been called, the application
-will terminate. Likewise, if ``self.postcmd()`` returns ``True``, the command finalization hooks
+If any postcommand hook (registered or ``self.postcmd()``) returns a
+``PostcommandData`` object with the stop attribute set to ``True``, subsequent
+postcommand hooks will still be called, as will the command finalization hooks,
+but once those hooks have all been called, the application will terminate.
+Likewise, if ``self.postcmd()`` returns ``True``, the command finalization hooks
will be called before the application terminates.
Any postcommand hook can change the value of the ``stop`` parameter before
@@ -270,7 +271,7 @@ compelling reason to do otherwise.
Command Finalization Hooks
-^^^^^^^^^^^^^^^^^^^^^^^^^^
+--------------------------
Command finalization hooks are called even if one of the other types of hooks or
the command method raise an exception. Here's how to create and register a
@@ -284,7 +285,9 @@ command finalization hook::
def myhookmethod(self, stop, statement):
return stop
-Command Finalization hooks must check whether the statement object is ``None``. There are certain circumstances where these hooks may be called before the statement has been parsed, so you can't always rely on having a statement.
+Command Finalization hooks must check whether the statement object is ``None``.
+There are certain circumstances where these hooks may be called before the
+statement has been parsed, so you can't always rely on having a statement.
If any prior postparsing or precommand hook has requested the application to
terminate, the value of the ``stop`` parameter passed to the first command
diff --git a/docs/features/transcripts.rst b/docs/features/transcripts.rst
index eeb51534..e4ff79a5 100644
--- a/docs/features/transcripts.rst
+++ b/docs/features/transcripts.rst
@@ -9,12 +9,12 @@ from commands that produce dynamic or variable output.
.. highlight:: none
-Creating a transcript
-=====================
-Automatically from history
---------------------------
-A transcript can automatically generated based upon commands previously executed in the *history* using ``history -t``::
+Creating From History
+---------------------
+
+A transcript can automatically generated based upon commands previously executed
+in the *history* using ``history -t``::
(Cmd) help
...
@@ -27,23 +27,28 @@ This is by far the easiest way to generate a transcript.
.. warning::
- Make sure you use the **poutput()** method in your ``cmd2`` application for generating command output. This method
- of the ``cmd2.Cmd`` class ensure that output is properly redirected when redirecting to a file, piping to a shell
+ Make sure you use the **poutput()** method in your ``cmd2`` application for
+ generating command output. This method of the ``cmd2.Cmd`` class ensure that
+ output is properly redirected when redirecting to a file, piping to a shell
command, and when generating a transcript.
-Automatically from a script file
---------------------------------
+
+Creating From A Script File
+---------------------------
+
A transcript can also be automatically generated from a script file using ``run_script -t``::
(Cmd) run_script scripts/script.txt -t transcript.txt
2 commands and their outputs saved to transcript file 'transcript.txt'
(Cmd)
-This is a particularly attractive option for automatically regenerating transcripts for regression testing as your ``cmd2``
-application changes.
+This is a particularly attractive option for automatically regenerating
+transcripts for regression testing as your ``cmd2`` application changes.
+
+
+Creating Manually
+-----------------
-Manually
---------
Here's a transcript created from ``python examples/example.py``::
(Cmd) say -r 3 Goodnight, Gracie
@@ -82,7 +87,8 @@ lines of the transcript as comments::
maybe we could like go to er lunch right?
In this example I've used several different commenting styles, and even bare
-text. It doesn't matter what you put on those beginning lines. Everything before::
+text. It doesn't matter what you put on those beginning lines. Everything
+before::
(Cmd) say -r 3 Goodnight, Gracie
@@ -90,14 +96,14 @@ will be ignored.
Regular Expressions
-===================
+-------------------
-If we used the above transcript as-is, it would likely fail. As you can see,
-the ``mumble`` command doesn't always return the same thing: it inserts random
-words into the input.
+If we used the above transcript as-is, it would likely fail. As you can see, the
+``mumble`` command doesn't always return the same thing: it inserts random words
+into the input.
-Regular expressions can be included in the response portion of a transcript,
-and are surrounded by slashes::
+Regular expressions can be included in the response portion of a transcript, and
+are surrounded by slashes::
(Cmd) mumble maybe we could go to lunch
/.*\bmaybe\b.*\bcould\b.*\blunch\b.*/
@@ -141,24 +147,24 @@ the path instead of specifying it verbatim, or we can escape the slashes::
prompt: (Cmd)/ /
Some terminal emulators strip trailing space when you copy text from them.
- This could make the actual data generated by your app different than the
- text you pasted into the transcript, and it might not be readily obvious why
- the transcript is not passing. Consider using :ref:`output_redirection` to
- the clipboard or to a file to ensure you accurately capture the output of
- your command.
+ This could make the actual data generated by your app different than the text
+ you pasted into the transcript, and it might not be readily obvious why the
+ transcript is not passing. Consider using :ref:`output_redirection` to the
+ clipboard or to a file to ensure you accurately capture the output of your
+ command.
- If you aren't using regular expressions, make sure the newlines at the end
- of your transcript exactly match the output of your commands. A common cause
- of a failing transcript is an extra or missing newline.
+ If you aren't using regular expressions, make sure the newlines at the end of
+ your transcript exactly match the output of your commands. A common cause of
+ a failing transcript is an extra or missing newline.
If you are using regular expressions, be aware that depending on how you
- write your regex, the newlines after the regex may or may not matter.
- ``\Z`` matches *after* the newline at the end of the string, whereas
- ``$`` matches the end of the string *or* just before a newline.
+ write your regex, the newlines after the regex may or may not matter. ``\Z``
+ matches *after* the newline at the end of the string, whereas ``$`` matches
+ the end of the string *or* just before a newline.
-Running a transcript
-====================
+Running A Transcript
+--------------------
Once you have created a transcript, it's easy to have your application play it
back and check the output. From within the ``examples/`` directory::
@@ -177,8 +183,8 @@ output matches the expected result from the transcript.
.. note::
If you have set ``allow_cli_args`` to False in order to disable parsing of
- command line arguments at invocation, then the use of ``-t`` or ``--test``
- to run transcript testing is automatically disabled. In this case, you can
+ command line arguments at invocation, then the use of ``-t`` or ``--test`` to
+ run transcript testing is automatically disabled. In this case, you can
alternatively provide a value for the optional ``transcript_files`` when
constructing the instance of your ``cmd2.Cmd`` derived class in order to
cause a transcript test to run::