[Tips] argparse模块使用

当python程序需要命令行参数时,如果输入参数不复杂可以直接使用`sys.args`模块。如果比较复杂可以使用argparse模块。import argparse 

# description参数可以用于描述脚本的参数作用,默认为空
parser=argparse.ArgumentParser(description="111")
parser.add_argument('--aa','-t',action='store_true',help='sample of aa')
parser.add_argument('--bb', choices=['windows','linux'], default='windows') # 如果choice中的元素类型不是字符串类型,则要指定type
parser.add_argument('--cc',choices=[1,2,3,4,5],default=5,type=int,help='sample of bb') 
parser.add_argument("--dd", type=int, required=True, help="sample of cc")
args=parser.parse_args()
print(args.aa)

  

猜你喜欢

转载自www.cnblogs.com/immortalBlog/p/12641170.html
今日推荐