study.py内容如下
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
import argparse
def argparseFunc():
'''
基于argparse模块实现命令参数解析功能
执行示例:
python study.py -i 172.19.7.236 -p 8080 -a -r
python study.py --ip 172.19.7.236 --port 7077 --auth -w -v True
'''
parser = argparse.ArgumentParser(description="study.py usage help document")
# 添加不带默认值的可解析参数
parser.add_argument("-i", "--ip", help="ip addr") #注意: -h、--help为内置参数,不可用
parser.add_argument("-p", "--port",help="host port")
# 添加带默认值的可解析参数(# action = store_true 表示是如果使用了这个参数,则值参数值设置为True # 更多action配置可参考源码
# 需要注意的是,不能为带默认值参数指定参数值,会报错,该参数值会被当作不识别的参数
parser.add_argument("-a", "--auth", help="if auth need", action="store_true")
# 添加互斥参数(比如 例中的-r和-w 同时只能用一个)
exclusive_group = parser.add_mutually_exclusive_group()
exclusive_group.add_argument("-r","--read", help="read enabled" , action="store_true")
exclusive_group.add_a