Python中的命令行参数 [英] Command Line Arguments In Python
本文介绍了Python中的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我最初是一个C程序员。我看到了许多技巧和黑客阅读许多不同的论点。
I am originally a C programmer. I have seen numerous tricks and "hacks" to read many different arguments.
Python程序员可以做些什么?
What are some of the ways Python programmers can do this?
- http://stackoverflow.com/questions/362426/implementing-a-command-action-parameter-style-command-line-interfaces\">实现[command] [action] [parameter]样式的命令行界面?
- 如何在Python中处理命令行参数?
- 如何使用Python的optparse格式化位置参数帮助?
- What’s the best way to grab/parse command line arguments passed to a Python script?
- Implementing a "[command] [action] [parameter]" style command-line interfaces?
- How can I process command line arguments in Python?
- How do I format positional argument help using Python’s optparse?
推荐答案
请注意,optparse在Python 2.7版中已被弃用:
http://docs.python.org/2/library/optparse.html 。 argparse 是替换项: http:// docs.python.org/2/library/argparse.html#module-argparse
Please note that optparse was deprecated in version 2.7 of Python:
http://docs.python.org/2/library/optparse.html. argparse is the replacement: http://docs.python.org/2/library/argparse.html#module-argparse
有标准库中的以下模块:
There are the following modules in the standard library:
- The getopt module is similar to GNU getopt.
- The optparse module offers object-oriented command line option parsing.
以下是使用文档中后者的示例:
Here is an example that uses the latter from the docs:
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
(options, args) = parser.parse_args()
optparse支持(除其他外):
optparse supports (among other things):
- 以任何顺序显示多个选项。
- 短期和长期选项。
- >生成使用帮助消息。
这篇关于Python中的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文