gooderp18绿色标准版
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

776 lines
63KB

  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="shlex — Simple lexical analysis" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/shlex.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/shlex.py The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell. This will often be useful for writing minilanguages, (for e..." />
  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/shlex.py The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell. This will often be useful for writing minilanguages, (for e..." />
  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>shlex — Simple lexical analysis &#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="Graphical User Interfaces with Tk" href="tk.html" />
  33. <link rel="prev" title="cmd — Support for line-oriented command interpreters" href="cmd.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/shlex.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">shlex</span></code> — Simple lexical analysis</a><ul>
  86. <li><a class="reference internal" href="#shlex-objects">shlex Objects</a></li>
  87. <li><a class="reference internal" href="#parsing-rules">Parsing Rules</a></li>
  88. <li><a class="reference internal" href="#improved-compatibility-with-shells">Improved Compatibility with Shells</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </div>
  93. <div>
  94. <h4>Previous topic</h4>
  95. <p class="topless"><a href="cmd.html"
  96. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> — Support for line-oriented command interpreters</a></p>
  97. </div>
  98. <div>
  99. <h4>Next topic</h4>
  100. <p class="topless"><a href="tk.html"
  101. title="next chapter">Graphical User Interfaces with Tk</a></p>
  102. </div>
  103. <div role="note" aria-label="source link">
  104. <h3>This Page</h3>
  105. <ul class="this-page-menu">
  106. <li><a href="../bugs.html">Report a Bug</a></li>
  107. <li>
  108. <a href="https://github.com/python/cpython/blob/main/Doc/library/shlex.rst"
  109. rel="nofollow">Show Source
  110. </a>
  111. </li>
  112. </ul>
  113. </div>
  114. </nav>
  115. </div>
  116. </div>
  117. <div class="related" role="navigation" aria-label="related navigation">
  118. <h3>Navigation</h3>
  119. <ul>
  120. <li class="right" style="margin-right: 10px">
  121. <a href="../genindex.html" title="General Index"
  122. accesskey="I">index</a></li>
  123. <li class="right" >
  124. <a href="../py-modindex.html" title="Python Module Index"
  125. >modules</a> |</li>
  126. <li class="right" >
  127. <a href="tk.html" title="Graphical User Interfaces with Tk"
  128. accesskey="N">next</a> |</li>
  129. <li class="right" >
  130. <a href="cmd.html" title="cmd — Support for line-oriented command interpreters"
  131. accesskey="P">previous</a> |</li>
  132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  134. <li class="switchers">
  135. <div class="language_switcher_placeholder"></div>
  136. <div class="version_switcher_placeholder"></div>
  137. </li>
  138. <li>
  139. </li>
  140. <li id="cpython-language-and-version">
  141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  142. </li>
  143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  144. <li class="nav-item nav-item-2"><a href="frameworks.html" accesskey="U">Program Frameworks</a> &#187;</li>
  145. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code> — Simple lexical analysis</a></li>
  146. <li class="right">
  147. <div class="inline-search" role="search">
  148. <form class="inline-search" action="../search.html" method="get">
  149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  150. <input type="submit" value="Go" />
  151. </form>
  152. </div>
  153. |
  154. </li>
  155. <li class="right">
  156. <label class="theme-selector-label">
  157. Theme
  158. <select class="theme-selector" oninput="activateTheme(this.value)">
  159. <option value="auto" selected>Auto</option>
  160. <option value="light">Light</option>
  161. <option value="dark">Dark</option>
  162. </select>
  163. </label> |</li>
  164. </ul>
  165. </div>
  166. <div class="document">
  167. <div class="documentwrapper">
  168. <div class="bodywrapper">
  169. <div class="body" role="main">
  170. <section id="module-shlex">
  171. <span id="shlex-simple-lexical-analysis"></span><h1><a class="reference internal" href="#module-shlex" title="shlex: Simple lexical analysis for Unix shell-like languages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code></a> — Simple lexical analysis<a class="headerlink" href="#module-shlex" title="Link to this heading">¶</a></h1>
  172. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/shlex.py">Lib/shlex.py</a></p>
  173. <hr class="docutils" />
  174. <p>The <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> class makes it easy to write lexical analyzers for
  175. simple syntaxes resembling that of the Unix shell. This will often be useful
  176. for writing minilanguages, (for example, in run control files for Python
  177. applications) or for parsing quoted strings.</p>
  178. <p>The <a class="reference internal" href="#module-shlex" title="shlex: Simple lexical analysis for Unix shell-like languages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code></a> module defines the following functions:</p>
  179. <dl class="py function">
  180. <dt class="sig sig-object py" id="shlex.split">
  181. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">split</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">comments</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">posix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.split" title="Link to this definition">¶</a></dt>
  182. <dd><p>Split the string <em>s</em> using shell-like syntax. If <em>comments</em> is <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>
  183. (the default), the parsing of comments in the given string will be disabled
  184. (setting the <a class="reference internal" href="#shlex.shlex.commenters" title="shlex.shlex.commenters"><code class="xref py py-attr docutils literal notranslate"><span class="pre">commenters</span></code></a> attribute of the
  185. <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> instance to the empty string). This function operates
  186. in POSIX mode by default, but uses non-POSIX mode if the <em>posix</em> argument is
  187. false.</p>
  188. <div class="versionchanged">
  189. <p><span class="versionmodified changed">Changed in version 3.12: </span>Passing <code class="docutils literal notranslate"><span class="pre">None</span></code> for <em>s</em> argument now raises an exception, rather than
  190. reading <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>.</p>
  191. </div>
  192. </dd></dl>
  193. <dl class="py function">
  194. <dt class="sig sig-object py" id="shlex.join">
  195. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">join</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">split_command</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.join" title="Link to this definition">¶</a></dt>
  196. <dd><p>Concatenate the tokens of the list <em>split_command</em> and return a string.
  197. This function is the inverse of <a class="reference internal" href="#shlex.split" title="shlex.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">split()</span></code></a>.</p>
  198. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">shlex</span> <span class="kn">import</span> <span class="n">join</span>
  199. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">join</span><span class="p">([</span><span class="s1">&#39;echo&#39;</span><span class="p">,</span> <span class="s1">&#39;-n&#39;</span><span class="p">,</span> <span class="s1">&#39;Multiple words&#39;</span><span class="p">]))</span>
  200. <span class="go">echo -n &#39;Multiple words&#39;</span>
  201. </pre></div>
  202. </div>
  203. <p>The returned value is shell-escaped to protect against injection
  204. vulnerabilities (see <a class="reference internal" href="#shlex.quote" title="shlex.quote"><code class="xref py py-func docutils literal notranslate"><span class="pre">quote()</span></code></a>).</p>
  205. <div class="versionadded">
  206. <p><span class="versionmodified added">New in version 3.8.</span></p>
  207. </div>
  208. </dd></dl>
  209. <dl class="py function">
  210. <dt class="sig sig-object py" id="shlex.quote">
  211. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">quote</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.quote" title="Link to this definition">¶</a></dt>
  212. <dd><p>Return a shell-escaped version of the string <em>s</em>. The returned value is a
  213. string that can safely be used as one token in a shell command line, for
  214. cases where you cannot use a list.</p>
  215. <div class="admonition warning" id="shlex-quote-warning">
  216. <p class="admonition-title">Warning</p>
  217. <p>The <code class="docutils literal notranslate"><span class="pre">shlex</span></code> module is <strong>only designed for Unix shells</strong>.</p>
  218. <p>The <a class="reference internal" href="#shlex.quote" title="shlex.quote"><code class="xref py py-func docutils literal notranslate"><span class="pre">quote()</span></code></a> function is not guaranteed to be correct on non-POSIX
  219. compliant shells or shells from other operating systems such as Windows.
  220. Executing commands quoted by this module on such shells can open up the
  221. possibility of a command injection vulnerability.</p>
  222. <p>Consider using functions that pass command arguments with lists such as
  223. <a class="reference internal" href="subprocess.html#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">subprocess.run()</span></code></a> with <code class="docutils literal notranslate"><span class="pre">shell=False</span></code>.</p>
  224. </div>
  225. <p>This idiom would be unsafe:</p>
  226. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;somefile; rm -rf ~&#39;</span>
  227. <span class="gp">&gt;&gt;&gt; </span><span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;ls -l </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
  228. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">command</span><span class="p">)</span> <span class="c1"># executed by a shell: boom!</span>
  229. <span class="go">ls -l somefile; rm -rf ~</span>
  230. </pre></div>
  231. </div>
  232. <p><a class="reference internal" href="#shlex.quote" title="shlex.quote"><code class="xref py py-func docutils literal notranslate"><span class="pre">quote()</span></code></a> lets you plug the security hole:</p>
  233. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">shlex</span> <span class="kn">import</span> <span class="n">quote</span>
  234. <span class="gp">&gt;&gt;&gt; </span><span class="n">command</span> <span class="o">=</span> <span class="s1">&#39;ls -l </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">quote</span><span class="p">(</span><span class="n">filename</span><span class="p">))</span>
  235. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">command</span><span class="p">)</span>
  236. <span class="go">ls -l &#39;somefile; rm -rf ~&#39;</span>
  237. <span class="gp">&gt;&gt;&gt; </span><span class="n">remote_command</span> <span class="o">=</span> <span class="s1">&#39;ssh home </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">quote</span><span class="p">(</span><span class="n">command</span><span class="p">))</span>
  238. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">remote_command</span><span class="p">)</span>
  239. <span class="go">ssh home &#39;ls -l &#39;&quot;&#39;&quot;&#39;somefile; rm -rf ~&#39;&quot;&#39;&quot;&#39;&#39;</span>
  240. </pre></div>
  241. </div>
  242. <p>The quoting is compatible with UNIX shells and with <a class="reference internal" href="#shlex.split" title="shlex.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">split()</span></code></a>:</p>
  243. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">shlex</span> <span class="kn">import</span> <span class="n">split</span>
  244. <span class="gp">&gt;&gt;&gt; </span><span class="n">remote_command</span> <span class="o">=</span> <span class="n">split</span><span class="p">(</span><span class="n">remote_command</span><span class="p">)</span>
  245. <span class="gp">&gt;&gt;&gt; </span><span class="n">remote_command</span>
  246. <span class="go">[&#39;ssh&#39;, &#39;home&#39;, &quot;ls -l &#39;somefile; rm -rf ~&#39;&quot;]</span>
  247. <span class="gp">&gt;&gt;&gt; </span><span class="n">command</span> <span class="o">=</span> <span class="n">split</span><span class="p">(</span><span class="n">remote_command</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
  248. <span class="gp">&gt;&gt;&gt; </span><span class="n">command</span>
  249. <span class="go">[&#39;ls&#39;, &#39;-l&#39;, &#39;somefile; rm -rf ~&#39;]</span>
  250. </pre></div>
  251. </div>
  252. <div class="versionadded">
  253. <p><span class="versionmodified added">New in version 3.3.</span></p>
  254. </div>
  255. </dd></dl>
  256. <p>The <a class="reference internal" href="#module-shlex" title="shlex: Simple lexical analysis for Unix shell-like languages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code></a> module defines the following class:</p>
  257. <dl class="py class">
  258. <dt class="sig sig-object py" id="shlex.shlex">
  259. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">shlex</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">instream</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">infile</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">posix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">punctuation_chars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex" title="Link to this definition">¶</a></dt>
  260. <dd><p>A <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> instance or subclass instance is a lexical analyzer
  261. object. The initialization argument, if present, specifies where to read
  262. characters from. It must be a file-/stream-like object with
  263. <a class="reference internal" href="io.html#io.TextIOBase.read" title="io.TextIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> and <a class="reference internal" href="io.html#io.TextIOBase.readline" title="io.TextIOBase.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a> methods, or
  264. a string. If no argument is given, input will be taken from <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>.
  265. The second optional argument is a filename string, which sets the initial
  266. value of the <a class="reference internal" href="#shlex.shlex.infile" title="shlex.shlex.infile"><code class="xref py py-attr docutils literal notranslate"><span class="pre">infile</span></code></a> attribute. If the <em>instream</em>
  267. argument is omitted or equal to <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>, this second argument
  268. defaults to “stdin”. The <em>posix</em> argument defines the operational mode:
  269. when <em>posix</em> is not true (default), the <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> instance will
  270. operate in compatibility mode. When operating in POSIX mode,
  271. <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> will try to be as close as possible to the POSIX shell
  272. parsing rules. The <em>punctuation_chars</em> argument provides a way to make the
  273. behaviour even closer to how real shells parse. This can take a number of
  274. values: the default value, <code class="docutils literal notranslate"><span class="pre">False</span></code>, preserves the behaviour seen under
  275. Python 3.5 and earlier. If set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, then parsing of the characters
  276. <code class="docutils literal notranslate"><span class="pre">();&lt;&gt;|&amp;</span></code> is changed: any run of these characters (considered punctuation
  277. characters) is returned as a single token. If set to a non-empty string of
  278. characters, those characters will be used as the punctuation characters. Any
  279. characters in the <a class="reference internal" href="#shlex.shlex.wordchars" title="shlex.shlex.wordchars"><code class="xref py py-attr docutils literal notranslate"><span class="pre">wordchars</span></code></a> attribute that appear in
  280. <em>punctuation_chars</em> will be removed from <a class="reference internal" href="#shlex.shlex.wordchars" title="shlex.shlex.wordchars"><code class="xref py py-attr docutils literal notranslate"><span class="pre">wordchars</span></code></a>. See
  281. <a class="reference internal" href="#improved-shell-compatibility"><span class="std std-ref">Improved Compatibility with Shells</span></a> for more information. <em>punctuation_chars</em>
  282. can be set only upon <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> instance creation and can’t be
  283. modified later.</p>
  284. <div class="versionchanged">
  285. <p><span class="versionmodified changed">Changed in version 3.6: </span>The <em>punctuation_chars</em> parameter was added.</p>
  286. </div>
  287. </dd></dl>
  288. <div class="admonition seealso">
  289. <p class="admonition-title">See also</p>
  290. <dl class="simple">
  291. <dt>Module <a class="reference internal" href="configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">configparser</span></code></a></dt><dd><p>Parser for configuration files similar to the Windows <code class="file docutils literal notranslate"><span class="pre">.ini</span></code> files.</p>
  292. </dd>
  293. </dl>
  294. </div>
  295. <section id="shlex-objects">
  296. <span id="id1"></span><h2>shlex Objects<a class="headerlink" href="#shlex-objects" title="Link to this heading">¶</a></h2>
  297. <p>A <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> instance has the following methods:</p>
  298. <dl class="py method">
  299. <dt class="sig sig-object py" id="shlex.shlex.get_token">
  300. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">get_token</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.get_token" title="Link to this definition">¶</a></dt>
  301. <dd><p>Return a token. If tokens have been stacked using <a class="reference internal" href="#shlex.shlex.push_token" title="shlex.shlex.push_token"><code class="xref py py-meth docutils literal notranslate"><span class="pre">push_token()</span></code></a>, pop a
  302. token off the stack. Otherwise, read one from the input stream. If reading
  303. encounters an immediate end-of-file, <a class="reference internal" href="#shlex.shlex.eof" title="shlex.shlex.eof"><code class="xref py py-attr docutils literal notranslate"><span class="pre">eof</span></code></a> is returned (the empty
  304. string (<code class="docutils literal notranslate"><span class="pre">''</span></code>) in non-POSIX mode, and <code class="docutils literal notranslate"><span class="pre">None</span></code> in POSIX mode).</p>
  305. </dd></dl>
  306. <dl class="py method">
  307. <dt class="sig sig-object py" id="shlex.shlex.push_token">
  308. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">push_token</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="#shlex.shlex.push_token" title="Link to this definition">¶</a></dt>
  309. <dd><p>Push the argument onto the token stack.</p>
  310. </dd></dl>
  311. <dl class="py method">
  312. <dt class="sig sig-object py" id="shlex.shlex.read_token">
  313. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">read_token</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.read_token" title="Link to this definition">¶</a></dt>
  314. <dd><p>Read a raw token. Ignore the pushback stack, and do not interpret source
  315. requests. (This is not ordinarily a useful entry point, and is documented here
  316. only for the sake of completeness.)</p>
  317. </dd></dl>
  318. <dl class="py method">
  319. <dt class="sig sig-object py" id="shlex.shlex.sourcehook">
  320. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">sourcehook</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.sourcehook" title="Link to this definition">¶</a></dt>
  321. <dd><p>When <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> detects a source request (see <a class="reference internal" href="#shlex.shlex.source" title="shlex.shlex.source"><code class="xref py py-attr docutils literal notranslate"><span class="pre">source</span></code></a>
  322. below) this method is given the following token as argument, and expected
  323. to return a tuple consisting of a filename and an open file-like object.</p>
  324. <p>Normally, this method first strips any quotes off the argument. If the result
  325. is an absolute pathname, or there was no previous source request in effect, or
  326. the previous source was a stream (such as <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>), the result is left
  327. alone. Otherwise, if the result is a relative pathname, the directory part of
  328. the name of the file immediately before it on the source inclusion stack is
  329. prepended (this behavior is like the way the C preprocessor handles <code class="docutils literal notranslate"><span class="pre">#include</span>
  330. <span class="pre">&quot;file.h&quot;</span></code>).</p>
  331. <p>The result of the manipulations is treated as a filename, and returned as the
  332. first component of the tuple, with <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> called on it to yield the second
  333. component. (Note: this is the reverse of the order of arguments in instance
  334. initialization!)</p>
  335. <p>This hook is exposed so that you can use it to implement directory search paths,
  336. addition of file extensions, and other namespace hacks. There is no
  337. corresponding ‘close’ hook, but a shlex instance will call the
  338. <a class="reference internal" href="io.html#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method of the sourced input stream when it returns
  339. EOF.</p>
  340. <p>For more explicit control of source stacking, use the <a class="reference internal" href="#shlex.shlex.push_source" title="shlex.shlex.push_source"><code class="xref py py-meth docutils literal notranslate"><span class="pre">push_source()</span></code></a> and
  341. <a class="reference internal" href="#shlex.shlex.pop_source" title="shlex.shlex.pop_source"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pop_source()</span></code></a> methods.</p>
  342. </dd></dl>
  343. <dl class="py method">
  344. <dt class="sig sig-object py" id="shlex.shlex.push_source">
  345. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">push_source</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">newstream</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newfile</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="#shlex.shlex.push_source" title="Link to this definition">¶</a></dt>
  346. <dd><p>Push an input source stream onto the input stack. If the filename argument is
  347. specified it will later be available for use in error messages. This is the
  348. same method used internally by the <a class="reference internal" href="#shlex.shlex.sourcehook" title="shlex.shlex.sourcehook"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sourcehook()</span></code></a> method.</p>
  349. </dd></dl>
  350. <dl class="py method">
  351. <dt class="sig sig-object py" id="shlex.shlex.pop_source">
  352. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">pop_source</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.pop_source" title="Link to this definition">¶</a></dt>
  353. <dd><p>Pop the last-pushed input source from the input stack. This is the same method
  354. used internally when the lexer reaches EOF on a stacked input stream.</p>
  355. </dd></dl>
  356. <dl class="py method">
  357. <dt class="sig sig-object py" id="shlex.shlex.error_leader">
  358. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">error_leader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">infile</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">lineno</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="#shlex.shlex.error_leader" title="Link to this definition">¶</a></dt>
  359. <dd><p>This method generates an error message leader in the format of a Unix C compiler
  360. error label; the format is <code class="docutils literal notranslate"><span class="pre">'&quot;%s&quot;,</span> <span class="pre">line</span> <span class="pre">%d:</span> <span class="pre">'</span></code>, where the <code class="docutils literal notranslate"><span class="pre">%s</span></code> is replaced
  361. with the name of the current source file and the <code class="docutils literal notranslate"><span class="pre">%d</span></code> with the current input
  362. line number (the optional arguments can be used to override these).</p>
  363. <p>This convenience is provided to encourage <a class="reference internal" href="#module-shlex" title="shlex: Simple lexical analysis for Unix shell-like languages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code></a> users to generate error
  364. messages in the standard, parseable format understood by Emacs and other Unix
  365. tools.</p>
  366. </dd></dl>
  367. <p>Instances of <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> subclasses have some public instance
  368. variables which either control lexical analysis or can be used for debugging:</p>
  369. <dl class="py attribute">
  370. <dt class="sig sig-object py" id="shlex.shlex.commenters">
  371. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">commenters</span></span><a class="headerlink" href="#shlex.shlex.commenters" title="Link to this definition">¶</a></dt>
  372. <dd><p>The string of characters that are recognized as comment beginners. All
  373. characters from the comment beginner to end of line are ignored. Includes just
  374. <code class="docutils literal notranslate"><span class="pre">'#'</span></code> by default.</p>
  375. </dd></dl>
  376. <dl class="py attribute">
  377. <dt class="sig sig-object py" id="shlex.shlex.wordchars">
  378. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">wordchars</span></span><a class="headerlink" href="#shlex.shlex.wordchars" title="Link to this definition">¶</a></dt>
  379. <dd><p>The string of characters that will accumulate into multi-character tokens. By
  380. default, includes all ASCII alphanumerics and underscore. In POSIX mode, the
  381. accented characters in the Latin-1 set are also included. If
  382. <a class="reference internal" href="#shlex.shlex.punctuation_chars" title="shlex.shlex.punctuation_chars"><code class="xref py py-attr docutils literal notranslate"><span class="pre">punctuation_chars</span></code></a> is not empty, the characters <code class="docutils literal notranslate"><span class="pre">~-./*?=</span></code>, which can
  383. appear in filename specifications and command line parameters, will also be
  384. included in this attribute, and any characters which appear in
  385. <code class="docutils literal notranslate"><span class="pre">punctuation_chars</span></code> will be removed from <code class="docutils literal notranslate"><span class="pre">wordchars</span></code> if they are present
  386. there. If <a class="reference internal" href="#shlex.shlex.whitespace_split" title="shlex.shlex.whitespace_split"><code class="xref py py-attr docutils literal notranslate"><span class="pre">whitespace_split</span></code></a> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, this will have no
  387. effect.</p>
  388. </dd></dl>
  389. <dl class="py attribute">
  390. <dt class="sig sig-object py" id="shlex.shlex.whitespace">
  391. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">whitespace</span></span><a class="headerlink" href="#shlex.shlex.whitespace" title="Link to this definition">¶</a></dt>
  392. <dd><p>Characters that will be considered whitespace and skipped. Whitespace bounds
  393. tokens. By default, includes space, tab, linefeed and carriage-return.</p>
  394. </dd></dl>
  395. <dl class="py attribute">
  396. <dt class="sig sig-object py" id="shlex.shlex.escape">
  397. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">escape</span></span><a class="headerlink" href="#shlex.shlex.escape" title="Link to this definition">¶</a></dt>
  398. <dd><p>Characters that will be considered as escape. This will be only used in POSIX
  399. mode, and includes just <code class="docutils literal notranslate"><span class="pre">'\'</span></code> by default.</p>
  400. </dd></dl>
  401. <dl class="py attribute">
  402. <dt class="sig sig-object py" id="shlex.shlex.quotes">
  403. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">quotes</span></span><a class="headerlink" href="#shlex.shlex.quotes" title="Link to this definition">¶</a></dt>
  404. <dd><p>Characters that will be considered string quotes. The token accumulates until
  405. the same quote is encountered again (thus, different quote types protect each
  406. other as in the shell.) By default, includes ASCII single and double quotes.</p>
  407. </dd></dl>
  408. <dl class="py attribute">
  409. <dt class="sig sig-object py" id="shlex.shlex.escapedquotes">
  410. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">escapedquotes</span></span><a class="headerlink" href="#shlex.shlex.escapedquotes" title="Link to this definition">¶</a></dt>
  411. <dd><p>Characters in <a class="reference internal" href="#shlex.shlex.quotes" title="shlex.shlex.quotes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">quotes</span></code></a> that will interpret escape characters defined in
  412. <a class="reference internal" href="#shlex.shlex.escape" title="shlex.shlex.escape"><code class="xref py py-attr docutils literal notranslate"><span class="pre">escape</span></code></a>. This is only used in POSIX mode, and includes just <code class="docutils literal notranslate"><span class="pre">'&quot;'</span></code> by
  413. default.</p>
  414. </dd></dl>
  415. <dl class="py attribute">
  416. <dt class="sig sig-object py" id="shlex.shlex.whitespace_split">
  417. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">whitespace_split</span></span><a class="headerlink" href="#shlex.shlex.whitespace_split" title="Link to this definition">¶</a></dt>
  418. <dd><p>If <code class="docutils literal notranslate"><span class="pre">True</span></code>, tokens will only be split in whitespaces. This is useful, for
  419. example, for parsing command lines with <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a>, getting
  420. tokens in a similar way to shell arguments. When used in combination with
  421. <a class="reference internal" href="#shlex.shlex.punctuation_chars" title="shlex.shlex.punctuation_chars"><code class="xref py py-attr docutils literal notranslate"><span class="pre">punctuation_chars</span></code></a>, tokens will be split on whitespace in addition to
  422. those characters.</p>
  423. <div class="versionchanged">
  424. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <a class="reference internal" href="#shlex.shlex.punctuation_chars" title="shlex.shlex.punctuation_chars"><code class="xref py py-attr docutils literal notranslate"><span class="pre">punctuation_chars</span></code></a> attribute was made compatible with the
  425. <a class="reference internal" href="#shlex.shlex.whitespace_split" title="shlex.shlex.whitespace_split"><code class="xref py py-attr docutils literal notranslate"><span class="pre">whitespace_split</span></code></a> attribute.</p>
  426. </div>
  427. </dd></dl>
  428. <dl class="py attribute">
  429. <dt class="sig sig-object py" id="shlex.shlex.infile">
  430. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">infile</span></span><a class="headerlink" href="#shlex.shlex.infile" title="Link to this definition">¶</a></dt>
  431. <dd><p>The name of the current input file, as initially set at class instantiation time
  432. or stacked by later source requests. It may be useful to examine this when
  433. constructing error messages.</p>
  434. </dd></dl>
  435. <dl class="py attribute">
  436. <dt class="sig sig-object py" id="shlex.shlex.instream">
  437. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">instream</span></span><a class="headerlink" href="#shlex.shlex.instream" title="Link to this definition">¶</a></dt>
  438. <dd><p>The input stream from which this <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> instance is reading
  439. characters.</p>
  440. </dd></dl>
  441. <dl class="py attribute">
  442. <dt class="sig sig-object py" id="shlex.shlex.source">
  443. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">source</span></span><a class="headerlink" href="#shlex.shlex.source" title="Link to this definition">¶</a></dt>
  444. <dd><p>This attribute is <code class="docutils literal notranslate"><span class="pre">None</span></code> by default. If you assign a string to it, that
  445. string will be recognized as a lexical-level inclusion request similar to the
  446. <code class="docutils literal notranslate"><span class="pre">source</span></code> keyword in various shells. That is, the immediately following token
  447. will be opened as a filename and input will be taken from that stream until
  448. EOF, at which point the <a class="reference internal" href="io.html#io.IOBase.close" title="io.IOBase.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method of that stream will be
  449. called and the input source will again become the original input stream. Source
  450. requests may be stacked any number of levels deep.</p>
  451. </dd></dl>
  452. <dl class="py attribute">
  453. <dt class="sig sig-object py" id="shlex.shlex.debug">
  454. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">debug</span></span><a class="headerlink" href="#shlex.shlex.debug" title="Link to this definition">¶</a></dt>
  455. <dd><p>If this attribute is numeric and <code class="docutils literal notranslate"><span class="pre">1</span></code> or more, a <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a>
  456. instance will print verbose progress output on its behavior. If you need
  457. to use this, you can read the module source code to learn the details.</p>
  458. </dd></dl>
  459. <dl class="py attribute">
  460. <dt class="sig sig-object py" id="shlex.shlex.lineno">
  461. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#shlex.shlex.lineno" title="Link to this definition">¶</a></dt>
  462. <dd><p>Source line number (count of newlines seen so far plus one).</p>
  463. </dd></dl>
  464. <dl class="py attribute">
  465. <dt class="sig sig-object py" id="shlex.shlex.token">
  466. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">token</span></span><a class="headerlink" href="#shlex.shlex.token" title="Link to this definition">¶</a></dt>
  467. <dd><p>The token buffer. It may be useful to examine this when catching exceptions.</p>
  468. </dd></dl>
  469. <dl class="py attribute">
  470. <dt class="sig sig-object py" id="shlex.shlex.eof">
  471. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">eof</span></span><a class="headerlink" href="#shlex.shlex.eof" title="Link to this definition">¶</a></dt>
  472. <dd><p>Token used to determine end of file. This will be set to the empty string
  473. (<code class="docutils literal notranslate"><span class="pre">''</span></code>), in non-POSIX mode, and to <code class="docutils literal notranslate"><span class="pre">None</span></code> in POSIX mode.</p>
  474. </dd></dl>
  475. <dl class="py attribute">
  476. <dt class="sig sig-object py" id="shlex.shlex.punctuation_chars">
  477. <span class="sig-prename descclassname"><span class="pre">shlex.</span></span><span class="sig-name descname"><span class="pre">punctuation_chars</span></span><a class="headerlink" href="#shlex.shlex.punctuation_chars" title="Link to this definition">¶</a></dt>
  478. <dd><p>A read-only property. Characters that will be considered punctuation. Runs of
  479. punctuation characters will be returned as a single token. However, note that no
  480. semantic validity checking will be performed: for example, ‘&gt;&gt;&gt;’ could be
  481. returned as a token, even though it may not be recognised as such by shells.</p>
  482. <div class="versionadded">
  483. <p><span class="versionmodified added">New in version 3.6.</span></p>
  484. </div>
  485. </dd></dl>
  486. </section>
  487. <section id="parsing-rules">
  488. <span id="shlex-parsing-rules"></span><h2>Parsing Rules<a class="headerlink" href="#parsing-rules" title="Link to this heading">¶</a></h2>
  489. <p>When operating in non-POSIX mode, <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> will try to obey to the
  490. following rules.</p>
  491. <ul class="simple">
  492. <li><p>Quote characters are not recognized within words (<code class="docutils literal notranslate"><span class="pre">Do&quot;Not&quot;Separate</span></code> is
  493. parsed as the single word <code class="docutils literal notranslate"><span class="pre">Do&quot;Not&quot;Separate</span></code>);</p></li>
  494. <li><p>Escape characters are not recognized;</p></li>
  495. <li><p>Enclosing characters in quotes preserve the literal value of all characters
  496. within the quotes;</p></li>
  497. <li><p>Closing quotes separate words (<code class="docutils literal notranslate"><span class="pre">&quot;Do&quot;Separate</span></code> is parsed as <code class="docutils literal notranslate"><span class="pre">&quot;Do&quot;</span></code> and
  498. <code class="docutils literal notranslate"><span class="pre">Separate</span></code>);</p></li>
  499. <li><p>If <a class="reference internal" href="#shlex.shlex.whitespace_split" title="shlex.shlex.whitespace_split"><code class="xref py py-attr docutils literal notranslate"><span class="pre">whitespace_split</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, any character not
  500. declared to be a word character, whitespace, or a quote will be returned as
  501. a single-character token. If it is <code class="docutils literal notranslate"><span class="pre">True</span></code>, <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> will only
  502. split words in whitespaces;</p></li>
  503. <li><p>EOF is signaled with an empty string (<code class="docutils literal notranslate"><span class="pre">''</span></code>);</p></li>
  504. <li><p>It’s not possible to parse empty strings, even if quoted.</p></li>
  505. </ul>
  506. <p>When operating in POSIX mode, <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> will try to obey to the
  507. following parsing rules.</p>
  508. <ul class="simple">
  509. <li><p>Quotes are stripped out, and do not separate words (<code class="docutils literal notranslate"><span class="pre">&quot;Do&quot;Not&quot;Separate&quot;</span></code> is
  510. parsed as the single word <code class="docutils literal notranslate"><span class="pre">DoNotSeparate</span></code>);</p></li>
  511. <li><p>Non-quoted escape characters (e.g. <code class="docutils literal notranslate"><span class="pre">'\'</span></code>) preserve the literal value of the
  512. next character that follows;</p></li>
  513. <li><p>Enclosing characters in quotes which are not part of
  514. <a class="reference internal" href="#shlex.shlex.escapedquotes" title="shlex.shlex.escapedquotes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">escapedquotes</span></code></a> (e.g. <code class="docutils literal notranslate"><span class="pre">&quot;'&quot;</span></code>) preserve the literal value
  515. of all characters within the quotes;</p></li>
  516. <li><p>Enclosing characters in quotes which are part of
  517. <a class="reference internal" href="#shlex.shlex.escapedquotes" title="shlex.shlex.escapedquotes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">escapedquotes</span></code></a> (e.g. <code class="docutils literal notranslate"><span class="pre">'&quot;'</span></code>) preserves the literal value
  518. of all characters within the quotes, with the exception of the characters
  519. mentioned in <a class="reference internal" href="#shlex.shlex.escape" title="shlex.shlex.escape"><code class="xref py py-attr docutils literal notranslate"><span class="pre">escape</span></code></a>. The escape characters retain its
  520. special meaning only when followed by the quote in use, or the escape
  521. character itself. Otherwise the escape character will be considered a
  522. normal character.</p></li>
  523. <li><p>EOF is signaled with a <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> value;</p></li>
  524. <li><p>Quoted empty strings (<code class="docutils literal notranslate"><span class="pre">''</span></code>) are allowed.</p></li>
  525. </ul>
  526. </section>
  527. <section id="improved-compatibility-with-shells">
  528. <span id="improved-shell-compatibility"></span><h2>Improved Compatibility with Shells<a class="headerlink" href="#improved-compatibility-with-shells" title="Link to this heading">¶</a></h2>
  529. <div class="versionadded">
  530. <p><span class="versionmodified added">New in version 3.6.</span></p>
  531. </div>
  532. <p>The <a class="reference internal" href="#module-shlex" title="shlex: Simple lexical analysis for Unix shell-like languages."><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a> class provides compatibility with the parsing performed by
  533. common Unix shells like <code class="docutils literal notranslate"><span class="pre">bash</span></code>, <code class="docutils literal notranslate"><span class="pre">dash</span></code>, and <code class="docutils literal notranslate"><span class="pre">sh</span></code>. To take advantage of
  534. this compatibility, specify the <code class="docutils literal notranslate"><span class="pre">punctuation_chars</span></code> argument in the
  535. constructor. This defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>, which preserves pre-3.6 behaviour.
  536. However, if it is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, then parsing of the characters <code class="docutils literal notranslate"><span class="pre">();&lt;&gt;|&amp;</span></code>
  537. is changed: any run of these characters is returned as a single token. While
  538. this is short of a full parser for shells (which would be out of scope for the
  539. standard library, given the multiplicity of shells out there), it does allow
  540. you to perform processing of command lines more easily than you could
  541. otherwise. To illustrate, you can see the difference in the following snippet:</p>
  542. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="go"> &gt;&gt;&gt; import shlex</span>
  543. <span class="go"> &gt;&gt;&gt; text = &quot;a &amp;&amp; b; c &amp;&amp; d || e; f &gt;&#39;abc&#39;; (def \&quot;ghi\&quot;)&quot;</span>
  544. <span class="go"> &gt;&gt;&gt; s = shlex.shlex(text, posix=True)</span>
  545. <span class="go"> &gt;&gt;&gt; s.whitespace_split = True</span>
  546. <span class="go"> &gt;&gt;&gt; list(s)</span>
  547. <span class="go"> [&#39;a&#39;, &#39;&amp;&amp;&#39;, &#39;b;&#39;, &#39;c&#39;, &#39;&amp;&amp;&#39;, &#39;d&#39;, &#39;||&#39;, &#39;e;&#39;, &#39;f&#39;, &#39;&gt;abc;&#39;, &#39;(def&#39;, &#39;ghi)&#39;]</span>
  548. <span class="go"> &gt;&gt;&gt; s = shlex.shlex(text, posix=True, punctuation_chars=True)</span>
  549. <span class="go"> &gt;&gt;&gt; s.whitespace_split = True</span>
  550. <span class="go"> &gt;&gt;&gt; list(s)</span>
  551. <span class="go"> [&#39;a&#39;, &#39;&amp;&amp;&#39;, &#39;b&#39;, &#39;;&#39;, &#39;c&#39;, &#39;&amp;&amp;&#39;, &#39;d&#39;, &#39;||&#39;, &#39;e&#39;, &#39;;&#39;, &#39;f&#39;, &#39;&gt;&#39;, &#39;abc&#39;, &#39;;&#39;,</span>
  552. <span class="go"> &#39;(&#39;, &#39;def&#39;, &#39;ghi&#39;, &#39;)&#39;]</span>
  553. </pre></div>
  554. </div>
  555. <p>Of course, tokens will be returned which are not valid for shells, and you’ll
  556. need to implement your own error checks on the returned tokens.</p>
  557. <p>Instead of passing <code class="docutils literal notranslate"><span class="pre">True</span></code> as the value for the punctuation_chars parameter,
  558. you can pass a string with specific characters, which will be used to determine
  559. which characters constitute punctuation. For example:</p>
  560. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">shlex</span>
  561. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">shlex</span><span class="o">.</span><span class="n">shlex</span><span class="p">(</span><span class="s2">&quot;a &amp;&amp; b || c&quot;</span><span class="p">,</span> <span class="n">punctuation_chars</span><span class="o">=</span><span class="s2">&quot;|&quot;</span><span class="p">)</span>
  562. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  563. <span class="go">[&#39;a&#39;, &#39;&amp;&#39;, &#39;&amp;&#39;, &#39;b&#39;, &#39;||&#39;, &#39;c&#39;]</span>
  564. </pre></div>
  565. </div>
  566. <div class="admonition note">
  567. <p class="admonition-title">Note</p>
  568. <p>When <code class="docutils literal notranslate"><span class="pre">punctuation_chars</span></code> is specified, the <a class="reference internal" href="#shlex.shlex.wordchars" title="shlex.shlex.wordchars"><code class="xref py py-attr docutils literal notranslate"><span class="pre">wordchars</span></code></a>
  569. attribute is augmented with the characters <code class="docutils literal notranslate"><span class="pre">~-./*?=</span></code>. That is because these
  570. characters can appear in file names (including wildcards) and command-line
  571. arguments (e.g. <code class="docutils literal notranslate"><span class="pre">--color=auto</span></code>). Hence:</p>
  572. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">shlex</span>
  573. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">shlex</span><span class="o">.</span><span class="n">shlex</span><span class="p">(</span><span class="s1">&#39;~/a &amp;&amp; b-c --color=auto || d *.py?&#39;</span><span class="p">,</span>
  574. <span class="gp">... </span> <span class="n">punctuation_chars</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  575. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  576. <span class="go">[&#39;~/a&#39;, &#39;&amp;&amp;&#39;, &#39;b-c&#39;, &#39;--color=auto&#39;, &#39;||&#39;, &#39;d&#39;, &#39;*.py?&#39;]</span>
  577. </pre></div>
  578. </div>
  579. <p>However, to match the shell as closely as possible, it is recommended to
  580. always use <code class="docutils literal notranslate"><span class="pre">posix</span></code> and <a class="reference internal" href="#shlex.shlex.whitespace_split" title="shlex.shlex.whitespace_split"><code class="xref py py-attr docutils literal notranslate"><span class="pre">whitespace_split</span></code></a> when using
  581. <a class="reference internal" href="#shlex.shlex.punctuation_chars" title="shlex.shlex.punctuation_chars"><code class="xref py py-attr docutils literal notranslate"><span class="pre">punctuation_chars</span></code></a>, which will negate
  582. <a class="reference internal" href="#shlex.shlex.wordchars" title="shlex.shlex.wordchars"><code class="xref py py-attr docutils literal notranslate"><span class="pre">wordchars</span></code></a> entirely.</p>
  583. </div>
  584. <p>For best effect, <code class="docutils literal notranslate"><span class="pre">punctuation_chars</span></code> should be set in conjunction with
  585. <code class="docutils literal notranslate"><span class="pre">posix=True</span></code>. (Note that <code class="docutils literal notranslate"><span class="pre">posix=False</span></code> is the default for
  586. <a class="reference internal" href="#shlex.shlex" title="shlex.shlex"><code class="xref py py-class docutils literal notranslate"><span class="pre">shlex</span></code></a>.)</p>
  587. </section>
  588. </section>
  589. <div class="clearer"></div>
  590. </div>
  591. </div>
  592. </div>
  593. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  594. <div class="sphinxsidebarwrapper">
  595. <div>
  596. <h3><a href="../contents.html">Table of Contents</a></h3>
  597. <ul>
  598. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code> — Simple lexical analysis</a><ul>
  599. <li><a class="reference internal" href="#shlex-objects">shlex Objects</a></li>
  600. <li><a class="reference internal" href="#parsing-rules">Parsing Rules</a></li>
  601. <li><a class="reference internal" href="#improved-compatibility-with-shells">Improved Compatibility with Shells</a></li>
  602. </ul>
  603. </li>
  604. </ul>
  605. </div>
  606. <div>
  607. <h4>Previous topic</h4>
  608. <p class="topless"><a href="cmd.html"
  609. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> — Support for line-oriented command interpreters</a></p>
  610. </div>
  611. <div>
  612. <h4>Next topic</h4>
  613. <p class="topless"><a href="tk.html"
  614. title="next chapter">Graphical User Interfaces with Tk</a></p>
  615. </div>
  616. <div role="note" aria-label="source link">
  617. <h3>This Page</h3>
  618. <ul class="this-page-menu">
  619. <li><a href="../bugs.html">Report a Bug</a></li>
  620. <li>
  621. <a href="https://github.com/python/cpython/blob/main/Doc/library/shlex.rst"
  622. rel="nofollow">Show Source
  623. </a>
  624. </li>
  625. </ul>
  626. </div>
  627. </div>
  628. <div id="sidebarbutton" title="Collapse sidebar">
  629. <span>«</span>
  630. </div>
  631. </div>
  632. <div class="clearer"></div>
  633. </div>
  634. <div class="related" role="navigation" aria-label="related navigation">
  635. <h3>Navigation</h3>
  636. <ul>
  637. <li class="right" style="margin-right: 10px">
  638. <a href="../genindex.html" title="General Index"
  639. >index</a></li>
  640. <li class="right" >
  641. <a href="../py-modindex.html" title="Python Module Index"
  642. >modules</a> |</li>
  643. <li class="right" >
  644. <a href="tk.html" title="Graphical User Interfaces with Tk"
  645. >next</a> |</li>
  646. <li class="right" >
  647. <a href="cmd.html" title="cmd — Support for line-oriented command interpreters"
  648. >previous</a> |</li>
  649. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  650. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  651. <li class="switchers">
  652. <div class="language_switcher_placeholder"></div>
  653. <div class="version_switcher_placeholder"></div>
  654. </li>
  655. <li>
  656. </li>
  657. <li id="cpython-language-and-version">
  658. <a href="../index.html">3.12.3 Documentation</a> &#187;
  659. </li>
  660. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  661. <li class="nav-item nav-item-2"><a href="frameworks.html" >Program Frameworks</a> &#187;</li>
  662. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code> — Simple lexical analysis</a></li>
  663. <li class="right">
  664. <div class="inline-search" role="search">
  665. <form class="inline-search" action="../search.html" method="get">
  666. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  667. <input type="submit" value="Go" />
  668. </form>
  669. </div>
  670. |
  671. </li>
  672. <li class="right">
  673. <label class="theme-selector-label">
  674. Theme
  675. <select class="theme-selector" oninput="activateTheme(this.value)">
  676. <option value="auto" selected>Auto</option>
  677. <option value="light">Light</option>
  678. <option value="dark">Dark</option>
  679. </select>
  680. </label> |</li>
  681. </ul>
  682. </div>
  683. <div class="footer">
  684. &copy;
  685. <a href="../copyright.html">
  686. Copyright
  687. </a>
  688. 2001-2024, Python Software Foundation.
  689. <br />
  690. This page is licensed under the Python Software Foundation License Version 2.
  691. <br />
  692. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  693. <br />
  694. See <a href="/license.html">History and License</a> for more information.<br />
  695. <br />
  696. The Python Software Foundation is a non-profit corporation.
  697. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  698. <br />
  699. <br />
  700. Last updated on Apr 09, 2024 (13:47 UTC).
  701. <a href="/bugs.html">Found a bug</a>?
  702. <br />
  703. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  704. </div>
  705. </body>
  706. </html>
上海开阖软件有限公司 沪ICP备12045867号-1