python | argparse模块基础语法记录

'''
Auther:Qi Guangyuan
Data:2021-12-3
Contact:qiguangyuan0406@163.com
Study Script:Use argparse to pass in command line parameters
'''

import argparse
'''
We call this method as the parameter parser method,and its use can be divided into three basic steps:
1.Instantiate the parameter parser;
2.Add parameter settings;
3.Parse the obtained parameters
'''

parser=argparse.ArgumentParser(description='An argparse example')
# Instantiate the parameter parser.

parser.add_argument('--method','-m',choices=['add','multiple'],help='choose whether to add or multiply')
parser.add_argument('-a',default=1,type=int,help='The first number')
parser.add_argument('-b',default=2,type=int,help='The second number')
'''
Important and commonly used parameters
1.names:the name of the parameter.A required parameter.It is ranked first(if there are multiple names,it will be ranked first).
        Usually,the "short format" or/and the "long format" are used.
        Such as:--version and -v.
2.type:The type to which the command line parameter should be converted,such as bool,int,float,str,etc.
3.default:When the parameter does not appear in the commend line,use this default value.
        Such as:parser.add_argument("-a",default='1')
                If the -a parameter is not passed in the command line,the value if this parameter will be set to 1.
4.action:Specify the action of the parameter,that is how to deal with this parameter.
        There are many actions to choose,which are not used temporarily.
5.choices:This parameter accepts a list as a value,the parameter value is limited to this list.
6.help:The help description for this parameter will be printed in the help file.
'''
args=parser.parse_args()
'''
Use the ArgumentParser.parse_args method to parse the obtained parameters and return the parsed result.
Then you can use the name specified for each parameter in the settings to get their value.
For example,use args.method to get the value obtained by the --method parameter. 
'''
if args.method == 'add':
    print(args.a+args.b)
else:
    print(args.a*args.b)

'''
Some examples of calls:

>python argparse_syntax.py
2

>python argparse_syntax.py --help
usage: argparse_syntax.py [-h] [--method {add,multiple}] [-a A] [-b B]

An argparse example

optional arguments:
  -h, --help            show this help message and exit
  --method {add,multiple}, -m {add,multiple}
                        choose whether to add or multiply
  -a A                  The first number
  -b B                  The second number
  
python argparse_syntax.py --method add -a 10 -b 20
30
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值