本文介绍了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?
- 。 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:
- 模块类似于GNU getopt。
- 模块提供面向对象的命令行选项解析。
- 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中的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!