Skip to main content

A command parser which breaks down a "command" into name, params, short & long options

Project description

LSparser

pypi_badge

一个基于Python的指令解析器(大概)

解析一定格式的指令,形如:

    .cmd param1 param2 -s1 -s2 s2val --l lval1 lval2 lval3

得到

    type: "."
    command: "cmd"
    params: ["param1", "param2"]
    s1: True
    s2: "s2val"
    l: ["lval1", "lval2", "lval3"]

安装

    pip install LSparser

指令格式

  • type/指令前缀:一个字符,用于分辩文本是否为指令

    默认为[".", "。", "-", "!", "!", "/"]

  • command/指令名:指令名称

  • params/参数:提供给指令的参数

  • short options/短选项:默认以"-"为前缀的选项,可以有至多一个选项值

  • long options/长选项:默认以"--"为前缀的选项,可以有多个选项值

一些规则:

  • 指令名、参数、选项、选项值之间通过" "分隔
  • 可以事先设定哪些选项可被解析,以及这些选项后是否有选项值
  • 没有附带选项值但出现在指令中的选项,其选项值当做True处理

使用方法

引入库

    from LSparser import *

创建指令模板,提供帮助文本,并设定要被解析的选项

    c=Command("cmd")
    c.help("这是名为cmd的指令")
    c.opt("-s1",OPT.Try,"短选项s1").opt("-s2",OPT.Must,"s2").opt("--l",OPT.Must,"还有长选项l")

可以为指令添加别名、指令前缀

    c.names("cmd1")
    c.types("$","¥")

添加回调函数

    @Events.onCmd("cmd")
    def cmdfunc(pr:ParseResult):
        # result 为 ParseResult 对象
        print("一个回调函数")
        print(pr) #解析结果储存在其中
        return "返回值"

解析指令

    cmd=".cmd param1 param2 -s1 -s2 s2val --l lval1 lval2 lval3"
    cp=CommandParser()
    pr:ParseResult=cp.tryParse(cmd)
    print("args:",pr.args) #选项
    print("output: ",pr.output) #回调函数执行结果
    # => ["返回值"]

进阶地,指令解析后各种情况

  • 未解析到指令:
    #根本不是指令
    @Events.onNotCmd
    def notCmd(pr:ParseResult,cp:CommandParser):
        pass
  • 解析到未定义指令:
    @Events.onUndefinedCmd
    def cmdUndefined(pr:ParseResult,cp:CommandParser):
        if pr.command=="test": #判断指令名
            pr.opt("-ts1",OPT.Must).opt("-opt",OPT.Not).opt(["-j","--j"],OPT.Try)
            pr.parse()
            #根据指令名设定选项,再次解析
            #可以在这里处理指令
  • 解析到指令,但指令前缀和定义不匹配:
    @Events.onWrongCmdType
    def wrongType(pr:ParseResult,cp:CommandParser):
        if pr.type=="$":
            #可以在这里处理错误
            pass

一些额外内容

  • 更多事件
    @Events.onCmd.error("cmd")
    @Events.onBeforeParse
    @Events.onAfterParse
    @Events.onExecuteError
    ...
  • 解析时提供额外数据
    Command("extra")
    @Events.onCmd("extra")
    def extraFunc(pr:ParseResult):
        print(pr.dataArgs) #=> ('R', 'P', 'K')
        print(pr.dataKW)   #=> {'atk': 3000, 'def': 2500}

    cp.tryParse(".extra",*["R","P","K"],**{"atk":3000,"def":2500})
  • 异步支持
    async def ainit():
        Command("async")
        @Events.onCmd("async")
        async def asyncFunc(pr:ParseResult):
            pass
        pr=await cp.asyncTryParse(".async")
  • 比较文本和各指令间的相似度
    similist=cp.core.getCmdSimilarity("zmd",top=5)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

LSparser-0.1.6.tar.gz (27.4 kB view details)

Uploaded Source

File details

Details for the file LSparser-0.1.6.tar.gz.

File metadata

  • Download URL: LSparser-0.1.6.tar.gz
  • Upload date:
  • Size: 27.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.26.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

File hashes

Hashes for LSparser-0.1.6.tar.gz
Algorithm Hash digest
SHA256 259df048dcbcb572429ed09d47083cbad01a44e8cec32b54249bfd6e8b040424
MD5 cb292483107d1fed0949e65d25fc8186
BLAKE2b-256 35207a62a16b7d6ae3c1b45a5f8228cb1261b7b34a3c6d374cb596bf278e750f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page