@@ -19,7 +19,11 @@ command processing loop.
1919Application Lifecycle Hooks
2020---------------------------
2121
22- You can register methods to be called at the beginning of the command loop::
22+ You can run a script on initialization by passing the script filename in the
23+ ``startup_script `` parameter of :meth: `cmd2.Cmd.__init__ `.
24+
25+ You can also register methods to be called at the beginning of the command
26+ loop::
2327
2428 class App(cmd2.Cmd):
2529 def __init__(self, *args, *kwargs):
@@ -29,8 +33,9 @@ You can register methods to be called at the beginning of the command loop::
2933 def myhookmethod(self):
3034 self.poutput("before the loop begins")
3135
32- To retain backwards compatibility with `cmd.Cmd `, after all registered preloop
33- hooks have been called, the ``preloop() `` method is called.
36+ To retain backwards compatibility with ``cmd.Cmd ``, after all registered
37+ preloop hooks have been called, the :meth: `~cmd2.Cmd.preloop ` method is
38+ called.
3439
3540A similar approach allows you to register functions to be called after the
3641command loop has finished::
@@ -43,54 +48,68 @@ command loop has finished::
4348 def myhookmethod(self):
4449 self.poutput("before the loop begins")
4550
46- To retain backwards compatibility with `cmd.Cmd `, after all registered postloop
47- hooks have been called, the ``postloop() `` method is called.
51+ To retain backwards compatibility with ``cmd.Cmd ``, after all registered
52+ postloop hooks have been called, the :meth: `~cmd2.Cmd.postloop ` method is
53+ called.
4854
4955Preloop and postloop hook methods are not passed any parameters and any return
5056value is ignored.
5157
58+ The approach of registering hooks instead of overriding methods allows multiple
59+ hooks to be called before the command loop begins or ends. Plugin authors
60+ should review :ref: `features/plugins:Hooks ` for best practices writing hooks.
61+
5262
5363Application Lifecycle Attributes
5464--------------------------------
5565
56- There are numerous attributes of and arguments to ``cmd2.Cmd `` which have a
57- significant effect on the application behavior upon entering or during the main
58- loop. A partial list of some of the more important ones is presented here:
66+ There are numerous attributes on :class: `cmd2.Cmd ` which affect application
67+ behavior upon entering or during the command loop:
68+
69+ - :data: `~cmd2.Cmd.intro ` - if provided this serves as the intro banner printed
70+ once at start of application, after :meth: `~cmd2.Cmd.preloop ` is called.
71+ - :data: `~cmd2.Cmd.prompt ` - see :ref: `features/prompt:Prompt ` for more
72+ information.
73+ - :data: `~cmd2.Cmd.continuation_prompt ` - The prompt issued to solicit input
74+ for the 2nd and subsequent lines of a
75+ :ref: `multiline command <features/multiline_commands:Multiline Commands >`
76+ - :data: `~cmd2.Cmd.echo ` - if ``True `` write the prompt and the command into
77+ the output stream.
5978
60- - **intro **: *str * - if provided this serves as the intro banner printed once
61- at start of application, after ``preloop `` runs
62- - **allow_cli_args **: *bool * - if True (default), then searches for -t or
63- --test at command line to invoke transcript testing mode instead of a normal
64- main loop and also processes any commands provided as arguments on the
65- command line just prior to entering the main loop
66- - **echo **: *bool * - if True, then the command line entered is echoed to the
67- screen (most useful when running scripts)
68- - **prompt **: *str * - sets the prompt which is displayed, can be dynamically
69- changed based on application state and/or command results
79+ In addition, several arguments to :meth: `cmd2.Cmd.__init__ ` also affect
80+ the command loop behavior:
81+
82+ - ``allow_cli_args `` - allows commands to be specified on the operating system
83+ command line which are executed before the command processing loop begins.
84+ - ``transcript_files `` - see :ref: `features/transcripts:Transcripts ` for more
85+ information
86+ - ``startup_script `` - run a script on initialization. See
87+ :ref: `features/scripting:Scripting ` for more information.
7088
7189
7290Command Processing Loop
7391-----------------------
7492
75- When you call ` . cmdloop() `, the following sequence of events are repeated until
76- the application exits:
93+ When you call :meth: ` cmd2.Cmd. cmdloop `, the following sequence of events are
94+ repeated until the application exits:
7795
7896#. Output the prompt
7997#. Accept user input
80- #. Parse user input into ` Statement ` object
81- #. Call methods registered with ` register_postparsing_hook() `
98+ #. Parse user input into a :class: ` ~cmd2. Statement ` object
99+ #. Call methods registered with :meth: ` ~cmd2.Cmd. register_postparsing_hook() `
82100#. Redirect output, if user asked for it and it's allowed
83101#. Start timer
84- #. Call methods registered with ` register_precmd_hook() `
85- #. Call ` precmd() ` - for backwards compatibility with ``cmd.Cmd ``
86- #. Add statement to history
102+ #. Call methods registered with :meth: ` ~cmd2.Cmd. register_precmd_hook `
103+ #. Call :meth: ` ~cmd2.Cmd. precmd ` - for backwards compatibility with ``cmd.Cmd ``
104+ #. Add statement to :ref: ` features/ history:History `
87105#. Call `do_command ` method
88- #. Call methods registered with ` register_postcmd_hook() `
89- #. Call ` postcmd(stop, statement) ` - for backwards compatibility with
106+ #. Call methods registered with :meth: ` ~cmd2.Cmd. register_postcmd_hook() `
107+ #. Call :meth: ` ~cmd2.Cmd. postcmd ` - for backwards compatibility with
90108 ``cmd.Cmd ``
91109#. Stop timer and display the elapsed time
92110#. Stop redirecting output if it was redirected
93- #. Call methods registered with `register_cmdfinalization_hook() `
111+ #. Call methods registered with
112+ :meth: `~cmd2.Cmd.register_cmdfinalization_hook() `
94113
95114By registering hook methods, steps 4, 8, 12, and 16 allow you to run code
96115during, and control the flow of the command processing loop. Be aware that
@@ -103,6 +122,7 @@ Postparsing, precommand, and postcommand hook methods share some common ways to
103122influence the command processing loop.
104123
105124If a hook raises an exception:
125+
106126- no more hooks (except command finalization hooks) of any kind will be called
107127- if the command has not yet been executed, it will not be executed
108128- the exception message will be displayed for the user.
0 commit comments