前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 中argparse模块的使用

Python 中argparse模块的使用

作者头像
致Great
发布2018-08-02 14:26:04
8490
发布2018-08-02 14:26:04
举报
文章被收录于专栏:程序生活

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

1 sys.argv

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

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

命令行执行效果:

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

2 argparse

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

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

args=parser.parse_args()
print(args)
print(args.toy,args.num_epochs,args.num_layers)

命令行执行效果:

代码语言:javascript
复制
>python demo.py --num_epochs 10 --num_layers 10
Namespace(num_epochs=10, num_layers=10, toy=False)
False 10 10

2.1 基本使用

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

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

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

help:参数说明

2.2 相关参数

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

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

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

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

代码语言:javascript
复制
>python demo.py -t  --num_layers 10
Namespace(num_epochs=5, num_layers=10, toy=True)
True 5 10

int:参数类型

  • 实例2
代码语言:javascript
复制
parser.add_argument("--num_layers", type=int, required=True, help="Network depth.")

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

代码语言:javascript
复制
>python demo.py -t --num_epochs 10
usage: demo.py [-h] [--toy] [--num_epochs {5,10,20}] --num_layers NUM_LAYERS
demo.py: error: the following arguments are required: --num_layers
  • 实例3 -h:输出参数使用说明信息
代码语言:javascript
复制
>python demo.py -h
usage: demo.py [-h] [--toy] [--num_epochs {5,10,20}] --num_layers NUM_LAYERS

A description of what the program does

optional arguments:
  -h, --help            show this help message and exit
  --toy, -t             Use only 50K samples of data
  --num_epochs {5,10,20}
                        Number of epochs.
  --num_layers NUM_LAYERS
                        Network depth.
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.07.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 sys.argv
  • 2 argparse
    • 2.1 基本使用
      • 2.2 相关参数
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档