gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

707 lines
54KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="cmd — Support for line-oriented command interpreters" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/cmd.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/cmd.py The Cmd class provides a simple framework for writing line-oriented command interpreters. These are often useful for test harnesses, administrative tools, and prototypes tha..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/cmd.py The Cmd class provides a simple framework for writing line-oriented command interpreters. These are often useful for test harnesses, administrative tools, and prototypes tha..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>cmd — Support for line-oriented command interpreters &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="shlex — Simple lexical analysis" href="shlex.html" />
  33. <link rel="prev" title="turtle — Turtle graphics" href="turtle.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/cmd.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> — Support for line-oriented command interpreters</a><ul>
  86. <li><a class="reference internal" href="#cmd-objects">Cmd Objects</a></li>
  87. <li><a class="reference internal" href="#cmd-example">Cmd Example</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="turtle.html"
  95. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code> — Turtle graphics</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="shlex.html"
  100. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code> — Simple lexical analysis</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/cmd.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="shlex.html" title="shlex — Simple lexical analysis"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="turtle.html" title="turtle — Turtle graphics"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="frameworks.html" accesskey="U">Program Frameworks</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> — Support for line-oriented command interpreters</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-cmd">
  170. <span id="cmd-support-for-line-oriented-command-interpreters"></span><h1><a class="reference internal" href="#module-cmd" title="cmd: Build line-oriented command interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code></a> — Support for line-oriented command interpreters<a class="headerlink" href="#module-cmd" title="Link to this heading">¶</a></h1>
  171. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/cmd.py">Lib/cmd.py</a></p>
  172. <hr class="docutils" />
  173. <p>The <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a> class provides a simple framework for writing line-oriented
  174. command interpreters. These are often useful for test harnesses, administrative
  175. tools, and prototypes that will later be wrapped in a more sophisticated
  176. interface.</p>
  177. <dl class="py class">
  178. <dt class="sig sig-object py" id="cmd.Cmd">
  179. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">cmd.</span></span><span class="sig-name descname"><span class="pre">Cmd</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">completekey</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'tab'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdin</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stdout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd" title="Link to this definition">¶</a></dt>
  180. <dd><p>A <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a> instance or subclass instance is a line-oriented interpreter
  181. framework. There is no good reason to instantiate <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a> itself; rather,
  182. it’s useful as a superclass of an interpreter class you define yourself in order
  183. to inherit <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a>’s methods and encapsulate action methods.</p>
  184. <p>The optional argument <em>completekey</em> is the <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a> name of a completion
  185. key; it defaults to <kbd class="kbd docutils literal notranslate">Tab</kbd>. If <em>completekey</em> is not <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> and
  186. <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a> is available, command completion is done automatically.</p>
  187. <p>The optional arguments <em>stdin</em> and <em>stdout</em> specify the input and output file
  188. objects that the Cmd instance or subclass instance will use for input and
  189. output. If not specified, they will default to <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a> and
  190. <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a>.</p>
  191. <p>If you want a given <em>stdin</em> to be used, make sure to set the instance’s
  192. <a class="reference internal" href="#cmd.Cmd.use_rawinput" title="cmd.Cmd.use_rawinput"><code class="xref py py-attr docutils literal notranslate"><span class="pre">use_rawinput</span></code></a> attribute to <code class="docutils literal notranslate"><span class="pre">False</span></code>, otherwise <em>stdin</em> will be
  193. ignored.</p>
  194. </dd></dl>
  195. <section id="cmd-objects">
  196. <span id="id1"></span><h2>Cmd Objects<a class="headerlink" href="#cmd-objects" title="Link to this heading">¶</a></h2>
  197. <p>A <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a> instance has the following methods:</p>
  198. <dl class="py method">
  199. <dt class="sig sig-object py" id="cmd.Cmd.cmdloop">
  200. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">cmdloop</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">intro</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.cmdloop" title="Link to this definition">¶</a></dt>
  201. <dd><p>Repeatedly issue a prompt, accept input, parse an initial prefix off the
  202. received input, and dispatch to action methods, passing them the remainder of
  203. the line as argument.</p>
  204. <p>The optional argument is a banner or intro string to be issued before the first
  205. prompt (this overrides the <a class="reference internal" href="#cmd.Cmd.intro" title="cmd.Cmd.intro"><code class="xref py py-attr docutils literal notranslate"><span class="pre">intro</span></code></a> class attribute).</p>
  206. <p>If the <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a> module is loaded, input will automatically inherit
  207. <strong class="program">bash</strong>-like history-list editing (e.g. <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">P</kbd></kbd> scrolls back
  208. to the last command, <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">N</kbd></kbd> forward to the next one, <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">F</kbd></kbd>
  209. moves the cursor to the right non-destructively, <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">B</kbd></kbd> moves the
  210. cursor to the left non-destructively, etc.).</p>
  211. <p>An end-of-file on input is passed back as the string <code class="docutils literal notranslate"><span class="pre">'EOF'</span></code>.</p>
  212. <p id="index-0">An interpreter instance will recognize a command name <code class="docutils literal notranslate"><span class="pre">foo</span></code> if and only if it
  213. has a method <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_foo()</span></code>. As a special case, a line beginning with the
  214. character <code class="docutils literal notranslate"><span class="pre">'?'</span></code> is dispatched to the method <a class="reference internal" href="#cmd.Cmd.do_help" title="cmd.Cmd.do_help"><code class="xref py py-meth docutils literal notranslate"><span class="pre">do_help()</span></code></a>. As another
  215. special case, a line beginning with the character <code class="docutils literal notranslate"><span class="pre">'!'</span></code> is dispatched to the
  216. method <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_shell()</span></code> (if such a method is defined).</p>
  217. <p>This method will return when the <a class="reference internal" href="#cmd.Cmd.postcmd" title="cmd.Cmd.postcmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">postcmd()</span></code></a> method returns a true value.
  218. The <em>stop</em> argument to <a class="reference internal" href="#cmd.Cmd.postcmd" title="cmd.Cmd.postcmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">postcmd()</span></code></a> is the return value from the command’s
  219. corresponding <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_*()</span></code> method.</p>
  220. <p>If completion is enabled, completing commands will be done automatically, and
  221. completing of commands args is done by calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">complete_foo()</span></code> with
  222. arguments <em>text</em>, <em>line</em>, <em>begidx</em>, and <em>endidx</em>. <em>text</em> is the string prefix
  223. we are attempting to match: all returned matches must begin with it. <em>line</em> is
  224. the current input line with leading whitespace removed, <em>begidx</em> and <em>endidx</em>
  225. are the beginning and ending indexes of the prefix text, which could be used to
  226. provide different completion depending upon which position the argument is in.</p>
  227. </dd></dl>
  228. <dl class="py method">
  229. <dt class="sig sig-object py" id="cmd.Cmd.do_help">
  230. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">do_help</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.do_help" title="Link to this definition">¶</a></dt>
  231. <dd><p>All subclasses of <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a> inherit a predefined <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_help()</span></code>. This
  232. method, called with an argument <code class="docutils literal notranslate"><span class="pre">'bar'</span></code>, invokes the corresponding method
  233. <code class="xref py py-meth docutils literal notranslate"><span class="pre">help_bar()</span></code>, and if that is not present, prints the docstring of
  234. <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_bar()</span></code>, if available. With no argument, <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_help()</span></code> lists all
  235. available help topics (that is, all commands with corresponding
  236. <code class="xref py py-meth docutils literal notranslate"><span class="pre">help_*()</span></code> methods or commands that have docstrings), and also lists any
  237. undocumented commands.</p>
  238. </dd></dl>
  239. <dl class="py method">
  240. <dt class="sig sig-object py" id="cmd.Cmd.onecmd">
  241. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">onecmd</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">str</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.onecmd" title="Link to this definition">¶</a></dt>
  242. <dd><p>Interpret the argument as though it had been typed in response to the prompt.
  243. This may be overridden, but should not normally need to be; see the
  244. <a class="reference internal" href="#cmd.Cmd.precmd" title="cmd.Cmd.precmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">precmd()</span></code></a> and <a class="reference internal" href="#cmd.Cmd.postcmd" title="cmd.Cmd.postcmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">postcmd()</span></code></a> methods for useful execution hooks. The
  245. return value is a flag indicating whether interpretation of commands by the
  246. interpreter should stop. If there is a <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_*()</span></code> method for the command
  247. <em>str</em>, the return value of that method is returned, otherwise the return value
  248. from the <a class="reference internal" href="#cmd.Cmd.default" title="cmd.Cmd.default"><code class="xref py py-meth docutils literal notranslate"><span class="pre">default()</span></code></a> method is returned.</p>
  249. </dd></dl>
  250. <dl class="py method">
  251. <dt class="sig sig-object py" id="cmd.Cmd.emptyline">
  252. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">emptyline</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.emptyline" title="Link to this definition">¶</a></dt>
  253. <dd><p>Method called when an empty line is entered in response to the prompt. If this
  254. method is not overridden, it repeats the last nonempty command entered.</p>
  255. </dd></dl>
  256. <dl class="py method">
  257. <dt class="sig sig-object py" id="cmd.Cmd.default">
  258. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">default</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">line</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.default" title="Link to this definition">¶</a></dt>
  259. <dd><p>Method called on an input line when the command prefix is not recognized. If
  260. this method is not overridden, it prints an error message and returns.</p>
  261. </dd></dl>
  262. <dl class="py method">
  263. <dt class="sig sig-object py" id="cmd.Cmd.completedefault">
  264. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">completedefault</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">line</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">begidx</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">endidx</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.completedefault" title="Link to this definition">¶</a></dt>
  265. <dd><p>Method called to complete an input line when no command-specific
  266. <code class="xref py py-meth docutils literal notranslate"><span class="pre">complete_*()</span></code> method is available. By default, it returns an empty list.</p>
  267. </dd></dl>
  268. <dl class="py method">
  269. <dt class="sig sig-object py" id="cmd.Cmd.columnize">
  270. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">columnize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">list</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">displaywidth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">80</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.columnize" title="Link to this definition">¶</a></dt>
  271. <dd><p>Method called to display a list of strings as a compact set of columns.
  272. Each column is only as wide as necessary.
  273. Columns are separated by two spaces for readability.</p>
  274. </dd></dl>
  275. <dl class="py method">
  276. <dt class="sig sig-object py" id="cmd.Cmd.precmd">
  277. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">precmd</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">line</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.precmd" title="Link to this definition">¶</a></dt>
  278. <dd><p>Hook method executed just before the command line <em>line</em> is interpreted, but
  279. after the input prompt is generated and issued. This method is a stub in
  280. <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a>; it exists to be overridden by subclasses. The return value is
  281. used as the command which will be executed by the <a class="reference internal" href="#cmd.Cmd.onecmd" title="cmd.Cmd.onecmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">onecmd()</span></code></a> method; the
  282. <a class="reference internal" href="#cmd.Cmd.precmd" title="cmd.Cmd.precmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">precmd()</span></code></a> implementation may re-write the command or simply return <em>line</em>
  283. unchanged.</p>
  284. </dd></dl>
  285. <dl class="py method">
  286. <dt class="sig sig-object py" id="cmd.Cmd.postcmd">
  287. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">postcmd</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stop</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">line</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.postcmd" title="Link to this definition">¶</a></dt>
  288. <dd><p>Hook method executed just after a command dispatch is finished. This method is
  289. a stub in <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a>; it exists to be overridden by subclasses. <em>line</em> is the
  290. command line which was executed, and <em>stop</em> is a flag which indicates whether
  291. execution will be terminated after the call to <a class="reference internal" href="#cmd.Cmd.postcmd" title="cmd.Cmd.postcmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">postcmd()</span></code></a>; this will be the
  292. return value of the <a class="reference internal" href="#cmd.Cmd.onecmd" title="cmd.Cmd.onecmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">onecmd()</span></code></a> method. The return value of this method will
  293. be used as the new value for the internal flag which corresponds to <em>stop</em>;
  294. returning false will cause interpretation to continue.</p>
  295. </dd></dl>
  296. <dl class="py method">
  297. <dt class="sig sig-object py" id="cmd.Cmd.preloop">
  298. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">preloop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.preloop" title="Link to this definition">¶</a></dt>
  299. <dd><p>Hook method executed once when <a class="reference internal" href="#cmd.Cmd.cmdloop" title="cmd.Cmd.cmdloop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cmdloop()</span></code></a> is called. This method is a stub
  300. in <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a>; it exists to be overridden by subclasses.</p>
  301. </dd></dl>
  302. <dl class="py method">
  303. <dt class="sig sig-object py" id="cmd.Cmd.postloop">
  304. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">postloop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cmd.Cmd.postloop" title="Link to this definition">¶</a></dt>
  305. <dd><p>Hook method executed once when <a class="reference internal" href="#cmd.Cmd.cmdloop" title="cmd.Cmd.cmdloop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cmdloop()</span></code></a> is about to return. This method
  306. is a stub in <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a>; it exists to be overridden by subclasses.</p>
  307. </dd></dl>
  308. <p>Instances of <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a> subclasses have some public instance variables:</p>
  309. <dl class="py attribute">
  310. <dt class="sig sig-object py" id="cmd.Cmd.prompt">
  311. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">prompt</span></span><a class="headerlink" href="#cmd.Cmd.prompt" title="Link to this definition">¶</a></dt>
  312. <dd><p>The prompt issued to solicit input.</p>
  313. </dd></dl>
  314. <dl class="py attribute">
  315. <dt class="sig sig-object py" id="cmd.Cmd.identchars">
  316. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">identchars</span></span><a class="headerlink" href="#cmd.Cmd.identchars" title="Link to this definition">¶</a></dt>
  317. <dd><p>The string of characters accepted for the command prefix.</p>
  318. </dd></dl>
  319. <dl class="py attribute">
  320. <dt class="sig sig-object py" id="cmd.Cmd.lastcmd">
  321. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">lastcmd</span></span><a class="headerlink" href="#cmd.Cmd.lastcmd" title="Link to this definition">¶</a></dt>
  322. <dd><p>The last nonempty command prefix seen.</p>
  323. </dd></dl>
  324. <dl class="py attribute">
  325. <dt class="sig sig-object py" id="cmd.Cmd.cmdqueue">
  326. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">cmdqueue</span></span><a class="headerlink" href="#cmd.Cmd.cmdqueue" title="Link to this definition">¶</a></dt>
  327. <dd><p>A list of queued input lines. The cmdqueue list is checked in
  328. <a class="reference internal" href="#cmd.Cmd.cmdloop" title="cmd.Cmd.cmdloop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cmdloop()</span></code></a> when new input is needed; if it is nonempty, its elements
  329. will be processed in order, as if entered at the prompt.</p>
  330. </dd></dl>
  331. <dl class="py attribute">
  332. <dt class="sig sig-object py" id="cmd.Cmd.intro">
  333. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">intro</span></span><a class="headerlink" href="#cmd.Cmd.intro" title="Link to this definition">¶</a></dt>
  334. <dd><p>A string to issue as an intro or banner. May be overridden by giving the
  335. <a class="reference internal" href="#cmd.Cmd.cmdloop" title="cmd.Cmd.cmdloop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cmdloop()</span></code></a> method an argument.</p>
  336. </dd></dl>
  337. <dl class="py attribute">
  338. <dt class="sig sig-object py" id="cmd.Cmd.doc_header">
  339. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">doc_header</span></span><a class="headerlink" href="#cmd.Cmd.doc_header" title="Link to this definition">¶</a></dt>
  340. <dd><p>The header to issue if the help output has a section for documented commands.</p>
  341. </dd></dl>
  342. <dl class="py attribute">
  343. <dt class="sig sig-object py" id="cmd.Cmd.misc_header">
  344. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">misc_header</span></span><a class="headerlink" href="#cmd.Cmd.misc_header" title="Link to this definition">¶</a></dt>
  345. <dd><p>The header to issue if the help output has a section for miscellaneous help
  346. topics (that is, there are <code class="xref py py-meth docutils literal notranslate"><span class="pre">help_*()</span></code> methods without corresponding
  347. <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_*()</span></code> methods).</p>
  348. </dd></dl>
  349. <dl class="py attribute">
  350. <dt class="sig sig-object py" id="cmd.Cmd.undoc_header">
  351. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">undoc_header</span></span><a class="headerlink" href="#cmd.Cmd.undoc_header" title="Link to this definition">¶</a></dt>
  352. <dd><p>The header to issue if the help output has a section for undocumented commands
  353. (that is, there are <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_*()</span></code> methods without corresponding <code class="xref py py-meth docutils literal notranslate"><span class="pre">help_*()</span></code>
  354. methods).</p>
  355. </dd></dl>
  356. <dl class="py attribute">
  357. <dt class="sig sig-object py" id="cmd.Cmd.ruler">
  358. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">ruler</span></span><a class="headerlink" href="#cmd.Cmd.ruler" title="Link to this definition">¶</a></dt>
  359. <dd><p>The character used to draw separator lines under the help-message headers. If
  360. empty, no ruler line is drawn. It defaults to <code class="docutils literal notranslate"><span class="pre">'='</span></code>.</p>
  361. </dd></dl>
  362. <dl class="py attribute">
  363. <dt class="sig sig-object py" id="cmd.Cmd.use_rawinput">
  364. <span class="sig-prename descclassname"><span class="pre">Cmd.</span></span><span class="sig-name descname"><span class="pre">use_rawinput</span></span><a class="headerlink" href="#cmd.Cmd.use_rawinput" title="Link to this definition">¶</a></dt>
  365. <dd><p>A flag, defaulting to true. If true, <a class="reference internal" href="#cmd.Cmd.cmdloop" title="cmd.Cmd.cmdloop"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cmdloop()</span></code></a> uses <a class="reference internal" href="functions.html#input" title="input"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a> to
  366. display a prompt and read the next command; if false, <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout.write()</span></code></a>
  367. and <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin.readline()</span></code></a> are used. (This means that by importing
  368. <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a>, on systems that support it, the interpreter will automatically
  369. support <strong class="program">Emacs</strong>-like line editing and command-history keystrokes.)</p>
  370. </dd></dl>
  371. </section>
  372. <section id="cmd-example">
  373. <span id="id2"></span><h2>Cmd Example<a class="headerlink" href="#cmd-example" title="Link to this heading">¶</a></h2>
  374. <p>The <a class="reference internal" href="#module-cmd" title="cmd: Build line-oriented command interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code></a> module is mainly useful for building custom shells that let a
  375. user work with a program interactively.</p>
  376. <p>This section presents a simple example of how to build a shell around a few of
  377. the commands in the <a class="reference internal" href="turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a> module.</p>
  378. <p>Basic turtle commands such as <a class="reference internal" href="turtle.html#turtle.forward" title="turtle.forward"><code class="xref py py-meth docutils literal notranslate"><span class="pre">forward()</span></code></a> are added to a
  379. <a class="reference internal" href="#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code></a> subclass with method named <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_forward()</span></code>. The argument is
  380. converted to a number and dispatched to the turtle module. The docstring is
  381. used in the help utility provided by the shell.</p>
  382. <p>The example also includes a basic record and playback facility implemented with
  383. the <a class="reference internal" href="#cmd.Cmd.precmd" title="cmd.Cmd.precmd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">precmd()</span></code></a> method which is responsible for converting the input to
  384. lowercase and writing the commands to a file. The <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_playback()</span></code> method
  385. reads the file and adds the recorded commands to the <a class="reference internal" href="#cmd.Cmd.cmdqueue" title="cmd.Cmd.cmdqueue"><code class="xref py py-attr docutils literal notranslate"><span class="pre">cmdqueue</span></code></a> for
  386. immediate playback:</p>
  387. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cmd</span><span class="o">,</span> <span class="nn">sys</span>
  388. <span class="kn">from</span> <span class="nn">turtle</span> <span class="kn">import</span> <span class="o">*</span>
  389. <span class="k">class</span> <span class="nc">TurtleShell</span><span class="p">(</span><span class="n">cmd</span><span class="o">.</span><span class="n">Cmd</span><span class="p">):</span>
  390. <span class="n">intro</span> <span class="o">=</span> <span class="s1">&#39;Welcome to the turtle shell. Type help or ? to list commands.</span><span class="se">\n</span><span class="s1">&#39;</span>
  391. <span class="n">prompt</span> <span class="o">=</span> <span class="s1">&#39;(turtle) &#39;</span>
  392. <span class="n">file</span> <span class="o">=</span> <span class="kc">None</span>
  393. <span class="c1"># ----- basic turtle commands -----</span>
  394. <span class="k">def</span> <span class="nf">do_forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  395. <span class="s1">&#39;Move the turtle forward by the specified distance: FORWARD 10&#39;</span>
  396. <span class="n">forward</span><span class="p">(</span><span class="o">*</span><span class="n">parse</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
  397. <span class="k">def</span> <span class="nf">do_right</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  398. <span class="s1">&#39;Turn turtle right by given number of degrees: RIGHT 20&#39;</span>
  399. <span class="n">right</span><span class="p">(</span><span class="o">*</span><span class="n">parse</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
  400. <span class="k">def</span> <span class="nf">do_left</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  401. <span class="s1">&#39;Turn turtle left by given number of degrees: LEFT 90&#39;</span>
  402. <span class="n">left</span><span class="p">(</span><span class="o">*</span><span class="n">parse</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
  403. <span class="k">def</span> <span class="nf">do_goto</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  404. <span class="s1">&#39;Move turtle to an absolute position with changing orientation. GOTO 100 200&#39;</span>
  405. <span class="n">goto</span><span class="p">(</span><span class="o">*</span><span class="n">parse</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
  406. <span class="k">def</span> <span class="nf">do_home</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  407. <span class="s1">&#39;Return turtle to the home position: HOME&#39;</span>
  408. <span class="n">home</span><span class="p">()</span>
  409. <span class="k">def</span> <span class="nf">do_circle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  410. <span class="s1">&#39;Draw circle with given radius an options extent and steps: CIRCLE 50&#39;</span>
  411. <span class="n">circle</span><span class="p">(</span><span class="o">*</span><span class="n">parse</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span>
  412. <span class="k">def</span> <span class="nf">do_position</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  413. <span class="s1">&#39;Print the current turtle position: POSITION&#39;</span>
  414. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Current position is </span><span class="si">%d</span><span class="s1"> </span><span class="si">%d</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">position</span><span class="p">())</span>
  415. <span class="k">def</span> <span class="nf">do_heading</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  416. <span class="s1">&#39;Print the current turtle heading in degrees: HEADING&#39;</span>
  417. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Current heading is </span><span class="si">%d</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">heading</span><span class="p">(),))</span>
  418. <span class="k">def</span> <span class="nf">do_color</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  419. <span class="s1">&#39;Set the color: COLOR BLUE&#39;</span>
  420. <span class="n">color</span><span class="p">(</span><span class="n">arg</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
  421. <span class="k">def</span> <span class="nf">do_undo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  422. <span class="s1">&#39;Undo (repeatedly) the last turtle action(s): UNDO&#39;</span>
  423. <span class="k">def</span> <span class="nf">do_reset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  424. <span class="s1">&#39;Clear the screen and return turtle to center: RESET&#39;</span>
  425. <span class="n">reset</span><span class="p">()</span>
  426. <span class="k">def</span> <span class="nf">do_bye</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  427. <span class="s1">&#39;Stop recording, close the turtle window, and exit: BYE&#39;</span>
  428. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Thank you for using Turtle&#39;</span><span class="p">)</span>
  429. <span class="bp">self</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  430. <span class="n">bye</span><span class="p">()</span>
  431. <span class="k">return</span> <span class="kc">True</span>
  432. <span class="c1"># ----- record and playback -----</span>
  433. <span class="k">def</span> <span class="nf">do_record</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  434. <span class="s1">&#39;Save future commands to filename: RECORD rose.cmd&#39;</span>
  435. <span class="bp">self</span><span class="o">.</span><span class="n">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span>
  436. <span class="k">def</span> <span class="nf">do_playback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
  437. <span class="s1">&#39;Playback commands from a file: PLAYBACK rose.cmd&#39;</span>
  438. <span class="bp">self</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  439. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  440. <span class="bp">self</span><span class="o">.</span><span class="n">cmdqueue</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">())</span>
  441. <span class="k">def</span> <span class="nf">precmd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
  442. <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
  443. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">file</span> <span class="ow">and</span> <span class="s1">&#39;playback&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
  444. <span class="nb">print</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="p">)</span>
  445. <span class="k">return</span> <span class="n">line</span>
  446. <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  447. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="p">:</span>
  448. <span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  449. <span class="bp">self</span><span class="o">.</span><span class="n">file</span> <span class="o">=</span> <span class="kc">None</span>
  450. <span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span>
  451. <span class="s1">&#39;Convert a series of zero or more numbers to an argument tuple&#39;</span>
  452. <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">arg</span><span class="o">.</span><span class="n">split</span><span class="p">()))</span>
  453. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  454. <span class="n">TurtleShell</span><span class="p">()</span><span class="o">.</span><span class="n">cmdloop</span><span class="p">()</span>
  455. </pre></div>
  456. </div>
  457. <p>Here is a sample session with the turtle shell showing the help functions, using
  458. blank lines to repeat commands, and the simple record and playback facility:</p>
  459. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Welcome to the turtle shell. Type help or ? to list commands.
  460. (turtle) ?
  461. Documented commands (type help &lt;topic&gt;):
  462. ========================================
  463. bye color goto home playback record right
  464. circle forward heading left position reset undo
  465. (turtle) help forward
  466. Move the turtle forward by the specified distance: FORWARD 10
  467. (turtle) record spiral.cmd
  468. (turtle) position
  469. Current position is 0 0
  470. (turtle) heading
  471. Current heading is 0
  472. (turtle) reset
  473. (turtle) circle 20
  474. (turtle) right 30
  475. (turtle) circle 40
  476. (turtle) right 30
  477. (turtle) circle 60
  478. (turtle) right 30
  479. (turtle) circle 80
  480. (turtle) right 30
  481. (turtle) circle 100
  482. (turtle) right 30
  483. (turtle) circle 120
  484. (turtle) right 30
  485. (turtle) circle 120
  486. (turtle) heading
  487. Current heading is 180
  488. (turtle) forward 100
  489. (turtle)
  490. (turtle) right 90
  491. (turtle) forward 100
  492. (turtle)
  493. (turtle) right 90
  494. (turtle) forward 400
  495. (turtle) right 90
  496. (turtle) forward 500
  497. (turtle) right 90
  498. (turtle) forward 400
  499. (turtle) right 90
  500. (turtle) forward 300
  501. (turtle) playback spiral.cmd
  502. Current position is 0 0
  503. Current heading is 0
  504. Current heading is 180
  505. (turtle) bye
  506. Thank you for using Turtle
  507. </pre></div>
  508. </div>
  509. </section>
  510. </section>
  511. <div class="clearer"></div>
  512. </div>
  513. </div>
  514. </div>
  515. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  516. <div class="sphinxsidebarwrapper">
  517. <div>
  518. <h3><a href="../contents.html">Table of Contents</a></h3>
  519. <ul>
  520. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> — Support for line-oriented command interpreters</a><ul>
  521. <li><a class="reference internal" href="#cmd-objects">Cmd Objects</a></li>
  522. <li><a class="reference internal" href="#cmd-example">Cmd Example</a></li>
  523. </ul>
  524. </li>
  525. </ul>
  526. </div>
  527. <div>
  528. <h4>Previous topic</h4>
  529. <p class="topless"><a href="turtle.html"
  530. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code> — Turtle graphics</a></p>
  531. </div>
  532. <div>
  533. <h4>Next topic</h4>
  534. <p class="topless"><a href="shlex.html"
  535. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code> — Simple lexical analysis</a></p>
  536. </div>
  537. <div role="note" aria-label="source link">
  538. <h3>This Page</h3>
  539. <ul class="this-page-menu">
  540. <li><a href="../bugs.html">Report a Bug</a></li>
  541. <li>
  542. <a href="https://github.com/python/cpython/blob/main/Doc/library/cmd.rst"
  543. rel="nofollow">Show Source
  544. </a>
  545. </li>
  546. </ul>
  547. </div>
  548. </div>
  549. <div id="sidebarbutton" title="Collapse sidebar">
  550. <span>«</span>
  551. </div>
  552. </div>
  553. <div class="clearer"></div>
  554. </div>
  555. <div class="related" role="navigation" aria-label="related navigation">
  556. <h3>Navigation</h3>
  557. <ul>
  558. <li class="right" style="margin-right: 10px">
  559. <a href="../genindex.html" title="General Index"
  560. >index</a></li>
  561. <li class="right" >
  562. <a href="../py-modindex.html" title="Python Module Index"
  563. >modules</a> |</li>
  564. <li class="right" >
  565. <a href="shlex.html" title="shlex — Simple lexical analysis"
  566. >next</a> |</li>
  567. <li class="right" >
  568. <a href="turtle.html" title="turtle — Turtle graphics"
  569. >previous</a> |</li>
  570. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  571. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  572. <li class="switchers">
  573. <div class="language_switcher_placeholder"></div>
  574. <div class="version_switcher_placeholder"></div>
  575. </li>
  576. <li>
  577. </li>
  578. <li id="cpython-language-and-version">
  579. <a href="../index.html">3.12.3 Documentation</a> &#187;
  580. </li>
  581. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  582. <li class="nav-item nav-item-2"><a href="frameworks.html" >Program Frameworks</a> &#187;</li>
  583. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> — Support for line-oriented command interpreters</a></li>
  584. <li class="right">
  585. <div class="inline-search" role="search">
  586. <form class="inline-search" action="../search.html" method="get">
  587. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  588. <input type="submit" value="Go" />
  589. </form>
  590. </div>
  591. |
  592. </li>
  593. <li class="right">
  594. <label class="theme-selector-label">
  595. Theme
  596. <select class="theme-selector" oninput="activateTheme(this.value)">
  597. <option value="auto" selected>Auto</option>
  598. <option value="light">Light</option>
  599. <option value="dark">Dark</option>
  600. </select>
  601. </label> |</li>
  602. </ul>
  603. </div>
  604. <div class="footer">
  605. &copy;
  606. <a href="../copyright.html">
  607. Copyright
  608. </a>
  609. 2001-2024, Python Software Foundation.
  610. <br />
  611. This page is licensed under the Python Software Foundation License Version 2.
  612. <br />
  613. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  614. <br />
  615. See <a href="/license.html">History and License</a> for more information.<br />
  616. <br />
  617. The Python Software Foundation is a non-profit corporation.
  618. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  619. <br />
  620. <br />
  621. Last updated on Apr 09, 2024 (13:47 UTC).
  622. <a href="/bugs.html">Found a bug</a>?
  623. <br />
  624. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  625. </div>
  626. </body>
  627. </html>
上海开阖软件有限公司 沪ICP备12045867号-1