@@ -135,28 +135,41 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
135135 allow_cli_args : bool = True , transcript_files : Optional [List [str ]] = None ,
136136 allow_redirection : bool = True , multiline_commands : Optional [List [str ]] = None ,
137137 terminators : Optional [List [str ]] = None , shortcuts : Optional [Dict [str , str ]] = None ) -> None :
138- """An easy but powerful framework for writing line-oriented command interpreters, extends Python's cmd package.
138+ """An easy but powerful framework for writing line-oriented command
139+ interpreters. Extends Python's cmd package.
139140
140141 :param completekey: readline name of a completion key, default to Tab
141142 :param stdin: alternate input file object, if not specified, sys.stdin is used
142143 :param stdout: alternate output file object, if not specified, sys.stdout is used
143144 :param persistent_history_file: file path to load a persistent cmd2 command history from
144- :param persistent_history_length: max number of history items to write to the persistent history file
145+ :param persistent_history_length: max number of history items to write
146+ to the persistent history file
145147 :param startup_script: file path to a script to execute at startup
146148 :param use_ipython: should the "ipy" command be included for an embedded IPython shell
147- :param allow_cli_args: if True, then cmd2 will process command line arguments as either
148- commands to be run or, if -t is specified, transcript files to run.
149- This should be set to False if your application parses its own arguments.
150- :param transcript_files: allow running transcript tests when allow_cli_args is False
151- :param allow_redirection: should output redirection and pipes be allowed. this is only a security setting
152- and does not alter parsing behavior.
149+ :param allow_cli_args: if ``True``, then :meth:`cmd2.Cmd.__init__` will process command
150+ line arguments as either commands to be run or, if ``-t`` or
151+ ``--test`` are given, transcript files to run. This should be
152+ set to ``False`` if your application parses its own command line
153+ arguments.
154+ :param transcript_files: pass a list of transcript files to be run on initialization.
155+ This allows running transcript tests when ``allow_cli_args``
156+ is ``False``. If ``allow_cli_args`` is ``True`` this parameter
157+ is ignored.
158+ :param allow_redirection: If ``False``, prevent output redirection and piping to shell
159+ commands. This parameter prevents redirection and piping, but
160+ does not alter parsing behavior. A user can still type
161+ redirection and piping tokens, and they will be parsed as such
162+ but they won't do anything.
153163 :param multiline_commands: list of commands allowed to accept multi-line input
154- :param terminators: list of characters that terminate a command. These are mainly intended for terminating
155- multiline commands, but will also terminate single-line commands. If not supplied, then
156- defaults to semicolon. If your app only contains single-line commands and you want
157- terminators to be treated as literals by the parser, then set this to an empty list.
158- :param shortcuts: dictionary containing shortcuts for commands. If not supplied, then defaults to
159- constants.DEFAULT_SHORTCUTS.
164+ :param terminators: list of characters that terminate a command. These are mainly
165+ intended for terminating multiline commands, but will also
166+ terminate single-line commands. If not supplied, the default
167+ is a semicolon. If your app only contains single-line commands
168+ and you want terminators to be treated as literals by the parser,
169+ then set this to an empty list.
170+ :param shortcuts: dictionary containing shortcuts for commands. If not supplied,
171+ then defaults to constants.DEFAULT_SHORTCUTS. If you do not want
172+ any shortcuts, pass an empty dictionary.
160173 """
161174 # If use_ipython is False, make sure the ipy command isn't available in this instance
162175 if not use_ipython :
@@ -192,7 +205,7 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
192205
193206 # A dictionary mapping settable names to their Settable instance
194207 self .settables = dict ()
195- self .build_settables ()
208+ self ._build_settables ()
196209
197210 # Use as prompt for multiline commands on the 2nd+ line of input
198211 self .continuation_prompt = '> '
@@ -374,24 +387,26 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
374387
375388 def add_settable (self , settable : Settable ) -> None :
376389 """
377- Convenience method to add a settable parameter to self.settables
390+ Convenience method to add a settable parameter to ``self.settables``
391+
378392 :param settable: Settable object being added
379393 """
380394 self .settables [settable .name ] = settable
381395
382396 def remove_settable (self , name : str ) -> None :
383397 """
384- Convenience method for removing a settable parameter from self.settables
398+ Convenience method for removing a settable parameter from ``self.settables``
399+
385400 :param name: name of the settable being removed
386- :raises: KeyError if the no Settable matches this name
401+ :raises: KeyError if the Settable matches this name
387402 """
388403 try :
389404 del self .settables [name ]
390405 except KeyError :
391406 raise KeyError (name + " is not a settable parameter" )
392407
393- def build_settables (self ):
394- """Populates self.add_settable with parameters that can be edited via the set command """
408+ def _build_settables (self ):
409+ """Construct the default settings """
395410 self .add_settable (Settable ('allow_style' , str ,
396411 'Allow ANSI text style sequences in output (valid values: '
397412 '{}, {}, {})' .format (ansi .STYLE_TERMINAL ,
@@ -1508,13 +1523,54 @@ def sigint_handler(self, signum: int, frame) -> None:
15081523 raise KeyboardInterrupt ("Got a keyboard interrupt" )
15091524
15101525 def precmd (self , statement : Statement ) -> Statement :
1511- """Hook method executed just before the command is processed by ``onecmd()`` and after adding it to the history.
1526+ """Hook method executed just before the command is executed by
1527+ :meth:`~cmd2.Cmd.onecmd` and after adding it to history.
15121528
15131529 :param statement: subclass of str which also contains the parsed input
15141530 :return: a potentially modified version of the input Statement object
1531+
1532+ See :meth:`~cmd2.Cmd.register_postparsing_hook` and
1533+ :meth:`~cmd2.Cmd.register_precmd_hook` for more robust ways
1534+ to run hooks before the command is executed. See
1535+ :ref:`features/hooks:Postparsing Hooks` and
1536+ :ref:`features/hooks:Precommand Hooks` for more information.
15151537 """
15161538 return statement
15171539
1540+ def postcmd (self , stop : bool , statement : Statement ) -> bool :
1541+ """Hook method executed just after a command is executed by
1542+ :meth:`~cmd2.Cmd.onecmd`.
1543+
1544+ :param stop: return `True` to request the command loop terminate
1545+ :param statement: subclass of str which also contains the parsed input
1546+
1547+ See :meth:`~cmd2.Cmd.register_postcmd_hook` and :meth:`~cmd2.Cmd.register_cmdfinalization_hook` for more robust ways
1548+ to run hooks after the command is executed. See
1549+ :ref:`features/hooks:Postcommand Hooks` and
1550+ :ref:`features/hooks:Command Finalization Hooks` for more information.
1551+ """
1552+ return stop
1553+
1554+ def preloop (self ):
1555+ """Hook method executed once when the :meth:`~.cmd2.Cmd.cmdloop()`
1556+ method is called.
1557+
1558+ See :meth:`~cmd2.Cmd.register_preloop_hook` for a more robust way
1559+ to run hooks before the command loop begins. See
1560+ :ref:`features/hooks:Application Lifecycle Hooks` for more information.
1561+ """
1562+ pass
1563+
1564+ def postloop (self ):
1565+ """Hook method executed once when the :meth:`~.cmd2.Cmd.cmdloop()`
1566+ method is about to return.
1567+
1568+ See :meth:`~cmd2.Cmd.register_postloop_hook` for a more robust way
1569+ to run hooks after the command loop completes. See
1570+ :ref:`features/hooks:Application Lifecycle Hooks` for more information.
1571+ """
1572+ pass
1573+
15181574 def parseline (self , line : str ) -> Tuple [str , str , str ]:
15191575 """Parse the line into a command name and a string containing the arguments.
15201576
0 commit comments