argparse 总结

从简单的入手

# myfile.py
import argparse
parser = argparse.ArgumentParser() # 初始化
args = parser.parse_arg() # 只有-h, -help两个参数

输出:

# 命令行输入
python myfile.py --help  
# 输出
usage:myfile.py [-h]

optional arguments:
-h, --help  show this help message and exit

开始加东西

# myfile.py
import argparse
parser = argparse.ArgumentParser(description='Myfile.py is a testing file') 


# grop 是互斥组,也即里面添加的参数最多只能出现一个
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--network-path', metavar='NETWORK', help="pretrained network or network path (destination where network is saved)")
# metavar参数 - 在 usage 说明中的参数名称,对于必选参数默认就是参数名称,对于可选参数默认是全大写的参数名称
# help参数 - 在输入-h或者--help时会显示的这个--network-path的帮助信息
group.add_argument('--network-offtheshelf', metavar='NETWORK',
help="off-the-shelf network")


# default, 当没有给出参数时,default为默认的参数
parser.add_argument('--datasets', metavar='DATASETS', default='oxford5k,paris6k', help="comma separated list of test datasets"# GPU ID,默认是 0
# ‘-g’ 短参数
parser.add_argument('--gpu-id', '-g', default='0', metavar='N', help="gpu id used for testing (default: '0')")

args = parser.parse_arg() 

来看看输出:

# 命令行输入
python myfile.py --help

# 输出
# 注意看这里group组添加的参数中是最多只能选一个的模样
usage: myfile.py [-h](--network-path NETWORK | --network-offtheshelf NETWORK)[--dataset DATASET][--gpu-id N]

Myfile.py is a testing file

optional arguments:
-h, --help  show this help message and exit
--network-path NETWORK 
			pretrained network or network path (destination 		      			where network is save)
--network-offtheshelf NETWORK
			off-the-shelf network
--datasets DATASETS
			comma separated list of test datasets
# 注意这里有个短参数的用法
--gup-id N,-g N        gpu id used for testing (default: '0')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值