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": #判断指令名
            cp.opt("-ts1",OPT.Must).opt("-opt",OPT.Not).opt(["-j","--j"],OPT.Try)
            pr=cp.parse(pr)
            #根据指令名设定选项,再次解析
            #可以在这里处理指令
  • 解析到指令,但指令前缀和定义不匹配:
    @Events.onWrongCmdType
    def wrongType(pr:ParseResult,cp:CommandParser):
        #可以在这里处理错误
        pass

额外地,可以比较文本和各指令间的相似度

    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.2.tar.gz (22.2 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: LSparser-0.1.2.tar.gz
  • Upload date:
  • Size: 22.2 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.2.tar.gz
Algorithm Hash digest
SHA256 485cf5a04f0e1820f57822c6ec2fdaf1254e8e2fe8b9e289ef1be8b5ba2978aa
MD5 425323b45364ef1764274ab15d0e09a5
BLAKE2b-256 7d61bcadd12d651229a451c94e23ccc008e0f2b99f93f5257e7df74a98cb2d4b

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