python的命令行参数解析库
argument的Python项目详细描述
python开发人员的参数解析
用法:
importargumentf=argument.Arguments()#Requried arguments, first argument will be stored as "candy"f.require("candy",help="Candy name")#optional unnamed valuef.maybe("soda")#optional value, set a default, can be changed by adding: --num=30, or -n=30f.option("num",25,help="How many pieces?",abbr="n")#add a switch, a flag with no argumentf.switch("reverse",help="Reverse ordering",abbr="r")f.switch("unwrap",help="unwrapcandy",abbr="u")#Process data before saving itf.process("candy",lambdax:x.upper())#Parse num as integerf.process("num",lambdax:int(x))f.validate("num",lambdax:x>10)#get dataarguments,errors=f.parse()
示例:
python tests/demo.py bubblegum
{‘num’: 25, ‘soda’: None, ‘reverse’: False, ‘candy’: ‘BUBBLEGUM’, ‘unwrap’: False}
python demo.py bubblegum cubacola
{‘num’: 25, ‘soda’: ‘cubacola’, ‘reverse’: False, ‘candy’: ‘BUBBLEGUM’, ‘unwrap’: False}
python tests/demo.py bubblegum -r -n=123 –unwrap
{‘num’: 123, ‘soda’: None, ‘reverse’: True, ‘candy’: ‘BUBBLEGUM’, ‘unwrap’: True}
python tests/demo.py bubblegum –n=5
{‘num’: 5, ‘soda’: None, ‘reverse’: False, ‘candy’: ‘BUBBLEGUM’, ‘unwrap’: False}
自动生成的帮助:
print(f)``用法:demo.py[选项]candy
必需参数:candy candy name
可选参数:soda不适用
选项:-n–num=25多少件?
开关:-r–反向排序-u–展开展开展开```