擅长:python、mysql、java
<p>如注释中所述,<code>argparse</code>与由shell和Python解释器生成的列表<code>sys.argv</code>一起工作。</p>
<p>使用简单的<code>argv</code>echo脚本:</p>
<pre><code>0928:~/mypy$ cat echo_argv.py
import sys
print(sys.argv)
</code></pre>
<p>我得到(一个猛击壳):</p>
<pre><code>0929:~/mypy$ python echo_argv.py Here is a list:\nItem 1
['echo_argv.py', 'Here', 'is', 'a', 'list:nItem', '1']
0931:~/mypy$ python echo_argv.py "Here is a list:\nItem 1 "
['echo_argv.py', 'Here is a list:\\nItem 1 ']
0931:~/mypy$ python echo_argv.py "Here is a list:\\nItem 1 "
['echo_argv.py', 'Here is a list:\\nItem 1 ']
</code></pre>
<p><code>argparse</code>将<code>argv</code>视为字符串列表。它对那些字符串没有任何作用,至少对默认的<code>None</code><code>type</code>参数没有作用。</p>