Skip to content

Commit f9602fb

Browse files
gh-144338: Improve cmd.Cmd docs for emptyline() and do_EOF
- Document that do_EOF should be defined to handle Ctrl-D gracefully, with a cross-reference to the example - Add a note to emptyline() docs about overriding with pass to suppress the default repeat-last-command behavior - Add emptyline() and do_EOF() to the TurtleShell example to demonstrate these common patterns Closes #144338 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f3a381e commit f9602fb

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Doc/library/cmd.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ A :class:`Cmd` instance has the following methods:
6868
cursor to the left non-destructively, etc.).
6969

7070
An end-of-file on input is passed back as the string ``'EOF'``.
71+
To handle this gracefully, define a :meth:`!do_EOF` method in your subclass
72+
(see :ref:`cmd-example` below).
7173

7274
.. index::
7375
single: ? (question mark); in a command interpreter
@@ -117,7 +119,8 @@ A :class:`Cmd` instance has the following methods:
117119
.. method:: Cmd.emptyline()
118120

119121
Method called when an empty line is entered in response to the prompt. If this
120-
method is not overridden, it repeats the last nonempty command entered.
122+
method is not overridden, it repeats the last nonempty command entered. To
123+
suppress this behavior, override with a method that does nothing (``pass``).
121124

122125

123126
.. method:: Cmd.default(line)
@@ -306,6 +309,16 @@ immediate playback::
306309
bye()
307310
return True
308311

312+
# ----- shell behavior -----
313+
def emptyline(self):
314+
'Do not repeat the last command on empty input'
315+
pass
316+
def do_EOF(self, arg):
317+
'Exit on EOF (Ctrl-D): EOF'
318+
print()
319+
self.close()
320+
return True
321+
309322
# ----- record and playback -----
310323
def do_record(self, arg):
311324
'Save future commands to filename: RECORD rose.cmd'

0 commit comments

Comments
 (0)