From f9602fbe68a76570e2763d72eaf05412430174ad Mon Sep 17 00:00:00 2001 From: Anandesh Sharma Date: Fri, 27 Feb 2026 04:11:39 +0530 Subject: [PATCH] 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 --- Doc/library/cmd.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Doc/library/cmd.rst b/Doc/library/cmd.rst index c988fcebd68a01..7f98155c0b49de 100644 --- a/Doc/library/cmd.rst +++ b/Doc/library/cmd.rst @@ -68,6 +68,8 @@ A :class:`Cmd` instance has the following methods: cursor to the left non-destructively, etc.). An end-of-file on input is passed back as the string ``'EOF'``. + To handle this gracefully, define a :meth:`!do_EOF` method in your subclass + (see :ref:`cmd-example` below). .. index:: single: ? (question mark); in a command interpreter @@ -117,7 +119,8 @@ A :class:`Cmd` instance has the following methods: .. method:: Cmd.emptyline() Method called when an empty line is entered in response to the prompt. If this - method is not overridden, it repeats the last nonempty command entered. + method is not overridden, it repeats the last nonempty command entered. To + suppress this behavior, override with a method that does nothing (``pass``). .. method:: Cmd.default(line) @@ -306,6 +309,16 @@ immediate playback:: bye() return True + # ----- shell behavior ----- + def emptyline(self): + 'Do not repeat the last command on empty input' + pass + def do_EOF(self, arg): + 'Exit on EOF (Ctrl-D): EOF' + print() + self.close() + return True + # ----- record and playback ----- def do_record(self, arg): 'Save future commands to filename: RECORD rose.cmd'