楼主: Dear_Li
1033 0

[程序分享] Python 中argparse模块的使用 [推广有奖]

  • 0关注
  • 14粉丝

等待验证会员

已卖:405份资源

博士生

64%

还不是VIP/贵宾

-

威望
0
论坛币
699 个
通用积分
7.9246
学术水平
12 点
热心指数
16 点
信用等级
2 点
经验
4905 点
帖子
186
精华
0
在线时间
246 小时
注册时间
2018-7-21
最后登录
2019-1-16

楼主
Dear_Li 发表于 2018-9-18 12:35:50 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Python解析命令行读取参数有两种方式:sys.argv和argparse


1、sys.argv

如果脚本很简单或临时使用,没有多个复杂的参数选项,可以直接利用sys.argv将脚本后的参数依次读取(读进来的默认是字符串格式)。

  1. import sysprint("输入的参数为:%s" % sys.argv[1])
复制代码

命令行执行效果:

  1. >python demo.py 1输入的参数为:1
复制代码


2、argparse

如果参数很多,比较复杂,并且类型不统一,那么argparse可以很好的解决这些问题,下面一个实例解释了argparse的基本使用方法。

  1. import argparse
  2. # description参数可以用于描述脚本的参数作用,默认为空
  3. parser=argparse.ArgumentParser(description="A description of what the program does")
  4. parser.add_argument('--toy','-t',action='store_true',help='Use only 50K samples of data')
  5.   parser.add_argument('--num_epochs',choices=[5,10,20],default=5,type=int,help='Number of epochs.')
  6. parser.add_argument("--num_layers", type=int, required=True, help="Network depth.")

  7. args=parser.parse_args()
  8. print(args)
  9. print(args.toy,args.num_epochs,args.num_layers)
复制代码

命令行执行效果:

  1. >python demo.py --num_epochs 10 --num_layers 10
  2. Namespace(num_epochs=10, num_layers=10, toy=False)
  3. False 10 10
复制代码

2.1.基本使用

  1. parser.add_argument('--toy','-t',action='store_true',help='Use only 50K samples of data')
复制代码

--toy:为参数名称;
-t:为参数别称;
action='store_true':参数是否使用,如果使用则为True,否则为False。

  1. >python demo.py -t --num_epochs 10 --num_layers 10
  2. Namespace(num_epochs=10, num_layers=10, toy=True)
  3. True 10 10 # 对比和上次执行的区别
复制代码

help:参数说明



2.2.相关参数

实例1

  1. parser.add_argument('--num_epochs',choices=[5,10,20],default=5,type=int,help='Number of epochs.')
复制代码

choices:候选值,输出参数必须在候选值里面,否如会出现下面的结果:

  1. >python demo.py -t --num_epochs 30 --num_layers 10
  2. usage: demo.py [-h] [--toy] [--num_epochs {5,10,20}] --num_layers NUM_LAYERS
  3. demo.py: error: argument --num_epochs: invalid choice: 30 (choose from 5, 10, 20)
复制代码

default:默认值,如果不输入参数,则使用该默认值

  1. >python demo.py -t  --num_layers 10
  2. Namespace(num_epochs=5, num_layers=10, toy=True)
  3. True 5 10
复制代码


实例2

  1. parser.add_argument("--num_layers", type=int, required=True, help="Network depth.")
复制代码

required:为必选参数,如果不输入,则出现以下错误:

  1. >python demo.py -t --num_epochs 10
  2. usage: demo.py [-h] [--toy] [--num_epochs {5,10,20}] --num_layers NUM_LAYERS
  3. demo.py: error: the following arguments are required: --num_layers
复制代码

实例3
-h:输出参数使用说明信息

  1. >python demo.py -h
  2. usage: demo.py [-h] [--toy] [--num_epochs {5,10,20}] --num_layers NUM_LAYERS

  3. A description of what the program does

  4. optional arguments:
  5. -h, --help            show this help message and exit
  6. --toy, -t             Use only 50K samples of data
  7. --num_epochs {5,10,20}
  8.                Number of epochs.
  9. --num_layers NUM_LAYERS
  10.                 Network depth.
复制代码

转载:宽客在线


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python ARS GPA Description Arguments Python argparse模块 解析命令行

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-4 13:45