A High-performance, Generality, Humane Command Line Arguments Parser Library.
Project description
Alconna
About
Alconna
is a powerful cli tool for parsing message chain or other raw message data. It is an overload version of CommandAnalysis
, affiliated to ArcletProject
.
Alconna
has a large number of built-in components and complex parsing functions. But do not afraid, you can use it as a simple command parser.
Installation
pip
$ pip install --upgrade arclet-alconna
$ pip install --upgrade arclet-alconna[full]
$ pip install --upgrade arclet-alconna[all]
Documentation
Official Document : 👉Link
Relevant Document : 📚Docs
A Simple Example
from arclet.alconna import Alconna, Option, Subcommand, Args
cmd = Alconna(
"/pip",
Subcommand("install", Option("-u|--upgrade"), Args.pak_name[str]),
Option("list")
)
result = cmd.parse("/pip install numpy --upgrade") # This method returns an 'Arparma' class instance.
print(result.query('install')) # Or result.install
Output as follows:
value=None args={'pak_name': 'numpy'} options={'upgrade': value=Ellipsis args={}} subcommands={}
Communication
QQ Group: Link
Features
- High Performance. On i5-10210U, performance is about
71000~289000 msg/s
; test script: benchmark - Powerful Automatic Type Parse and Conversion
- Customizable Help Text Formatter and Control of Command Analyser
- i18n Support
- Cache of input command for quick response of repeated command
- Easy-to-use Construct and Usage of Command Shortcut
- Can bind callback function to execute after command parsing
- Can create command completion session to implement multi-round continuous completion prompt
- Various Features (FuzzyMatch, Output Capture, etc.)
Example of Callback Executor:
# callback.py
from arclet.alconna import Alconna, Args
alc = Alconna("test", Args["foo", int]["bar", str])
@alc.bind()
def callback(foo: int, bar: str):
print(f"foo: {foo}")
print(f"bar: {bar}")
print(bar * foo)
if __name__ == "__main__":
alc()
$ python callback.py test 3 hello
foo: 3
bar: hello
hellohellohello
Example of Type Conversion:
from arclet.alconna import Alconna, Args
from pathlib import Path
read = Alconna(
"read", Args["data", bytes],
action=lambda data: print(type(data))
)
read.parse(["read", b'hello'])
read.parse("read test_fire.py")
read.parse(["read", Path("test_fire.py")])
'''
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
'''
Example of Command Shortcut:
# shortcut.py
from arclet.alconna import Alconna, Args
alc = Alconna("eval", Args["content", str], action=lambda x: eval(x, {}, {}))
alc.shortcut("echo", {"command": "eval print(\\'{*}\\')"})
if __name__ == "__main__":
alc()
$ python shortcut.py eval print(\"hello world\")
hello world
$ python shortcut.py echo hello world!
hello world!
Example of Command Completion:
# completion.py
from arclet.alconna import Alconna, Args, Option
alc = Alconna("test", Args["bar", int]) + Option("foo") + Option("fool")
if __name__ == "__main__":
alc.completion()
$ python completion.py test ?
next input maybe:
> foo
> int
> -h
> --help
> fool
Example of typing
Support:
from typing import Annotated # or typing_extensions.Annotated
from arclet.alconna import Alconna, Args
alc = Alconna("test", Args.foo[Annotated[int, lambda x: x % 2 == 0]])
alc.parse("test 2")
alc.parse("test 3")
'''
'foo': 2
ParamsUnmatched: param 3 is incorrect
'''
Example of FuzzyMatch:
# fuzzy.py
from arclet.alconna import Alconna, CommandMeta, Arg
alc = Alconna('!test_fuzzy', Arg("foo", str), meta=CommandMeta(fuzzy_match=True))
if __name__ == "__main__":
alc()
$ python fuzzy.py /test_fuzzy foo bar
/test_fuzy not matched. Are you mean "!test_fuzzy"?
License
Alconna is licensed under the MIT License.
Acknowledgement
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for arclet_alconna-1.7.0rc4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b0758d4aac909d2ee0d4b1002ebdee353b630980f3ba536e8f1e6c1f1e7b91f |
|
MD5 | 405258d8efb30b575774faa595971177 |
|
BLAKE2b-256 | ff4bcb2d5af7a39c4a4cb4b2cb26580fe5b6dd2b48b1f7f0f19cf0941a2497f9 |