569 lines
53 KiB
HTML
569 lines
53 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>shlex —— 简单的词义分析 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="Tk图形用户界面(GUI)" href="tk.html" />
|
||
<link rel="prev" title="cmd --- 支持面向行的命令解释器" href="cmd.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/shlex.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
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" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code> —— 简单的词义分析</a><ul>
|
||
<li><a class="reference internal" href="#shlex-objects">shlex 对象</a></li>
|
||
<li><a class="reference internal" href="#parsing-rules">解析规则</a></li>
|
||
<li><a class="reference internal" href="#improved-compatibility-with-shells">改进的 shell 兼容性</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="cmd.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> --- 支持面向行的命令解释器</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="tk.html"
|
||
title="下一章">Tk图形用户界面(GUI)</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/shlex.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/shlex.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="tk.html" title="Tk图形用户界面(GUI)"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="cmd.html" title="cmd --- 支持面向行的命令解释器"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="frameworks.html" accesskey="U">程序框架</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-shlex">
|
||
<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> —— 简单的词义分析<a class="headerlink" href="#module-shlex" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/shlex.py">Lib/shlex.py</a></p>
|
||
<hr class="docutils" />
|
||
<p><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> 类可用于编写类似 Unix shell 的简单词义分析程序。通常可用于编写“迷你语言”(如 Python 应用程序的运行控制文件)或解析带引号的字符串。</p>
|
||
<p><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> 模块中定义了以下函数:</p>
|
||
<dl class="function">
|
||
<dt id="shlex.split">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">split</code><span class="sig-paren">(</span><em class="sig-param">s</em>, <em class="sig-param">comments=False</em>, <em class="sig-param">posix=True</em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.split" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用类似 shell 的语法拆分字符串 <em>s</em>。如果 <em>comments</em> 为 <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> (默认值),则不会解析给定字符串中的注释 (<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> 属性的 <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> 实例设为空字符串)。 本函数默认工作于 POSIX 模式下,但若 <em>posix</em> 参数为 False,则采用非 POSIX 模式。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>Since the <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> function instantiates 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, passing <code class="docutils literal notranslate"><span class="pre">None</span></code> for <em>s</em> will read the string to split from
|
||
standard input.</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shlex.join">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">join</code><span class="sig-paren">(</span><em class="sig-param">split_command</em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.join" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将列表 <em>split_command</em> 中的单词串联起来,返回一个字符串。本函数是 <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>
|
||
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">shlex</span> <span class="kn">import</span> <span class="n">join</span>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">join</span><span class="p">([</span><span class="s1">'echo'</span><span class="p">,</span> <span class="s1">'-n'</span><span class="p">,</span> <span class="s1">'Multiple words'</span><span class="p">]))</span>
|
||
<span class="go">echo -n 'Multiple words'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>为防止注入漏洞,返回值是经过 shell 转义的(参见 <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>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.8 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="shlex.quote">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">quote</code><span class="sig-paren">(</span><em class="sig-param">s</em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.quote" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回经过 shell 转义的字符串 <em>s</em> 。返回值为字符串,可以安全地作为 shell 命令行中的单词使用,可用于不能使用列表的场合。</p>
|
||
<p>以下用法是不安全的:</p>
|
||
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">filename</span> <span class="o">=</span> <span class="s1">'somefile; rm -rf ~'</span>
|
||
<span class="gp">>>> </span><span class="n">command</span> <span class="o">=</span> <span class="s1">'ls -l </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="go">ls -l somefile; rm -rf ~</span>
|
||
</pre></div>
|
||
</div>
|
||
<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> 可以堵住这种安全漏洞:</p>
|
||
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">shlex</span> <span class="kn">import</span> <span class="n">quote</span>
|
||
<span class="gp">>>> </span><span class="n">command</span> <span class="o">=</span> <span class="s1">'ls -l </span><span class="si">{}</span><span class="s1">'</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>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">command</span><span class="p">)</span>
|
||
<span class="go">ls -l 'somefile; rm -rf ~'</span>
|
||
<span class="gp">>>> </span><span class="n">remote_command</span> <span class="o">=</span> <span class="s1">'ssh home </span><span class="si">{}</span><span class="s1">'</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>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">remote_command</span><span class="p">)</span>
|
||
<span class="go">ssh home 'ls -l '"'"'somefile; rm -rf ~'"'"''</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>这种包装方式兼容于 UNIX shell 和 <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>
|
||
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">shlex</span> <span class="kn">import</span> <span class="n">split</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </span><span class="n">remote_command</span>
|
||
<span class="go">['ssh', 'home', "ls -l 'somefile; rm -rf ~'"]</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </span><span class="n">command</span>
|
||
<span class="go">['ls', '-l', 'somefile; rm -rf ~']</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.3 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<p><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> 模块中定义了以下类:</p>
|
||
<dl class="class">
|
||
<dt id="shlex.shlex">
|
||
<em class="property">class </em><code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">shlex</code><span class="sig-paren">(</span><em class="sig-param">instream=None</em>, <em class="sig-param">infile=None</em>, <em class="sig-param">posix=False</em>, <em class="sig-param">punctuation_chars=False</em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><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> 及其子类的实例是一种词义分析器对象。 利用初始化参数可指定从哪里读取字符。 初始化参数必须是具备 <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> 和 <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> 方法的文件/流对象,或者是一个字符串。 如果没有给出初始化参数,则会从 <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code> 获取输入。 第二个可选参数是个文件名字符串,用于设置 <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> 属性的初始值。 如果 <em>instream</em> 参数被省略或等于 <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>,则第二个参数默认为 "stdin"。 <em>posix</em> 参数定义了操作的模式:若 <em>posix</em> 不为真值(默认),则 <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> 实例将工作于兼容模式。 若运行于 POSIX 模式下,则 <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> 会尽可能地应用 POSIX shell 解析规则。 <em>punctuation_chars</em> 参数提供了一种使行为更接近于真正的 shell 解析的方式。 该参数可接受多种值:默认值、<code class="docutils literal notranslate"><span class="pre">False</span></code>、保持 Python 3.5 及更早版本的行为。 如果设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则会改变对字符 <code class="docutils literal notranslate"><span class="pre">();<>|&</span></code> 的解析方式:这些字符将作为独立的形符被返回(视作标点符号)。 如果设为非空字符串,则这些字符将被用作标点符号。 出现在 <em>punctuation_chars</em> 中的 <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> 属性中的任何字符都会从 <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> 中被删除。 请参阅 <a class="reference internal" href="#improved-shell-compatibility"><span class="std std-ref">改进的 shell 兼容性</span></a> 了解详情。 <em>punctuation_chars</em> 只能在创建 <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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>加入 <em>punctuation_chars</em> 参数。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<dl class="simple">
|
||
<dt><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>配置文件解析器,类似于 Windows 的 <code class="file docutils literal notranslate"><span class="pre">.ini</span></code> 文件。</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
<section id="shlex-objects">
|
||
<span id="id1"></span><h2>shlex 对象<a class="headerlink" href="#shlex-objects" title="永久链接至标题">¶</a></h2>
|
||
<p><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>
|
||
<dl class="method">
|
||
<dt id="shlex.shlex.get_token">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">get_token</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.get_token" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回一个单词。如果所有单词已用 <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> 堆叠在一起了,则从堆中弹出一个单词。否则就从输入流中读取一个。如果读取时遇到文件结束符,则会返回 <code class="xref py py-attr docutils literal notranslate"><span class="pre">eof`(在非</span> <span class="pre">POSIX</span> <span class="pre">模式下为空字符串</span> <span class="pre">`</span></code>''<code class="docutils literal notranslate"><span class="pre">,在</span> <span class="pre">POSIX</span> <span class="pre">模式下为</span> <span class="pre">``None</span></code>)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="shlex.shlex.push_token">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">push_token</code><span class="sig-paren">(</span><em class="sig-param">str</em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.push_token" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将参数值压入单词堆栈。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="shlex.shlex.read_token">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">read_token</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.read_token" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>读取一个原单词。忽略堆栈,且不解释源请求。(通常没什么用,只是为了完整起见。)</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="shlex.shlex.sourcehook">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">sourcehook</code><span class="sig-paren">(</span><em class="sig-param">filename</em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.sourcehook" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>当 <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> 检测到源请求(见下面的 <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>),本方法被赋予以下单词作为参数,并应返回一个由文件名和打开的文件对象组成的元组。</p>
|
||
<p>通常本方法会先移除参数中的引号。如果结果为绝对路径名,或者之前没有有效的源请求,或者之前的源请求是一个流对象(比如 <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>),那么结果将不做处理。否则,如果结果是相对路径名,那么前面将会加上目录部分,目录名来自于源堆栈中前一个文件名(类似于 C 预处理器对 <code class="docutils literal notranslate"><span class="pre">#include</span> <span class="pre">"file.h"</span></code> 的处理方式)。</p>
|
||
<p>结果被视为一个文件名,并作为元组的第一部分返回,元组的第二部分以此为基础调用 <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> 获得。(注意:这与实例初始化中的参数顺序相反!)</p>
|
||
<p>此钩子函数是公开的,可用于实现路径搜索、添加文件扩展名或黑入其他命名空间。没有对应的“关闭”钩子函数,但 shlex 实例在返回 EOF 时会调用源输入流的 <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> 方法。</p>
|
||
<p>若要更明确地控制源堆栈,请采用 <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> 和 <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> 方法。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="shlex.shlex.push_source">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">push_source</code><span class="sig-paren">(</span><em class="sig-param">newstream</em>, <em class="sig-param">newfile=None</em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.push_source" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将输入源流压入输入堆栈。如果指定了文件名参数,以后错误信息中将会用到。这与 <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> 内部使用的方法相同。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="shlex.shlex.pop_source">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">pop_source</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.pop_source" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>从输入堆栈中弹出最后一条输入源。当遇到输入流的 EOF 时,内部也使用同一方法。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="shlex.shlex.error_leader">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">error_leader</code><span class="sig-paren">(</span><em class="sig-param">infile=None</em>, <em class="sig-param">lineno=None</em><span class="sig-paren">)</span><a class="headerlink" href="#shlex.shlex.error_leader" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>本方法生成一条错误信息的首部,以 Unix C 编译器错误标签的形式;格式为``'"%s", line %d: '<code class="docutils literal notranslate"><span class="pre">,其中</span> <span class="pre">``%s</span></code> 被替换为当前源文件的名称,<code class="docutils literal notranslate"><span class="pre">%d</span></code> 被替换为当前输入行号(可用可选参数覆盖)。</p>
|
||
<p>这是个快捷函数,旨在鼓励 <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> 用户以标准的、可解析的格式生成错误信息,以便 Emacs 和其他 Unix 工具理解。</p>
|
||
</dd></dl>
|
||
|
||
<p><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>
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.commenters">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">commenters</code><a class="headerlink" href="#shlex.shlex.commenters" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将被视为注释起始字符串。从注释起始字符串到行尾的所有字符都将被忽略。默认情况下只包括 <code class="docutils literal notranslate"><span class="pre">'#'</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.wordchars">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">wordchars</code><a class="headerlink" href="#shlex.shlex.wordchars" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>可连成多字符单词的字符串。默认包含所有 ASCII 字母数字和下划线。在 POSIX 模式下,Latin-1 字符集的重音字符也被包括在内。如果 <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> 不为空,则可出现在文件名规范和命令行参数中的 <code class="docutils literal notranslate"><span class="pre">~-./*?=</span></code> 字符也将包含在内,任何 <code class="docutils literal notranslate"><span class="pre">punctuation_chars</span></code> 中的字符将从 <code class="docutils literal notranslate"><span class="pre">wordchars</span></code> 中移除。如果 <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> 设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则本规则无效。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.whitespace">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">whitespace</code><a class="headerlink" href="#shlex.shlex.whitespace" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将被视为空白符并跳过的字符。空白符是单词的边界。默认包含空格、制表符、换行符和回车符。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.escape">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">escape</code><a class="headerlink" href="#shlex.shlex.escape" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将视为转义字符。仅适用于 POSIX 模式,默认只包含 <code class="docutils literal notranslate"><span class="pre">'\'</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.quotes">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">quotes</code><a class="headerlink" href="#shlex.shlex.quotes" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将视为引号的字符。单词中的字符将会累至再次遇到同样的引号(因此,不同的引号会像在 shell 中一样相互包含。)默认包含 ASCII 单引号和双引号。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.escapedquotes">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">escapedquotes</code><a class="headerlink" href="#shlex.shlex.escapedquotes" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><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> 中的字符将会解析 <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> 定义的转义字符。这只在 POSIX 模式下使用,默认只包含 <code class="docutils literal notranslate"><span class="pre">'"'</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.whitespace_split">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">whitespace_split</code><a class="headerlink" href="#shlex.shlex.whitespace_split" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>若为 <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> 解析命令行,用类似 shell 参数的方式读取各个单词。当与 <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> 一起使用时,将根据空白符和这些字符拆分单词。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.8 版更改: </span><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> 属性已与 <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> 属性兼容。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.infile">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">infile</code><a class="headerlink" href="#shlex.shlex.infile" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>当前输入的文件名,可能是在类实例化时设置的,或者是由后来的源请求堆栈生成的。在构建错误信息时可能会用到本属性。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.instream">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">instream</code><a class="headerlink" href="#shlex.shlex.instream" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><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>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.source">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">source</code><a class="headerlink" href="#shlex.shlex.source" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>本属性默认值为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 如果给定一个字符串,则会识别为包含请求,类似于各种 shell 中的 <code class="docutils literal notranslate"><span class="pre">source</span></code> 关键字。 也就是说,紧随其后的单词将作为文件名打开,作为输入流,直至遇到 EOF 后调用流的 <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> 方法,然后原输入流仍变回输入源。Source 请求可以在词义堆栈中嵌套任意深度。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.debug">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">debug</code><a class="headerlink" href="#shlex.shlex.debug" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果本属性为大于 <code class="docutils literal notranslate"><span class="pre">1</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> 实例会把动作进度详细地输出出来。若需用到本属性,可阅读源代码来了解细节。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.lineno">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">lineno</code><a class="headerlink" href="#shlex.shlex.lineno" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>源的行数(到目前为止读到的换行符数量加 1)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.token">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">token</code><a class="headerlink" href="#shlex.shlex.token" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>单词缓冲区。在捕获异常时可能会用到。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.eof">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">eof</code><a class="headerlink" href="#shlex.shlex.eof" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用于确定文件结束的单词。在非 POSIX 模式下,将设为空字符串 <code class="docutils literal notranslate"><span class="pre">''</span></code>,在 POSIX 模式下被设为 <code class="docutils literal notranslate"><span class="pre">None</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="attribute">
|
||
<dt id="shlex.shlex.punctuation_chars">
|
||
<code class="sig-prename descclassname">shlex.</code><code class="sig-name descname">punctuation_chars</code><a class="headerlink" href="#shlex.shlex.punctuation_chars" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>只读属性。表示应视作标点符号的字符。标点符号将作为单个单词返回。然而,请注意不会进行语义有效性检查:比如 “>>>” 可能会作为一个单词返回,虽然 shell 可能无法识别。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.6 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="parsing-rules">
|
||
<span id="shlex-parsing-rules"></span><h2>解析规则<a class="headerlink" href="#parsing-rules" title="永久链接至标题">¶</a></h2>
|
||
<p>在非 POSIX 模式下时,<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>
|
||
<ul class="simple">
|
||
<li><p>不识别单词中的引号(<code class="docutils literal notranslate"><span class="pre">Do"Not"Separate</span></code> 解析为一个单词 <code class="docutils literal notranslate"><span class="pre">Do"Not"Separate</span></code>)。</p></li>
|
||
<li><p>不识别转义字符;</p></li>
|
||
<li><p>引号包裹的字符保留字面意思。</p></li>
|
||
<li><p>成对的引号会将单词分离(<code class="docutils literal notranslate"><span class="pre">"Do"Separate</span></code> 解析为 <code class="docutils literal notranslate"><span class="pre">"Do"</span></code> 和 <code class="docutils literal notranslate"><span class="pre">Separate</span></code>)。</p></li>
|
||
<li><p>如果 <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> 为 <code class="docutils literal notranslate"><span class="pre">False</span></code>,则未声明为单词字符、空白或引号的字符将作为单字符标记返回。若为 <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> 只根据空白符拆分单词。</p></li>
|
||
<li><p>EOF 用空字符串(<code class="docutils literal notranslate"><span class="pre">''</span></code>)表示;</p></li>
|
||
<li><p>空字符串无法解析,即便是加了引号。</p></li>
|
||
</ul>
|
||
<p>在 POSIX 模式时,<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>
|
||
<ul class="simple">
|
||
<li><p>引号会被剔除,且不会拆分单词( <code class="docutils literal notranslate"><span class="pre">"Do"Not"Separate"</span></code> 将解析为单个单词 <code class="docutils literal notranslate"><span class="pre">DoNotSeparate</span></code>)。</p></li>
|
||
<li><p>未加引号包裹的转义字符(如 <code class="docutils literal notranslate"><span class="pre">'\'</span></code> )保留后一个字符的字面意思;</p></li>
|
||
<li><p>引号中的字符不属于 <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> (例如,<code class="docutils literal notranslate"><span class="pre">"'"</span></code>),则保留引号中所有字符的字面值。</p></li>
|
||
<li><p>若引号包裹的字符属于 <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> (例如 <code class="docutils literal notranslate"><span class="pre">'"'</span></code>),则保留引号中所有字符的字面意思,属于 <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> 中的字符除外。仅当后跟后半个引号或转义字符本身时,转义字符才保留其特殊含义。否则,转义字符将视作普通字符。</p></li>
|
||
<li><p>EOF 用 <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> 表示。</p></li>
|
||
<li><p>允许出现引号包裹的空字符串(<code class="docutils literal notranslate"><span class="pre">''</span></code>)。</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="improved-compatibility-with-shells">
|
||
<span id="improved-shell-compatibility"></span><h2>改进的 shell 兼容性<a class="headerlink" href="#improved-compatibility-with-shells" title="永久链接至标题">¶</a></h2>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.6 新版功能.</span></p>
|
||
</div>
|
||
<p><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> 类提供了与常见 Unix shell(如 <code class="docutils literal notranslate"><span class="pre">bash</span></code>、<code class="docutils literal notranslate"><span class="pre">dash</span></code> 和``sh``)的解析兼容性。为了充分利用这种兼容性,请在构造函数中设定 <code class="docutils literal notranslate"><span class="pre">punctuation_chars</span></code> 参数。该参数默认为 <code class="docutils literal notranslate"><span class="pre">False</span></code>,维持 3.6 以下版本的行为。如果设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则会改变对 <code class="docutils literal notranslate"><span class="pre">();<>|&</span></code> 字符的解析方式:这些字符都将视为单个标记返回。虽然不算是完整的 shell 解析程序(考虑到 shell 的多样性,超出了标准库的范围),但确实能比其他方式更容易进行命令行的处理。以下代码段演示了两者的差异:</p>
|
||
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="go"> >>> import shlex</span>
|
||
<span class="go"> >>> text = "a && b; c && d || e; f >'abc'; (def \"ghi\")"</span>
|
||
<span class="go"> >>> s = shlex.shlex(text, posix=True)</span>
|
||
<span class="go"> >>> s.whitespace_split = True</span>
|
||
<span class="go"> >>> list(s)</span>
|
||
<span class="go"> ['a', '&&', 'b;', 'c', '&&', 'd', '||', 'e;', 'f', '>abc;', '(def', 'ghi)']</span>
|
||
<span class="go"> >>> s = shlex.shlex(text, posix=True, punctuation_chars=True)</span>
|
||
<span class="go"> >>> s.whitespace_split = True</span>
|
||
<span class="go"> >>> list(s)</span>
|
||
<span class="go"> ['a', '&&', 'b', ';', 'c', '&&', 'd', '||', 'e', ';', 'f', '>', 'abc', ';',</span>
|
||
<span class="go"> '(', 'def', 'ghi', ')']</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>当然,返回的词法单元对 shell 无效,需要对返回的词法单元自行进行错误检查。</p>
|
||
<p>punctuation_chars 参数可以不传入 <code class="docutils literal notranslate"><span class="pre">True</span></code> ,而是传入包含特定字符的字符串,用于确定由哪些字符构成标点符号。例如:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">shlex</span>
|
||
<span class="gp">>>> </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">"a && b || c"</span><span class="p">,</span> <span class="n">punctuation_chars</span><span class="o">=</span><span class="s2">"|"</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
|
||
<span class="go">['a', '&', '&', 'b', '||', 'c']</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>如果指定了 <code class="docutils literal notranslate"><span class="pre">punctuation_chars</span></code>,则 <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> 属性的参数会是 <code class="docutils literal notranslate"><span class="pre">~-./*?=</span></code>。因为这些字符可以出现在文件名(包括通配符)和命令行参数中(如 <code class="docutils literal notranslate"><span class="pre">--color=auto</span></code>)。因此:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">shlex</span>
|
||
<span class="gp">>>> </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">'~/a && b-c --color=auto || d *.py?'</span><span class="p">,</span>
|
||
<span class="gp">... </span> <span class="n">punctuation_chars</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
|
||
<span class="go">['~/a', '&&', 'b-c', '--color=auto', '||', 'd', '*.py?']</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>不过为了尽可能接近于 shell ,建议在使用 <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> 时始终使用 <code class="docutils literal notranslate"><span class="pre">posix</span></code> 和 <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> ,这将完全否定 <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> 。</p>
|
||
</div>
|
||
<p>为了达到最佳效果,<code class="docutils literal notranslate"><span class="pre">punctuation_chars</span></code> 应与 <code class="docutils literal notranslate"><span class="pre">posix=True</span></code> 一起设置。(注意 <code class="docutils literal notranslate"><span class="pre">posix=False</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> 的默认设置)。</p>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code> —— 简单的词义分析</a><ul>
|
||
<li><a class="reference internal" href="#shlex-objects">shlex 对象</a></li>
|
||
<li><a class="reference internal" href="#parsing-rules">解析规则</a></li>
|
||
<li><a class="reference internal" href="#improved-compatibility-with-shells">改进的 shell 兼容性</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="cmd.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code> --- 支持面向行的命令解释器</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="tk.html"
|
||
title="下一章">Tk图形用户界面(GUI)</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/shlex.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="tk.html" title="Tk图形用户界面(GUI)"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="cmd.html" title="cmd --- 支持面向行的命令解释器"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="frameworks.html" >程序框架</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |