Coder Social home page Coder Social logo

arcletproject / alconna Goto Github PK

View Code? Open in Web Editor NEW
71.0 1.0 6.0 1.81 MB

A High-performance, Generality, Humane Python CLI Arguments Parser Library. 一个直观的、高性能、泛用的Python命令行参数解析器集成库

License: MIT License

Python 100.00%
python command cli argparse parser

alconna's Issues

[Feature] 命令组

命令组允许合并多个命令头相同的命令,或在设置命令组根命令后合并任意多个命令

[Feature] PatternValidator

验证器负责在参数验证的最后阶段调用,即可在类型转换后再进行验证

对于内置类型但又希望增加验证器的,可以提供一个函数:bind,或使用typing.Annotated来进行

[Feature]Option Behavior

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
增加命令选项之间的行为
如:

  • 选项互斥
  • 选项默认值

[Feature] 半自动补全

请确认:

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

命令补全可提供如下方式触发:

  1. 通过内置新选项"--comp"发送提醒
  2. 通过命令开启"completable", 并在.parse时传入一个session(或使用global session),mgr会存储可能的补全状态

要求Args提供可能的CompletionSupplier, 生成补全信息(大概交给ArgsField)

是否已有相关实现

暂无

其他内容

暂无

[Feature] 选择Alconna解析模式

有序:
Option("--test", Args["foo":"bar", "aaa":int:234], disorder=False) # default False
--test bar 666
--test 123 barX

无序:
Option("--test", Args["foo":"bar", "aaa":int:234], disorder=True)
--test bar 666
--test 123 bar

[Feature]support subcmd in firelike

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
当类有内部类时,该内部类解析为subcmd

是否已有相关实现

from arclet.alconna import AlconnaFire

class Test:

    def __init__(self, user):
        self.user = user
    
    class Sub:
        def subopt(self, name):
            print(name)

alc = AlconnaFire(Test)
alc.parse("Test RF Sub subopt Hello")

其他内容
处理方式基本与外部类相同

[Bug]Option的alias并没有自动解析到对应的name上

检查

  • [✅] 问题的标题明确
  • [✅] 我翻阅过其他的issue并且找不到类似的问题
  • [✅] 我已经阅读了文档 the wiki 并仍然认为这是一个Bug

Bug

问题
Option在设置alias之后,analyse_message取得的Arpamar结果中取该Option本身的name结果为空,取alias本身时正确。

如何复现
参考控制台日志

预期行为

cmd_test.analyse_message('/test -h').help
Ellipsis

使用环境:

  • Python 版本: 3.10.1

日志/截图

cmd_test = Alconna(
... command='/test',
... options=[
... Option('-help', alias='-h').help('获取帮助'),
... ],
... )
cmd_test.analyse_message('/test -h').h
Ellipsis
cmd_test.analyse_message('/test -h').help
(无输出)

[Feature] 1.4 TODO

请确认:

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

  1. 重构字符串分割系统
  2. 重构 Args 与 MultiArg部分
  3. 允许 Subcommand 嵌套
  4. Arpamar 功能增加:
arp.query_args("$main”)
arp.query_value("foo")
  1. Arpamar source与origin 弱链接(通过 manager)

[Feature]AlconnaFire

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
Fire-Like形式(即通过传入function、class等)构造Alconna.

是否已有相关实现

[Feature]异步支持

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
当action为Coroutine时依然能够执行

其他内容
可能的方案:

  1. Alc内置loop, 异步以loop_create_task实现
  2. 现有全部sync部分复制为async实现

[Feature]添加Click-Like构造方式

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
用类似于Click构造command的方式构造一个Alconna

是否已有相关实现

command_decorate = AlconnaDecorator()
@command_decorate()
@command_decorate.option("--foo", Args["bar":str])
def test(bar: str):
    ...

其他内容
1.该方式不支持传入多类命令头
2.该方式装饰的函数暂定为main_args_action
3.该方式或许有其他更简易的相关实现

[Feature] 明确测试脚本

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

在项目中添加benchmark.py以供参考

其他内容

文档 中没有指出使用哪个脚本进行性能测试,希望可以在仓库中添加一个benchmark.py作为参考

[Feature] v2 design

请确认:

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

2.0版本下,可能将抛弃OptionSubcommandArgs概念

新概念 Node:

  • 是解析命令的最小单位。

  • 应当是两个sep之间的字符串或者两个字符串之间的非字符单元。

  • 可以选择先验匹配(提交几类固定值,解析器直接判断)或后验匹配(使用nepattern,解析器传入后分析解析结果,类似 Arg)。

新概念 Scope:

  • 由一到多个 Node 或 Scope 组成

  • 可约束 Node 之间的 sep, 或继承其上层 Scope 的配置

  • Node 分配其子 Node 时自动以自身的 dest 创建一个 Scope, 并让子节点加入

  • 同一个 Scope 下不允许存在名字相同的两个节点 ( 同 dest 不影响)

[OSPP] Alconna 2.0 版本设计与开发

描述

Alconna 1.x 版本存在诸多设计问题和历史遗留问题,这些问题需要在 2.0 版本中进行集中解决。仓库主分支目前已在2.0-alpha2阶段,清理了部分冗余功能并且提出了一些2.0设计。此项目需要基于 Alconna 1.x 的基础设计进行 2.0 版本的项目设计与开发,并且需要将 Alconna 的上游基础组件库一并进行优化与设计迭代,这将涉及到对文本解析算法、系统架构、文字样式排版、语言底层原理的大量工作。

项目产出要求

  1. Alconna 2.0 主体架构

    • 全新的或改善过的命令体构造方式,并对开发者友好
    • 对模糊匹配、命令补全、消息帮助等功能与解析器/执行器进行解耦合,抽象为可拓展组件最佳
    • 优化解析/执行逻辑,使得解析过程是具状态的,状态可复制的,控制流可移交的流程
    • 减少代码复杂度、增加可读性
    • 命令配置项读取方法更新
    • 配套的测试文件更新2. Alconna 基础组件库更新
  2. 类型验证&类型转换组件库的性能优化,可使用cython或pyo3等方式

  3. 字符串分割组件的优化,例如使其可识别全角引号、识别自定义特殊符号等

  4. 更新样式组件库,可提供更多样式

  5. Alconna 2.0 外部接口,需要总结 Alconna 1.x 版本现有的外部衍生项目,抽象成统一的对外功能接口,并尽可能提供具体实现

项目技术要求

  1. 完善的python 基础,熟悉 python 语法特性,掌握装饰器、描述器、生成器等特性用法,了解python底层原理,对性能优化有一定知识
  2. C语言基础,了解 python 的 C-API 用法,了解 Cython 工作流
  3. 结构设计能力与抽象能力,能够系统性策划该架构各部分的功能

[Feature]Arg的choice

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
Args的value处接受一个Iterable实例时, 会将其作为choice对象。解析时对应参数只能为choice内的其中一个参数,否则报错

是否已有相关实现

Args["part":["abc", "def"]]

其他内容
内部暂且不作其他判断

[Feature]Enhance Arpamar

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
即强化自动补全,目前想法为:

  • 扫描一遍命令内的所有Args并动态创建Arpamar
  • 扫描所有option与subcommand,将name写入Arpamar中

[Bug] 开启报错后 GraiaDispatcher 报"非法元素"

请确认:

  • 问题的标题明确
  • 我翻阅过其他的issue并且找不到类似的问题
  • 我已经阅读了文档 the wiki 并仍然认为这是一个Bug

Bug

问题

当消息传入时会在处理消息阶段报错

预期行为

对于过滤元素的报错消息应当忽略

使用环境:

  • Python 版本: 3.9
  • Alconna 版本: 0.8.1

日志/截图

image

image

[Feature]api改进提议-No.2

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

  1. 提供更简单的构造方式:
class Alc:
    main = Args[...]
    opt1 = Option(...)
    sub = Subcommand(...)
delegate(Alc).parse(...)
  1. ArgPattern提供简易的构造方法,例如
def my_converter(text:str):
    return int(text)
with convert(my_converter, r"\d+") as int_:
    args = Args["foo":int_]

或者是

@pattern("int_", r"\d+")
def my_converter(text:str):
    return int(text)
...
args = Args["foo":"int_"]
  1. 可变参数提供长度限制,如
args = Args["tag[3]":str]
  1. 接入权限接口

[Feature]AlconnaMessageProxy

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
负责提供一个模版类,包括消息的获取规范与解析结果的处理规范

是否已有相关实现
main.py:

amp = DefaultAlconnaMessageProcessor(loop=loop)
alc = Alconna("command", main_args="foo:str").callback(lambda foo:foo)
amp.run()

AMP.py:

class AbstractAlconnaMessageProcessor:
    async def fetch_message():
        ...
    async def execute_handler():
        ...

其他内容
该内容随时可能被驳回

[Feature] Raw-All-Command-Help

请确认:

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

命令管理器的all_command_help可以返回原始的帮助数据而非排版好的帮助信息

是否已有相关实现

暂无

其他内容

暂无

[Feature] 主动隐藏命令

请确认:

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

命令创建时可以对命令管理器主动隐藏,从而在获取所有命令帮助时不显示

是否已有相关实现

暂无

其他内容

暂无

[Feature]FireConfig

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
类似pydantic,提供固定接口类"Config", 当Firelike解析到这个类时会优先以该类的内部类变量进行构造

大致提供如下:

  • namespace: 命名空间
  • headers: 别名
  • raise_exception: 是否报错
  • help_text: 帮助信息
  • extra: 当出现未知类型时按原类型处理还是转为AnyParam

[Feature]api优化意见及建议-No.1

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
以下会作为0.6.5-0.7的更改内容

  1. 解析器api部分细化, Args- Analyse按情况需要进行可拓展化
  2. 几类特殊构造方式独立出去,而不是作为类方法
  3. Subcommand取消*option,而是传入Iterable[Option]
  4. help与separate方法内移进init里

and more……

[Feature]json(Dict)相关操作

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
1.from_dict()与to_dict()

这两个方法可用于pickle保存Alconna、从json生成Alconna等功能

2.commandline::analyse 与 create --analysed

analyse命令可以将传入的cmdstring转为一命令结构,create --analysed则会尝试获取之前analyse获取的命令结构并转为Alconna结构

[Feature]Enhance ArgPattern

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
将非文字类型参数也作为ArgPattern内的pattern参数, 必要时还可以使其导入ObjectPattern.

其他内容
针对此种情况,PatternToken的
Direct -> 不验证类型
Match —> 验证类型
Transform —> 验证且转换为另一类型

[Feature]优化rematch

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
1.re.pattern由ArgPattern生成
2.对于(.+), 考虑同AnyParam一样针对,即不经过re匹配
3.命令头部分也应存在一个pattern以防止大量重复构建pattern

是否已有相关实现

ArgPattern:
    re_pattern: Pattern
    
    def refind(self, mayarg: str):
        return self.re_pattern.findall(mayarg)

[Feature]Help String Enhance

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

  1. 提供更多的help文档格式

目前已有的格式适用于bot交互,
之后应提供适用于cli的help格式
形如:

TestCommand
Usage:
    TestCommand <subcommand> [options]

Subcommand:
    sub1    ……
    sub2    ……
  1. help提取作为一个单独的模块, 以拓展其行为:
    增加HelpText类, 其提供参数:
  • parameters: 当前的参数列, 由Args提供
  • description: 当前说明, 由help_text提供
  • tips: 辅助说明
  • example: 使用例

[Feature] ActionBehavior

自0.9.0开始,因为默认引入缓存机制,参数动作(ArgAction)会因为原先的解析时运行只会在某个命令首次解析时执行。

因此,考虑将Action的执行行为全权交给一个内置的Behavior

该Behavior的实现需要满足如下要求:

  1. Arpamar 可以访问原 Alconna
  2. Alconna 会在解析器内被生成一个action映射表,表示该Alconna内所有含Action的组件
  3. Arpamar 能够更便捷的访问选项与选项参数

注:特殊的两个选项helpshortcut不受此影响

0.5.5的default是换写法了吗?匹配一直是False。Python 3.10.1

0.5.5

test = Alconna(command='/test', main_args=Args(num=int).default(num=1))
test.analyse_message('/test 10')
matched=True head_matched=True error_data=[] _options={} _other_args={} _main_args={'num': 10}
test.analyse_message('/test')
matched=False head_matched=True error_data=[''] _options={} _other_args={} _main_args={}

0.5.3

test = Alconna(command='/test', main_args=Args(num=int).default(num=1))
test.analyse_message('/test 10')
current_index=1 is_str=True results={'main_args': {'num': 10}} raw_data={} need_main_args=True matched=True head_matched=True _options={} _args={}
test.analyse_message('/test')
current_index=1 is_str=True results={'main_args': {'num': 1}} raw_data={} need_main_args=True matched=True head_matched=True _options={} _args={}

[Feature] ArgsField

请确认:

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

Args的"default"可以添加一个 Field (like dataclasses.field), 主要有aliasdefaultdefault_factory

是否已有相关实现

暂无

其他内容

暂无

[Bug] 改进 `-h`

请确认:

  • 问题的标题明确
  • 我翻阅过其他的issue并且找不到类似的问题
  • 我已经阅读了文档 the wiki 并仍然认为这是一个Bug

Bug

问题

-h 作为 --help 预设位,picupload -w 200 -h 200 此类命令不会按直觉解析,应该给予 Exception 或 提示 避免困惑。

如何复现

    Option("--width|-wi", Args['width', int], help_text="设置宽度"),
    Option("--height|-he", Args['height', int], help_text="设置高度"),

预期行为

使用环境:

  • Python 版本: 3.9
  • Alconna 版本: 1.7.34

[Feature]改进help部分

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
help_string存入TemplateCommand中,
get_help会调用各部分的help生成器

[Feature] Arpamar 改进

在此前版本中,Arpamar 仅作为解析结果载体,无法很好的与其他组件互通

为此,Arpamar 或许会进行如下调整:

  1. 添加origin与source,代表传入的原消息与 Alconna
  2. 添加get_duplication
  3. 与BehaviorInterface合并
  4. 添加以传入Type返回Dict[str, Type]的方法

[Feature]Args反向匹配

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
反向匹配中传入的正则或者类型在匹配成功时认为解析失败, 即用来排除特定参数

是否已有相关实现

Args["!path":".+/.+\.apk"]

[Feature]shortcut功能

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
可用于设置快捷指令
即将一段字符串作为一个复杂命令的快捷方式

是否已有相关实现

github = Alconna(command="#github",options=[...])
cmd = "#github RimoChan repo yinglish"
github.shortcut("ying语仓库", cmd)
github.analyse_message("ying语仓库")  # eq. github.analyse_message(cmd)

其他内容
1.shortcut有三个形参:

  • shortname: 快捷指令名称
  • command: 对应的指令
  • reserve_args: 是否仍需要接受参数, 默认为False

[Feature]Format改进

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要

  1. 当key值不存在于format_list时,其作为AnyParam处理
  2. 主参数修正

[Feature]Alconna 命令行功能

检查

  • 新特性的目的明确
  • 我已经阅读了文档 the wiki 并且找不到类似特性

Feature

概要
帮助用户创建一个Alconna

是否已有相关实现
image
其他内容
该功能应该由Alconna本体实现(

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.