如何读取/处理命令行参数? [英] How to read/process command line arguments?
本文介绍了如何读取/处理命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我本来是C程序员。我已经看到许多技巧和技巧来阅读许多不同的论点。
Python程序员可以执行哪些操作?
相关
- 获取/解析传递给Python脚本的命令行参数的最佳方法是什么?
- 实现 [command] [action] [parameter]样式的命令行界面吗?
- 如何在Python中处理命令行参数?
- 如何使用Python的optparse格式化位置参数帮助?
解决方案
标准库中的规范解决方案是 argparse
(文档):
以下是示例:
从argparse导入ArgumentParser
parser = ArgumentParser()
parser.add_argument(-f, --file,dest = filename,
help =将报告写入FILE,metavar = FILE)
parser.add_argument(-q, --quiet,
action = store_false,dest = verbose,默认= True,
help =不将状态消息打印到标准输出)
args = parser.parse_args()
argparse
支持(除其他功能外):
- 任意顺序的多个选项。
- 短和长选项。
- 默认值。
- 生成使用帮助消息。
I am originally a C programmer. I have seen numerous tricks and "hacks" to read many different arguments.
What are some of the ways Python programmers can do this?
Related
- 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?
解决方案
The canonical solution in the standard library is argparse
(docs):
Here is an example:
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-f", "--file", dest="filename",
help="write report to FILE", metavar="FILE")
parser.add_argument("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
args = parser.parse_args()
argparse
supports (among other things):
- Multiple options in any order.
- Short and long options.
- Default values.
- Generation of a usage help message.
这篇关于如何读取/处理命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文