Package: arclet-alconna
Alconna简体中文|EnglishAboutAlconnais a powerful cli tool for parsing message chain or other raw message data. It is an overload version ofCommandAnalysis, affiliated toArcletProject.Alconnahas a large number of built-in components and complex parsing functions.But do not afraid, you can use it as a simple command parser.Installationpip$pipinstall--upgradearclet-alconna
$pipinstall--upgradearclet-alconna[full]DocumentationOfficial Document :👉LinkRelevant Document :📚DocsA Simple Examplefromarclet.alconnaimportAlconna,Option,Subcommand,Argscmd=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.installOutput as follows:value=None args={'pak_name': 'numpy'} options={'upgrade': value=Ellipsis args={}} subcommands={}CommunicationQQ Group:LinkFeaturesHigh Performance. On i5-10210U, performance is about71000~289000 msg/s; test script:benchmarkIntuitive way to create command componentsPowerful Automatic Type Parse and ConversionCustomizable Help Text Formatter and Control of Command Analyseri18n SupportCache of input command for quick response of repeated commandEasy-to-use Construct and Usage of Command ShortcutCan bind callback function to execute after command parsingCan create command completion session to implement multi-round continuous completion promptVarious Features (FuzzyMatch, Output Capture, etc.)Example of Callback Executor:# callback.pyfromarclet.alconnaimportAlconna,Argsalc=Alconna("callback",Args["foo",int]["bar",str])@alc.bind()defcallback(foo:int,bar:str):print(f"foo:{foo}")print(f"bar:{bar}")print(bar*foo)if__name__=="__main__":alc()$pythoncallback.py3hello
foo:3bar:hello
hellohellohelloExample of Type Conversion:fromarclet.alconnaimportAlconna,ArgsfrompathlibimportPathread=Alconna("read",Args["data",bytes])@read.bind()defcb(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:# component.pyfromarclet.alconnaimportAlconna,Args,Option,Subcommand,store_true,count,appendalc=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"))$pythoncomponent.py/home/arclet-vvvv-f1-f2-f3subbar
/home/arclet4['1','2','3'](value=Ellipsisargs={}options={'bar':(value=Trueargs={})}subcommands={})Example of Command Shortcut:# shortcut.pyfromarclet.alconnaimportAlconna,Argsalc=Alconna("eval",Args["content",str])alc.shortcut("echo",{"command":"eval print(\\'{*}\\')"})@alc.bind()defcb(content:str):eval(content,{},{})if__name__=='__main__':alc()$pythonshortcut.pyevalprint(\"helloworld\")helloworld
$pythonshortcut.pyechohelloworld!
helloworld!Example of Command Completion:# completion.pyfromarclet.alconnaimportAlconna,Args,Optionalc=Alconna("complete",Args["bar",int])+Option("foo")+Option("fool")if__name__=="__main__":alc()$pythoncompletion.py?
suggestinputfollows:
*bar:int
*--help
*-h
*foo
*foolExample oftypingSupport:fromtypingimportAnnotated# or typing_extensions.Annotatedfromarclet.alconnaimportAlconna,Argsalc=Alconna("test",Args.foo[Annotated[int,lambdax:x%2==0]])alc.parse("test 2")alc.parse("test 3")''''foo': 2ParamsUnmatched: param 3 is incorrect'''Example of FuzzyMatch:# fuzzy.pyfromarclet.alconnaimportAlconna,CommandMeta,Argalc=Alconna('!test_fuzzy',Arg("foo",str),meta=CommandMeta(fuzzy_match=True))if__name__=="__main__":alc()$pythonfuzzy.py/test_fuzzyfoobar
/test_fuzynotmatched.Areyoumean"!test_fuzzy"?ExamplesNameFileCalculatecalculate.pyExecuteexec_code.pyRequest Routeendpoint.pyImage Searchimg_search.pyPIPpip.pyDatabase Queryexec_sql.pyLicenseAlconna is licensed under theMIT License.AcknowledgementJetBrains: Support Authorize forPyCharm