python中 命令行参数解析模块 argparse

 

001、

[root@PC1 test]# ls
hello.py
[root@PC1 test]# cat hello.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import argparse          ## 导入模块

parser = argparse.ArgumentParser()        ## 创建参数解析器对象 parser
parser.add_argument('--name', type=str, help='Your name')    ## 定义一个命令行参数 --name
args = parser.parse_args()                              ## 解析命令行输入的参数,并把结果保存在 args

print(f"Hello, {args.name}!")
[root@PC1 test]# python hello.py --name zhangsan
Hello, zhangsan!

image

。 

 

posted @ 2025-07-29 17:36  小鲨鱼2018  阅读(4)  评论(0)    收藏  举报