用于解析命令行参数的python库

blinkparse的Python项目详细描述


闪烁分析

用于解析命令行参数的python库

安装

pip install blinkparse

使用

Parser(args=[], commands=[], description='', commandRequired=False, noOperands=False)

  • 返回blinkparse.Arguments对象
  • argsblinkparse.Argument对象的列表
  • commandsblinkparse.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)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JNI和取消异步任务   java整数n作为List<String>的第二个参数传递。子列表(fromIndex,toIndex),但是stacktrace说toIndex在Kotlin中是n+2   如何在Java中正确嵌套多个ArrayList/Maps?   在嵌套条件语句中比较数组中的值时,java List没有打印正确的值   从CRON作业运行java代码   JRE使用jvisualvm检测到java致命错误   java将字符添加到BufferedReader中   带有枚举的java单例与带有双重检查锁定的单例   Java中的编译器构造基类的更改会影响子类的字节码吗?   Java作为Windows服务用户数据路径   java clone():ArrayList。克隆人()我认为它是一个肤浅的复制品   在jfilechooser多文件选择器启用中打开对话框后,java文件将显示所选文件   java在常用的操作方法中使用反射可以吗?   java ReloadableResourceBundleMessageSource。getMessage()不使用我给它的参数