![](https://socialify.git.ci/ArcletProject/Alconna/image?description=1&descriptionEditable=A%20High-performance%2C%20Generality%2C%20Humane%20Command%20Line%20Arguments%20Parser%20Library.&font=Inter&forks=1&issues=1&language=1&logo=https%3A%2F%2Farclet.top%2Fimg%2Farclet.png&name=1&owner=1&pattern=Brick%20Wall&stargazers=1&theme=Auto)
<div align="center">
# Alconna
</div>
![Alconna](https://img.shields.io/badge/Arclet-Alconna-2564c2.svg)
![latest release](https://img.shields.io/github/release/ArcletProject/Alconna)
[![Licence](https://img.shields.io/github/license/ArcletProject/Alconna)](https://github.com/ArcletProject/Alconna/blob/master/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/arclet-alconna)](https://pypi.org/project/arclet-alconna)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/arclet-alconna)](https://www.python.org/)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FArcletProject%2FAlconna.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FArcletProject%2FAlconna?ref=badge_shield)
[**简体中文**](README.md)|[**English**](README-EN.md)
## 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
```shell
$ pip install --upgrade arclet-alconna
$ pip install --upgrade arclet-alconna[full]
```
## Documentation
Official Document : [👉Link](https://arclet.top/docs/tutorial/alconna)
Relevant Document : [📚Docs](https://graiax.cn/guide/message_parser/alconna.html)
## A Simple Example
```python
from arclet.alconna import Alconna, Option, Subcommand, Args
cmd = Alconna(
"/pip",
Subcommand("install", Option("-U|--upgrade"), Args["pak", 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': 'numpy'} options={'upgrade': value=Ellipsis args={}} subcommands={}
```
## Communication
QQ Group: [Link](https://jq.qq.com/?_wv=1027&k=PUPOnCSH)
## Features
* High Performance. On i5-10210U, performance is about `71000~289000 msg/s`; test script: [benchmark](benchmark.py)
* Intuitive way to create command components
* 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:
```python
# callback.py
from arclet.alconna import Alconna, Args
alc = Alconna("callback", 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()
```
```shell
$ python callback.py 3 hello
foo: 3
bar: hello
hellohellohello
```
Example of Type Conversion:
```python
from arclet.alconna import Alconna, Args
from pathlib import Path
read = Alconna("read", Args["data", bytes])
@read.bind()
def cb(data: bytes):
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 Component creation:
```python
# component.py
from arclet.alconna import Alconna, Args, Option, Subcommand, store_true, count, append
alc = Alconna(
"component",
Args["path", str],
Option("--verbose|-v", action=count),
Option("-f", Args["flag", str], compact=True, action=append),
Subcommand("sub", Option("bar", action=store_true, default=False))
)
if __name__ == '__main__':
res = alc()
print(res.query("path"))
print(res.query("verbose.value"))
print(res.query("f.flag"))
print(res.query("sub"))
```
```shell
$ python component.py /home/arclet -vvvv -f1 -f2 -f3 sub bar
/home/arclet
4
['1', '2', '3']
(value=Ellipsis args={} options={'bar': (value=True args={})} subcommands={})
```
Example of Command Shortcut:
```python
# shortcut.py
from arclet.alconna import Alconna, Args
alc = Alconna("eval", Args["content", str])
alc.shortcut("echo", {"command": "eval print(\\'{*}\\')"})
@alc.bind()
def cb(content: str):
eval(content, {}, {})
if __name__ == '__main__':
alc()
```
```shell
$ python shortcut.py eval print(\"hello world\")
hello world
$ python shortcut.py echo hello world!
hello world!
```
Example of Command Completion:
```python
# completion.py
from arclet.alconna import Alconna, Args, Option
alc = Alconna("complete", Args["bar", int]) + Option("foo") + Option("fool")
if __name__ == "__main__":
alc()
```
```shell
$ python completion.py ?
suggest input follows:
* bar: int
* --help
* -h
* foo
* fool
```
Example of `typing` Support:
```python
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:
```python
# 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()
```
```shell
$ python fuzzy.py /test_fuzzy foo bar
/test_fuzy not matched. Are you mean "!test_fuzzy"?
```
## Examples
| Name | File |
|----------------|------------------------------------------|
| Calculate | [calculate.py](./example/calculate.py) |
| Execute | [exec_code.py](./example/exec_code.py) |
| Request Route | [endpoint.py](./example/endpoint.py) |
| Image Search | [img_search.py](./example/img_search.py) |
| PIP | [pip.py](./example/pip.py) |
| Database Query | [exec_sql.py](./example/exec_sql.py) |
## License
Alconna is licensed under the [MIT License](LICENSE).
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FArcletProject%2FAlconna.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FArcletProject%2FAlconna?ref=badge_large)
## Acknowledgement
[JetBrains](https://www.jetbrains.com/): Support Authorize for [PyCharm](https://www.jetbrains.com/pycharm/)<br>
[<img src="https://cdn.jsdelivr.net/gh/Kyomotoi/CDN@master/noting/jetbrains-variant-3.png" width="200"/>](https://www.jetbrains.com/)
Raw data
{
"_id": null,
"home_page": null,
"name": "arclet-alconna",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "command, argparse, fast, alconna, cli, command-line, parsing, optparse",
"author": null,
"author_email": "RF-Tar-Railt <rf_tar_railt@qq.com>",
"download_url": "https://files.pythonhosted.org/packages/9e/8a/9e228e6ca1f550c9c5dab758bdcdff0a132a1e3c1b45beaf8350fdb2e08a/arclet_alconna-1.8.32.tar.gz",
"platform": null,
"description": "![](https://socialify.git.ci/ArcletProject/Alconna/image?description=1&descriptionEditable=A%20High-performance%2C%20Generality%2C%20Humane%20Command%20Line%20Arguments%20Parser%20Library.&font=Inter&forks=1&issues=1&language=1&logo=https%3A%2F%2Farclet.top%2Fimg%2Farclet.png&name=1&owner=1&pattern=Brick%20Wall&stargazers=1&theme=Auto)\n<div align=\"center\"> \n\n# Alconna\n\n</div>\n\n![Alconna](https://img.shields.io/badge/Arclet-Alconna-2564c2.svg)\n![latest release](https://img.shields.io/github/release/ArcletProject/Alconna)\n[![Licence](https://img.shields.io/github/license/ArcletProject/Alconna)](https://github.com/ArcletProject/Alconna/blob/master/LICENSE)\n[![PyPI](https://img.shields.io/pypi/v/arclet-alconna)](https://pypi.org/project/arclet-alconna)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/arclet-alconna)](https://www.python.org/)\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FArcletProject%2FAlconna.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FArcletProject%2FAlconna?ref=badge_shield)\n\n[**\u7b80\u4f53\u4e2d\u6587**](README.md)|[**English**](README-EN.md)\n\n## About\n\n`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`.\n\n`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.\n\n## Installation\n\npip\n```shell\n$ pip install --upgrade arclet-alconna\n$ pip install --upgrade arclet-alconna[full]\n```\n\n## Documentation\n\nOfficial Document : [\ud83d\udc49Link](https://arclet.top/docs/tutorial/alconna)\n\nRelevant Document : [\ud83d\udcdaDocs](https://graiax.cn/guide/message_parser/alconna.html)\n\n## A Simple Example\n\n```python\nfrom arclet.alconna import Alconna, Option, Subcommand, Args\n\ncmd = Alconna(\n \"/pip\",\n Subcommand(\"install\", Option(\"-U|--upgrade\"), Args[\"pak\", str]),\n Option(\"list\")\n)\n\nresult = cmd.parse(\"/pip install numpy --upgrade\") # This method returns an 'Arparma' class instance.\nprint(result.query('install')) # Or result.install\n```\n\nOutput as follows:\n```\nvalue=None args={'pak': 'numpy'} options={'upgrade': value=Ellipsis args={}} subcommands={}\n```\n\n## Communication\n\nQQ Group: [Link](https://jq.qq.com/?_wv=1027&k=PUPOnCSH)\n\n## Features\n\n* High Performance. On i5-10210U, performance is about `71000~289000 msg/s`; test script: [benchmark](benchmark.py)\n* Intuitive way to create command components\n* Powerful Automatic Type Parse and Conversion\n* Customizable Help Text Formatter and Control of Command Analyser\n* i18n Support\n* Cache of input command for quick response of repeated command\n* Easy-to-use Construct and Usage of Command Shortcut\n* Can bind callback function to execute after command parsing\n* Can create command completion session to implement multi-round continuous completion prompt\n* Various Features (FuzzyMatch, Output Capture, etc.)\n\nExample of Callback Executor:\n\n```python\n# callback.py\nfrom arclet.alconna import Alconna, Args\n\nalc = Alconna(\"callback\", Args[\"foo\", int][\"bar\", str])\n\n@alc.bind()\ndef callback(foo: int, bar: str):\n print(f\"foo: {foo}\")\n print(f\"bar: {bar}\")\n print(bar * foo)\n \nif __name__ == \"__main__\":\n alc()\n```\n\n```shell\n$ python callback.py 3 hello\nfoo: 3\nbar: hello\nhellohellohello\n```\n\n\nExample of Type Conversion:\n\n```python\nfrom arclet.alconna import Alconna, Args\nfrom pathlib import Path\n\nread = Alconna(\"read\", Args[\"data\", bytes])\n\n@read.bind()\ndef cb(data: bytes):\n print(type(data))\n\nread.parse([\"read\", b'hello'])\nread.parse(\"read test_fire.py\")\nread.parse([\"read\", Path(\"test_fire.py\")])\n\n'''\n<class 'bytes'>\n<class 'bytes'>\n<class 'bytes'>\n'''\n```\n\nExample of Component creation:\n```python\n# component.py\nfrom arclet.alconna import Alconna, Args, Option, Subcommand, store_true, count, append\n\nalc = Alconna(\n \"component\",\n Args[\"path\", str],\n Option(\"--verbose|-v\", action=count),\n Option(\"-f\", Args[\"flag\", str], compact=True, action=append),\n Subcommand(\"sub\", Option(\"bar\", action=store_true, default=False))\n)\n\nif __name__ == '__main__':\n res = alc()\n print(res.query(\"path\"))\n print(res.query(\"verbose.value\"))\n print(res.query(\"f.flag\"))\n print(res.query(\"sub\"))\n```\n\n```shell\n$ python component.py /home/arclet -vvvv -f1 -f2 -f3 sub bar\n/home/arclet\n4\n['1', '2', '3']\n(value=Ellipsis args={} options={'bar': (value=True args={})} subcommands={})\n```\n\nExample of Command Shortcut:\n```python\n# shortcut.py\nfrom arclet.alconna import Alconna, Args\n\nalc = Alconna(\"eval\", Args[\"content\", str])\nalc.shortcut(\"echo\", {\"command\": \"eval print(\\\\'{*}\\\\')\"})\n\n@alc.bind()\ndef cb(content: str):\n eval(content, {}, {})\n\nif __name__ == '__main__':\n alc()\n```\n\n```shell\n$ python shortcut.py eval print(\\\"hello world\\\")\nhello world\n$ python shortcut.py echo hello world!\nhello world!\n```\n\nExample of Command Completion:\n```python\n# completion.py\nfrom arclet.alconna import Alconna, Args, Option\n\nalc = Alconna(\"complete\", Args[\"bar\", int]) + Option(\"foo\") + Option(\"fool\")\n\nif __name__ == \"__main__\":\n alc()\n```\n\n```shell\n$ python completion.py ?\nsuggest input follows:\n* bar: int\n* --help\n* -h\n* foo\n* fool\n```\n\nExample of `typing` Support:\n```python\nfrom typing import Annotated # or typing_extensions.Annotated\nfrom arclet.alconna import Alconna, Args\n\nalc = Alconna(\"test\", Args.foo[Annotated[int, lambda x: x % 2 == 0]])\nalc.parse(\"test 2\")\nalc.parse(\"test 3\")\n\n'''\n'foo': 2\nParamsUnmatched: param 3 is incorrect\n'''\n```\n\nExample of FuzzyMatch:\n\n```python\n# fuzzy.py\nfrom arclet.alconna import Alconna, CommandMeta, Arg\n\nalc = Alconna('!test_fuzzy', Arg(\"foo\", str), meta=CommandMeta(fuzzy_match=True))\n\nif __name__ == \"__main__\":\n alc()\n\n```\n\n```shell\n$ python fuzzy.py /test_fuzzy foo bar\n/test_fuzy not matched. Are you mean \"!test_fuzzy\"?\n```\n\n## Examples\n\n| Name | File |\n|----------------|------------------------------------------|\n| Calculate | [calculate.py](./example/calculate.py) |\n| Execute | [exec_code.py](./example/exec_code.py) |\n| Request Route | [endpoint.py](./example/endpoint.py) |\n| Image Search | [img_search.py](./example/img_search.py) |\n| PIP | [pip.py](./example/pip.py) |\n| Database Query | [exec_sql.py](./example/exec_sql.py) |\n\n## License\n\nAlconna is licensed under the [MIT License](LICENSE).\n\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FArcletProject%2FAlconna.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FArcletProject%2FAlconna?ref=badge_large)\n\n## Acknowledgement\n\n[JetBrains](https://www.jetbrains.com/): Support Authorize for [PyCharm](https://www.jetbrains.com/pycharm/)<br>\n[<img src=\"https://cdn.jsdelivr.net/gh/Kyomotoi/CDN@master/noting/jetbrains-variant-3.png\" width=\"200\"/>](https://www.jetbrains.com/)",
"bugtrack_url": null,
"license": "MIT",
"summary": "A High-performance, Generality, Humane Command Line Arguments Parser Library.",
"version": "1.8.32",
"project_urls": {
"documentation": "https://arcletproject.github.io/docs/alconna/tutorial",
"repository": "https://github.com/ArcletProject/Alconna"
},
"split_keywords": [
"command",
" argparse",
" fast",
" alconna",
" cli",
" command-line",
" parsing",
" optparse"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d98ba2343cf25168b49e058ad1c88c21f978685acbc86e8e5e55b8958c2f0796",
"md5": "9dcb687a6e11c26205e7b1931628c25a",
"sha256": "3cd0437d6cf42e4a85290aceffe72264010089af70041f16f78c820549fafa82"
},
"downloads": -1,
"filename": "arclet_alconna-1.8.32-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9dcb687a6e11c26205e7b1931628c25a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 76625,
"upload_time": "2024-11-01T16:33:59",
"upload_time_iso_8601": "2024-11-01T16:33:59.544688Z",
"url": "https://files.pythonhosted.org/packages/d9/8b/a2343cf25168b49e058ad1c88c21f978685acbc86e8e5e55b8958c2f0796/arclet_alconna-1.8.32-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9e8a9e228e6ca1f550c9c5dab758bdcdff0a132a1e3c1b45beaf8350fdb2e08a",
"md5": "986f51e453685a4dea823f1ecd613b2f",
"sha256": "51626e9d5a6e107139adcf060cda3d737c49459f8798bc91dab18817818b8865"
},
"downloads": -1,
"filename": "arclet_alconna-1.8.32.tar.gz",
"has_sig": false,
"md5_digest": "986f51e453685a4dea823f1ecd613b2f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 79812,
"upload_time": "2024-11-01T16:34:00",
"upload_time_iso_8601": "2024-11-01T16:34:00.760545Z",
"url": "https://files.pythonhosted.org/packages/9e/8a/9e228e6ca1f550c9c5dab758bdcdff0a132a1e3c1b45beaf8350fdb2e08a/arclet_alconna-1.8.32.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-01 16:34:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ArcletProject",
"github_project": "Alconna",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "arclet-alconna"
}