用于解析命令行参数的python库
blinkparse的Python项目详细描述
闪烁分析
用于解析命令行参数的python库
安装
pip install blinkparse
使用
Parser(args=[], commands=[], description='', commandRequired=False, noOperands=False)
- 返回
blinkparse.Arguments
对象 args
是blinkparse.Argument
对象的列表commands
是blinkparse.Command
对象的列表description
是要显示在帮助页顶部的说明commandRequired
是程序是否需要传入命令noOperands
是程序是否允许接受操作数parse(inputArgs=None)
- 返回
blinkparse.Arguments
对象 inputArgs
是要分析的参数列表。默认为sys.argv[1:]
- 返回
Argument(name, shortName=None, takesValue=False, required=False, description=None)
name
是长语法中使用的参数名(--myArg
,--myArg=myValue
)shortName
是长语法中使用的参数名(-a
,-a myValue
)takesValue
是参数是否接受输入(见上文)required
使blinkparse在未传入参数时引发错误description
是要在帮助页中显示的说明
Command(name, args=[], aliases=[])
- 使用
Command('hello', [CommandArgument('person'), CommandArgument('gender', required=False, options=['male', 'female', 'other])], 'h')
$ python3 example.py hello jered
$ python3 example.py hello joe male
name
是命令的名称args
是命令接受的blinkparse.CommandArgument
对象的列表CommandArgument(name, options=None, required=True)
name
是命令参数的名称options
是参数允许的选项列表(默认为所有选项)required
使blinkparse在未传入命令参数时引发错误
aliases
是命令的其他名称的列表
完整的例子
fromblinkparseimport*args=Parser(args=[Argument('save','s',description='Save the program output'),],commands=[Command('hello',[CommandArgument('person'),CommandArgument('gender',required=False,options=['male','female','other'])]),Command('bye',[CommandArgument('person')])],description='''hello.pyA simple demo of blinkparse ''',commandRequired=True).parse()ifargs.command=='hello':output='Hello '+args.commandArgs['person']if'gender'inargs.commandArgs:output+=', you are '+args.commandArgs['gender']else:output='Bye '+args.commandArgs['person']if'save'inargs.args:withopen('hello.txt','w')asoutputFile:outputFile.write(output)else:print(output)
- 项目
标签: