@@ -4,14 +4,46 @@ History
44For Developers
55--------------
66
7- - Describe how ``cmd2 `` tracks history
8- - how persistent history works
9- - differences in history and bash shell history (we only store valid commands
10- in history)
11- - reference the public code structures we use to store history
7+ The ``cmd `` module from the Python standard library includes ``readline ``
8+ history.
9+
10+ :class: `cmd2.Cmd ` offers the same ``readline `` capabilities, but also maintains
11+ it's own data structures for the history of all commands entered by the user.
12+ When the class is initialized, it creates an instance of the
13+ :class: `cmd2.history.History ` class (which is a subclass of ``list ``) as
14+ :data: `cmd2.Cmd.history `.
15+
16+ Each time a command is executed (this gets
17+ complex, see :ref: `features/hooks:Command Processing Loop ` for exactly when)
18+ the parsed :class: `cmd2.Statement ` is appended to :data: `cmd2.Cmd.history `.
1219
1320``cmd2 `` adds the option of making this history persistent via optional
14- arguments to :meth: `cmd2.Cmd.__init__ `.
21+ arguments to :meth: `cmd2.Cmd.__init__ `. If you pass a filename in the
22+ ``persistent_history_file `` argument, the contents of :data: `cmd2.Cmd.history `
23+ will be pickled into that history file. We chose to use pickle instead of plain
24+ text so that we can save the results of parsing all the commands.
25+
26+ .. note ::
27+
28+ ``readline `` saves everything you type, whether it is a valid command or
29+ not. ``cmd2 `` only saves input to internal history if the command parses
30+ successfully and is a valid command. This design choice was intentional,
31+ because the contents of history can be saved to a file as a script, or can
32+ be re-run. Not saving invalid input reduces unintentional errors when doing
33+ so.
34+
35+ However, this design choice causes an inconsistency between the
36+ ``readline `` history and the ``cmd2 `` history when you enter an invalid
37+ command: it is saved to the ``readline `` history, but not to the ``cmd2 ``
38+ history.
39+
40+ The :data: `cmd2.Cmd.history ` attribute, the :class: `cmd2.history.History `
41+ class, and the :class: `cmd2.history.HistoryItem ` class are all part of the
42+ public API for :class: `cmd2.Cmd `. You could use these classes to implement
43+ write your own ``history `` command (see below for documentation on how the
44+ included ``history `` command works). If you don't like pickled history, you
45+ could implement your own mechanism for saving and loading history from a plain
46+ text file.
1547
1648
1749For Users
0 commit comments