笨办法学python(续)1--argparse

Lear more python the hard way

argparse库学习,可以参考该文章

import  argparse

parser = argparse.ArgumentParser(description='this is a desc for the program')

#argparse将会自动生成-h, --help
#必选参数num,类型num, 帮助描述信息
parser.add_argument('num', type=int, help='this value for test')

#可选参数turn-on, 出现则为True,否则为假
parser.add_argument('-t', '--turn-on', action='store_true', help='turn on the operation')

#可选值及默认值
parser.add_argument('-c', type=int, choices=[0, 1, 2], default=1)
args = parser.parse_args()
print(args)


#运行结果
# (test) D:\pycode>python test.py 2 -t
# Namespace(c=1, num=2, turn_on=True)

# (test) D:\pycode>python test.py -h
# usage: test.py [-h] [-t] [-c {0,1,2}] num

# this is a desc for the program

# positional arguments:
#   num            this value for test

# optional arguments:
#   -h, --help     show this help message and exit
#   -t, --turn-on  turn on the operation
#   -c {0,1,2}

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值