python获取命令行参数实例方法讲解
作者:PYCN 时间:2022-12-10 06:40:32
Python 在命令行解析方面给出了类似的几个选择:自己解析, 自给自足(batteries-included)的方式,以及大量的第三方方式。
自己解析
你可以从 sys 模块中获取程序的参数。
import sys
if __name__ == '__main__':
for value in sys.argv:
print(value)
自给自足
在 Python 标准库中已经有几个参数解析模块的实现: getopt 、 optparse ,以及最近的 argparse 。argparse 允许程序员为用户提供一致的、有帮助的用户体验,但就像它的 GNU 前辈一样,它需要程序员做大量的工作和“ 模板代码 ”才能使它“奏效”。
from argparse import ArgumentParser
if __name__ == "__main__":
argparser = ArgumentParser(description='My Cool Program')
argparser.add_argument("--foo", "-f", help="A user supplied foo")
argparser.add_argument("--bar", "-b", help="A user supplied bar")
results = argparser.parse_args()
print(results.foo, results.bar)
CLI 的现代方法
Click 框架使用 装饰器 的方式来构建命令行解析。
import click
@click.command()
@click.option("-f", "--foo", default="foo", help="User supplied foo.")
@click.option("-b", "--bar", default="bar", help="User supplied bar.")
def echo(foo, bar):
"""My Cool Program
It does stuff. Here is the documentation for it.
"""
print(foo, bar)
if __name__ == "__main__":
echo()
在 Click 接口中添加参数就像在堆栈中添加另一个装饰符并将新的参数添加到函数定义中一样简单。
知识拓展:
Typer 建立在 Click 之上,是一个更新的 CLI 框架,它结合了 Click 的功能和现代 Python 类型提示 。使用 Click 的缺点之一是必须在函数中添加一堆装饰符。CLI 参数必须在两个地方指定:装饰符和函数参数列表。Typer 免去你造轮子 去写 CLI 规范,让代码更容易阅读和维护。
import typer
cli = typer.Typer()
@cli.command()
def echo(foo: str = "foo", bar: str = "bar"):
"""My Cool Program
It does stuff. Here is the documentation for it.
"""
print(foo, bar)
if __name__ == "__main__":
cli()
来源:https://www.py.cn/jishu/jichu/20400.html
标签:python,命令行参数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python入门教程之Python的安装下载配置
2021-08-18 06:01:14
![](https://img.aspxhome.com/file/2023/7/86597_0s.png)
Python中with上下文管理协议的作用及用法
2022-09-27 16:05:24
![](https://img.aspxhome.com/file/2023/0/135250_0s.png)
python制作定时发送信息脚本的实现思路
2023-01-17 15:24:11
![](https://img.aspxhome.com/file/2023/0/105050_0s.png)
vue-Split实现面板分割
2024-04-27 15:49:43
![](https://img.aspxhome.com/file/2023/9/133099_0s.jpg)
mysql慢查询日志轮转_MySQL慢查询日志实操
2024-01-26 05:54:14
![](https://img.aspxhome.com/file/2023/5/69775_0s.png)
详解numpy1.19.4与python3.9版本冲突解决
2021-10-26 22:51:26
![](https://img.aspxhome.com/file/2023/6/123546_0s.png)
python3 字符串str和bytes相互转换
2023-03-31 09:12:47
关于Python中字符串的各种操作
2022-06-25 16:18:38
用Python判断奇偶数示例
2021-03-17 22:18:22
![](https://img.aspxhome.com/file/2023/5/97545_0s.png)
golang实现的文件上传下载小工具
2023-06-28 05:34:34
Django零基础入门之运行Django版的hello world
2022-11-10 02:44:58
![](https://img.aspxhome.com/file/2023/1/69611_0s.jpg)
Bootstrap table分页问题汇总
2024-04-29 13:12:07
golang 中strings包的Replace的使用说明
2024-02-09 16:02:04
Go语言包管理工具Godep的用法
2024-02-20 04:09:44
![](https://img.aspxhome.com/file/2023/0/105150_0s.png)
Python如何提取csv数据并筛选指定条件数据详解
2022-07-20 22:47:59
![](https://img.aspxhome.com/file/2023/0/94060_0s.png)
Python 如何调试程序崩溃错误
2022-04-27 15:22:30
python实现字符串加密 生成唯一固定长度字符串
2022-02-04 21:02:27
python计算N天之后日期的方法
2022-04-20 10:33:51
python使用心得之获得github代码库列表
2023-12-01 21:31:51
SQL实现相邻两行数据的加减乘除操作
2024-01-24 23:12:34
![](https://img.aspxhome.com/file/2023/6/118146_0s.jpg)