Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
cmd.html751 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="cmd — Support for line-oriented command interpreters" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/cmd.html" />10<meta property="og:site_name" content="Python documentation" />11<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..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_cmd_2429b9ea.png" />15<meta property="og:image:alt" 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..." />16<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..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>cmd — Support for line-oriented command interpreters &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="Concurrent Execution" href="concurrency.html" />43    <link rel="prev" title="curses.panel — A panel stack extension for curses" href="curses.panel.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/cmd.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <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>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h3><a href="../contents.html">Table of Contents</a></h3>106    <ul>107<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>108<li><a class="reference internal" href="#cmd-objects">Cmd Objects</a></li>109<li><a class="reference internal" href="#cmd-example">Cmd Example</a></li>110</ul>111</li>112</ul>113 114  </div>115  <div>116    <h4>Previous topic</h4>117    <p class="topless"><a href="curses.panel.html"118                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses.panel</span></code> — A panel stack extension for curses</a></p>119  </div>120  <div>121    <h4>Next topic</h4>122    <p class="topless"><a href="concurrency.html"123                          title="next chapter">Concurrent Execution</a></p>124  </div>125  <script>126    document.addEventListener('DOMContentLoaded', () => {127        const title = document.querySelector('meta[property="og:title"]').content;128        const elements = document.querySelectorAll('.improvepage');129        const pageurl = window.location.href.split('?')[0];130        elements.forEach(element => {131            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));132            url.searchParams.set('pagetitle', title);133            url.searchParams.set('pageurl', pageurl);134            url.searchParams.set('pagesource', "library/cmd.rst");135            element.href = url.toString();136        });137    });138  </script>139  <div role="note" aria-label="source link">140    <h3>This page</h3>141    <ul class="this-page-menu">142      <li><a href="../bugs.html">Report a bug</a></li>143      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>144      <li>145        <a href="https://github.com/python/cpython/blob/main/Doc/library/cmd.rst?plain=1"146            rel="nofollow">Show source147        </a>148      </li>149      150    </ul>151  </div>152        </nav>153    </div>154</div>155 156  157    <div class="related" role="navigation" aria-label="Related">158      <h3>Navigation</h3>159      <ul>160        <li class="right" style="margin-right: 10px">161          <a href="../genindex.html" title="General Index"162             accesskey="I">index</a></li>163        <li class="right" >164          <a href="../py-modindex.html" title="Python Module Index"165             >modules</a> |</li>166        <li class="right" >167          <a href="concurrency.html" title="Concurrent Execution"168             accesskey="N">next</a> |</li>169        <li class="right" >170          <a href="curses.panel.html" title="curses.panel — A panel stack extension for curses"171             accesskey="P">previous</a> |</li>172 173          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>174          <li><a href="https://www.python.org/">Python</a> &#187;</li>175          <li class="switchers">176            <div class="language_switcher_placeholder"></div>177            <div class="version_switcher_placeholder"></div>178          </li>179          <li>180              181          </li>182    <li id="cpython-language-and-version">183      <a href="../index.html">3.15.0a6 Documentation</a> &#187;184    </li>185 186          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>187          <li class="nav-item nav-item-2"><a href="cmdlinelibs.html" accesskey="U">Command-line interface libraries</a> &#187;</li>188        <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>189                <li class="right">190                    191 192    <div class="inline-search" role="search">193        <form class="inline-search" action="../search.html" method="get">194          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">195          <input type="submit" value="Go">196        </form>197    </div>198                     |199                </li>200            <li class="right">201<label class="theme-selector-label">202    Theme203    <select class="theme-selector" oninput="activateTheme(this.value)">204        <option value="auto" selected>Auto</option>205        <option value="light">Light</option>206        <option value="dark">Dark</option>207    </select>208</label> |</li>209            210      </ul>211    </div>    212 213    <div class="document">214      <div class="documentwrapper">215        <div class="bodywrapper">216          <div class="body" role="main">217            218  <section id="module-cmd">219<span id="cmd-support-for-line-oriented-command-interpreters"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> — Support for line-oriented command interpreters<a class="headerlink" href="#module-cmd" title="Link to this heading">¶</a></h1>220<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/cmd.py">Lib/cmd.py</a></p>221<hr class="docutils" />222<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-oriented223command interpreters.  These are often useful for test harnesses, administrative224tools, and prototypes that will later be wrapped in a more sophisticated225interface.</p>226<dl class="py class">227<dt class="sig sig-object py" id="cmd.Cmd">228<em class="property"><span class="k"><span class="pre">class</span></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>229<dd><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code> instance or subclass instance is a line-oriented interpreter230framework.  There is no good reason to instantiate <code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code> itself; rather,231it’s useful as a superclass of an interpreter class you define yourself in order232to inherit <code class="xref py py-class docutils literal notranslate"><span class="pre">Cmd</span></code>’s methods and encapsulate action methods.</p>233<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."><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a> name of a completion234key; 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> and235<code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code> is available, command completion is done automatically.</p>236<p>The default, <code class="docutils literal notranslate"><span class="pre">'tab'</span></code>, is treated specially, so that it refers to the237<kbd class="kbd docutils literal notranslate">Tab</kbd> key on every <a class="reference internal" href="readline.html#readline.backend" title="readline.backend"><code class="xref py py-data docutils literal notranslate"><span class="pre">readline.backend</span></code></a>.238Specifically, if <code class="xref py py-data docutils literal notranslate"><span class="pre">readline.backend</span></code> is <code class="docutils literal notranslate"><span class="pre">editline</span></code>,239<code class="docutils literal notranslate"><span class="pre">Cmd</span></code> will use <code class="docutils literal notranslate"><span class="pre">'^I'</span></code> instead of <code class="docutils literal notranslate"><span class="pre">'tab'</span></code>.240Note that other values are not treated this way, and might only work241with a specific backend.</p>242<p>The optional arguments <em>stdin</em> and <em>stdout</em> specify the  input and output file243objects that the Cmd instance or subclass  instance will use for input and244output. 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> and245<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>246<p>If you want a given <em>stdin</em> to be used, make sure to set the instance’s247<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 be248ignored.</p>249<div class="versionchanged">250<p><span class="versionmodified changed">Changed in version 3.13: </span><code class="docutils literal notranslate"><span class="pre">completekey='tab'</span></code> is replaced by <code class="docutils literal notranslate"><span class="pre">'^I'</span></code> for <code class="docutils literal notranslate"><span class="pre">editline</span></code>.</p>251</div>252</dd></dl>253 254<section id="cmd-objects">255<span id="id1"></span><h2>Cmd Objects<a class="headerlink" href="#cmd-objects" title="Link to this heading">¶</a></h2>256<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>257<dl class="py method">258<dt class="sig sig-object py" id="cmd.Cmd.cmdloop">259<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>260<dd><p>Repeatedly issue a prompt, accept input, parse an initial prefix off the261received input, and dispatch to action methods, passing them the remainder of262the line as argument.</p>263<p>The optional argument is a banner or intro string to be issued before the first264prompt (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>265<p>If the <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a> module is loaded, input will automatically inherit266<strong class="program">bash</strong>-like history-list editing (e.g. <kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">P</kbd> scrolls back267to the last command, <kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">N</kbd> forward to the next one, <kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">F</kbd>268moves the cursor to the right non-destructively, <kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">B</kbd> moves the269cursor to the left non-destructively, etc.).</p>270<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>271<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 it272has 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 the273character <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 another274special case, a line beginning with the character <code class="docutils literal notranslate"><span class="pre">'!'</span></code> is dispatched to the275method <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_shell()</span></code> (if such a method is defined).</p>276<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.277The <em>stop</em> argument to <code class="xref py py-meth docutils literal notranslate"><span class="pre">postcmd()</span></code> is the return value from the command’s278corresponding <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_*()</span></code> method.</p>279<p>If completion is enabled, completing commands will be done automatically, and280completing of commands args is done by calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">complete_foo()</span></code> with281arguments <em>text</em>, <em>line</em>, <em>begidx</em>, and <em>endidx</em>.  <em>text</em> is the string prefix282we are attempting to match: all returned matches must begin with it. <em>line</em> is283the current input line with leading whitespace removed, <em>begidx</em> and <em>endidx</em>284are the beginning and ending indexes of the prefix text, which could be used to285provide different completion depending upon which position the argument is in.</p>286</dd></dl>287 288<dl class="py method">289<dt class="sig sig-object py" id="cmd.Cmd.do_help">290<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>291<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>.  This292method, called with an argument <code class="docutils literal notranslate"><span class="pre">'bar'</span></code>, invokes the corresponding method293<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 of294<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 all295available help topics (that is, all commands with corresponding296<code class="xref py py-meth docutils literal notranslate"><span class="pre">help_*()</span></code> methods or commands that have docstrings), and also lists any297undocumented commands.</p>298</dd></dl>299 300<dl class="py method">301<dt class="sig sig-object py" id="cmd.Cmd.onecmd">302<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>303<dd><p>Interpret the argument as though it had been typed in response to the prompt.304This may be overridden, but should not normally need to be; see the305<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.  The306return value is a flag indicating whether interpretation of commands by the307interpreter should stop.  If there is a <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_*()</span></code> method for the command308<em>str</em>, the return value of that method is returned, otherwise the return value309from 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>310</dd></dl>311 312<dl class="py method">313<dt class="sig sig-object py" id="cmd.Cmd.emptyline">314<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>315<dd><p>Method called when an empty line is entered in response to the prompt. If this316method is not overridden, it repeats the last nonempty command entered.</p>317</dd></dl>318 319<dl class="py method">320<dt class="sig sig-object py" id="cmd.Cmd.default">321<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>322<dd><p>Method called on an input line when the command prefix is not recognized. If323this method is not overridden, it prints an error message and returns.</p>324</dd></dl>325 326<dl class="py method">327<dt class="sig sig-object py" id="cmd.Cmd.completedefault">328<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>329<dd><p>Method called to complete an input line when no command-specific330<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>331</dd></dl>332 333<dl class="py method">334<dt class="sig sig-object py" id="cmd.Cmd.columnize">335<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>336<dd><p>Method called to display a list of strings as a compact set of columns.337Each column is only as wide as necessary.338Columns are separated by two spaces for readability.</p>339</dd></dl>340 341<dl class="py method">342<dt class="sig sig-object py" id="cmd.Cmd.precmd">343<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>344<dd><p>Hook method executed just before the command line <em>line</em> is interpreted, but345after the input prompt is generated and issued.  This method is a stub in346<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 is347used 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; the348<code class="xref py py-meth docutils literal notranslate"><span class="pre">precmd()</span></code> implementation may re-write the command or simply return <em>line</em>349unchanged.</p>350</dd></dl>351 352<dl class="py method">353<dt class="sig sig-object py" id="cmd.Cmd.postcmd">354<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>355<dd><p>Hook method executed just after a command dispatch is finished.  This method is356a 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 the357command line which was executed, and <em>stop</em> is a flag which indicates whether358execution will be terminated after the call to <code class="xref py py-meth docutils literal notranslate"><span class="pre">postcmd()</span></code>; this will be the359return 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 will360be used as the new value for the internal flag which corresponds to <em>stop</em>;361returning false will cause interpretation to continue.</p>362</dd></dl>363 364<dl class="py method">365<dt class="sig sig-object py" id="cmd.Cmd.preloop">366<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>367<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 stub368in <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>369</dd></dl>370 371<dl class="py method">372<dt class="sig sig-object py" id="cmd.Cmd.postloop">373<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>374<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 method375is 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>376</dd></dl>377 378<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>379<dl class="py attribute">380<dt class="sig sig-object py" id="cmd.Cmd.prompt">381<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>382<dd><p>The prompt issued to solicit input.</p>383</dd></dl>384 385<dl class="py attribute">386<dt class="sig sig-object py" id="cmd.Cmd.identchars">387<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>388<dd><p>The string of characters accepted for the command prefix.</p>389</dd></dl>390 391<dl class="py attribute">392<dt class="sig sig-object py" id="cmd.Cmd.lastcmd">393<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>394<dd><p>The last nonempty command prefix seen.</p>395</dd></dl>396 397<dl class="py attribute">398<dt class="sig sig-object py" id="cmd.Cmd.cmdqueue">399<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>400<dd><p>A list of queued input lines.  The cmdqueue list is checked in401<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 elements402will be processed in order, as if entered at the prompt.</p>403</dd></dl>404 405<dl class="py attribute">406<dt class="sig sig-object py" id="cmd.Cmd.intro">407<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>408<dd><p>A string to issue as an intro or banner.  May be overridden by giving the409<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>410</dd></dl>411 412<dl class="py attribute">413<dt class="sig sig-object py" id="cmd.Cmd.doc_header">414<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>415<dd><p>The header to issue if the help output has a section for documented commands.</p>416</dd></dl>417 418<dl class="py attribute">419<dt class="sig sig-object py" id="cmd.Cmd.misc_header">420<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>421<dd><p>The header to issue if the help output has a section for miscellaneous  help422topics (that is, there are <code class="xref py py-meth docutils literal notranslate"><span class="pre">help_*()</span></code> methods without corresponding423<code class="xref py py-meth docutils literal notranslate"><span class="pre">do_*()</span></code> methods).</p>424</dd></dl>425 426<dl class="py attribute">427<dt class="sig sig-object py" id="cmd.Cmd.undoc_header">428<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>429<dd><p>The header to issue if the help output has a section for undocumented  commands430(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>431methods).</p>432</dd></dl>433 434<dl class="py attribute">435<dt class="sig sig-object py" id="cmd.Cmd.ruler">436<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>437<dd><p>The character used to draw separator lines under the help-message headers.  If438empty, no ruler line is drawn.  It defaults to <code class="docutils literal notranslate"><span class="pre">'='</span></code>.</p>439</dd></dl>440 441<dl class="py attribute">442<dt class="sig sig-object py" id="cmd.Cmd.use_rawinput">443<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>444<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> to445display 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>446and <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 importing447<a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a>, on systems that support it, the interpreter will automatically448support <strong class="program">Emacs</strong>-like line editing  and command-history keystrokes.)</p>449</dd></dl>450 451</section>452<section id="cmd-example">453<span id="id2"></span><h2>Cmd Example<a class="headerlink" href="#cmd-example" title="Link to this heading">¶</a></h2>454<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> module is mainly useful for building custom shells that let a455user work with a program interactively.</p>456<p>This section presents a simple example of how to build a shell around a few of457the 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>458<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 a459<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 is460converted to a number and dispatched to the turtle module.  The docstring is461used in the help utility provided by the shell.</p>462<p>The example also includes a basic record and playback facility implemented with463the <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 to464lowercase and writing the commands to a file.  The <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_playback()</span></code> method465reads 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> for466immediate playback:</p>467<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">cmd</span><span class="o">,</span><span class="w"> </span><span class="nn">sys</span>468<span class="kn">from</span><span class="w"> </span><span class="nn">turtle</span><span class="w"> </span><span class="kn">import</span> <span class="o">*</span>469 470<span class="k">class</span><span class="w"> </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>471    <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>472    <span class="n">prompt</span> <span class="o">=</span> <span class="s1">&#39;(turtle) &#39;</span>473    <span class="n">file</span> <span class="o">=</span> <span class="kc">None</span>474 475    <span class="c1"># ----- basic turtle commands -----</span>476    <span class="k">def</span><span class="w"> </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>477        <span class="s1">&#39;Move the turtle forward by the specified distance:  FORWARD 10&#39;</span>478        <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>479    <span class="k">def</span><span class="w"> </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>480        <span class="s1">&#39;Turn turtle right by given number of degrees:  RIGHT 20&#39;</span>481        <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>482    <span class="k">def</span><span class="w"> </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>483        <span class="s1">&#39;Turn turtle left by given number of degrees:  LEFT 90&#39;</span>484        <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>485    <span class="k">def</span><span class="w"> </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>486        <span class="s1">&#39;Move turtle to an absolute position with changing orientation.  GOTO 100 200&#39;</span>487        <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>488    <span class="k">def</span><span class="w"> </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>489        <span class="s1">&#39;Return turtle to the home position:  HOME&#39;</span>490        <span class="n">home</span><span class="p">()</span>491    <span class="k">def</span><span class="w"> </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>492        <span class="s1">&#39;Draw circle with given radius an options extent and steps:  CIRCLE 50&#39;</span>493        <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>494    <span class="k">def</span><span class="w"> </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>495        <span class="s1">&#39;Print the current turtle position:  POSITION&#39;</span>496        <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>497    <span class="k">def</span><span class="w"> </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>498        <span class="s1">&#39;Print the current turtle heading in degrees:  HEADING&#39;</span>499        <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>500    <span class="k">def</span><span class="w"> </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>501        <span class="s1">&#39;Set the color:  COLOR BLUE&#39;</span>502        <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>503    <span class="k">def</span><span class="w"> </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>504        <span class="s1">&#39;Undo (repeatedly) the last turtle action(s):  UNDO&#39;</span>505    <span class="k">def</span><span class="w"> </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>506        <span class="s1">&#39;Clear the screen and return turtle to center:  RESET&#39;</span>507        <span class="n">reset</span><span class="p">()</span>508    <span class="k">def</span><span class="w"> </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>509        <span class="s1">&#39;Stop recording, close the turtle window, and exit:  BYE&#39;</span>510        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Thank you for using Turtle&#39;</span><span class="p">)</span>511        <span class="bp">self</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>512        <span class="n">bye</span><span class="p">()</span>513        <span class="k">return</span> <span class="kc">True</span>514 515    <span class="c1"># ----- record and playback -----</span>516    <span class="k">def</span><span class="w"> </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>517        <span class="s1">&#39;Save future commands to filename:  RECORD rose.cmd&#39;</span>518        <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>519    <span class="k">def</span><span class="w"> </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>520        <span class="s1">&#39;Playback commands from a file:  PLAYBACK rose.cmd&#39;</span>521        <span class="bp">self</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>522        <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>523            <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>524    <span class="k">def</span><span class="w"> </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>525        <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>526        <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>527            <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>528        <span class="k">return</span> <span class="n">line</span>529    <span class="k">def</span><span class="w"> </span><span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>530        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="p">:</span>531            <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>532            <span class="bp">self</span><span class="o">.</span><span class="n">file</span> <span class="o">=</span> <span class="kc">None</span>533 534<span class="k">def</span><span class="w"> </span><span class="nf">parse</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span>535    <span class="s1">&#39;Convert a series of zero or more numbers to an argument tuple&#39;</span>536    <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>537 538<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>539    <span class="n">TurtleShell</span><span class="p">()</span><span class="o">.</span><span class="n">cmdloop</span><span class="p">()</span>540</pre></div>541</div>542<p>Here is a sample session with the turtle shell showing the help functions, using543blank lines to repeat commands, and the simple record and playback facility:</p>544<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Welcome to the turtle shell.   Type help or ? to list commands.545 546(turtle) ?547 548Documented commands (type help &lt;topic&gt;):549========================================550bye     color    goto     home  playback  record  right551circle  forward  heading  left  position  reset   undo552 553(turtle) help forward554Move the turtle forward by the specified distance:  FORWARD 10555(turtle) record spiral.cmd556(turtle) position557Current position is 0 0558 559(turtle) heading560Current heading is 0561 562(turtle) reset563(turtle) circle 20564(turtle) right 30565(turtle) circle 40566(turtle) right 30567(turtle) circle 60568(turtle) right 30569(turtle) circle 80570(turtle) right 30571(turtle) circle 100572(turtle) right 30573(turtle) circle 120574(turtle) right 30575(turtle) circle 120576(turtle) heading577Current heading is 180578 579(turtle) forward 100580(turtle)581(turtle) right 90582(turtle) forward 100583(turtle)584(turtle) right 90585(turtle) forward 400586(turtle) right 90587(turtle) forward 500588(turtle) right 90589(turtle) forward 400590(turtle) right 90591(turtle) forward 300592(turtle) playback spiral.cmd593Current position is 0 0594 595Current heading is 0596 597Current heading is 180598 599(turtle) bye600Thank you for using Turtle601</pre></div>602</div>603</section>604</section>605 606 607            <div class="clearer"></div>608          </div>609        </div>610      </div>611      <div class="sphinxsidebar" role="navigation" aria-label="Main">612        <div class="sphinxsidebarwrapper">613  <div>614    <h3><a href="../contents.html">Table of Contents</a></h3>615    <ul>616<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>617<li><a class="reference internal" href="#cmd-objects">Cmd Objects</a></li>618<li><a class="reference internal" href="#cmd-example">Cmd Example</a></li>619</ul>620</li>621</ul>622 623  </div>624  <div>625    <h4>Previous topic</h4>626    <p class="topless"><a href="curses.panel.html"627                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses.panel</span></code> — A panel stack extension for curses</a></p>628  </div>629  <div>630    <h4>Next topic</h4>631    <p class="topless"><a href="concurrency.html"632                          title="next chapter">Concurrent Execution</a></p>633  </div>634  <script>635    document.addEventListener('DOMContentLoaded', () => {636        const title = document.querySelector('meta[property="og:title"]').content;637        const elements = document.querySelectorAll('.improvepage');638        const pageurl = window.location.href.split('?')[0];639        elements.forEach(element => {640            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));641            url.searchParams.set('pagetitle', title);642            url.searchParams.set('pageurl', pageurl);643            url.searchParams.set('pagesource', "library/cmd.rst");644            element.href = url.toString();645        });646    });647  </script>648  <div role="note" aria-label="source link">649    <h3>This page</h3>650    <ul class="this-page-menu">651      <li><a href="../bugs.html">Report a bug</a></li>652      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>653      <li>654        <a href="https://github.com/python/cpython/blob/main/Doc/library/cmd.rst?plain=1"655            rel="nofollow">Show source656        </a>657      </li>658      659    </ul>660  </div>661        </div>662<div id="sidebarbutton" title="Collapse sidebar">663<span>«</span>664</div>665 666      </div>667      <div class="clearer"></div>668    </div>  669    <div class="related" role="navigation" aria-label="Related">670      <h3>Navigation</h3>671      <ul>672        <li class="right" style="margin-right: 10px">673          <a href="../genindex.html" title="General Index"674             >index</a></li>675        <li class="right" >676          <a href="../py-modindex.html" title="Python Module Index"677             >modules</a> |</li>678        <li class="right" >679          <a href="concurrency.html" title="Concurrent Execution"680             >next</a> |</li>681        <li class="right" >682          <a href="curses.panel.html" title="curses.panel — A panel stack extension for curses"683             >previous</a> |</li>684 685          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>686          <li><a href="https://www.python.org/">Python</a> &#187;</li>687          <li class="switchers">688            <div class="language_switcher_placeholder"></div>689            <div class="version_switcher_placeholder"></div>690          </li>691          <li>692              693          </li>694    <li id="cpython-language-and-version">695      <a href="../index.html">3.15.0a6 Documentation</a> &#187;696    </li>697 698          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>699          <li class="nav-item nav-item-2"><a href="cmdlinelibs.html" >Command-line interface libraries</a> &#187;</li>700        <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>701                <li class="right">702                    703 704    <div class="inline-search" role="search">705        <form class="inline-search" action="../search.html" method="get">706          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">707          <input type="submit" value="Go">708        </form>709    </div>710                     |711                </li>712            <li class="right">713<label class="theme-selector-label">714    Theme715    <select class="theme-selector" oninput="activateTheme(this.value)">716        <option value="auto" selected>Auto</option>717        <option value="light">Light</option>718        <option value="dark">Dark</option>719    </select>720</label> |</li>721            722      </ul>723    </div>  724    <div class="footer">725    &copy; <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.726    <br>727    This page is licensed under the Python Software Foundation License Version 2.728    <br>729    Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.730    <br>731    732      See <a href="/license.html">History and License</a> for more information.<br>733    734    735    <br>736 737    The Python Software Foundation is a non-profit corporation.738<a href="https://www.python.org/psf/donations/">Please donate.</a>739<br>740    <br>741      Last updated on Mar 10, 2026 (08:58 UTC).742    743      <a href="/bugs.html">Found a bug</a>?744    745    <br>746 747    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.748    </div>749 750  </body>751</html>