也从cli执行python可调用函数!
clitoo的Python项目详细描述
cli too:也从cli执行python可调用函数!
有时我想在命令上执行带有一些参数的python回调 行,但我很沮丧它总是需要把我的命令包装成 某种类型的包装器,或者做一些比调用参数化 从cli回调。
克莱托不信任我。
安装:
$ pip install clitoo
帮助:
Clitoo makes your python callbacks work on CLI too ! This CLI can execute python callbacks with parameters. Clitoo recognizes 4 types of command line arguments: - lone arguments are passed as args - arguments with = are passed as kwargs - dashed arguments like -f arrive in context.args - dashed arguments like -foo=bar arrive in context.kwargs It doesn't matter how many dashes you put in the front, they are all removed. To use the context in your callback just import the clitoo context:: from clitoo import context print(context.args, context.kwargs) Clitoo provides 2 builtin commands: help and debug. Any other first argument will be considered as the dotted path to the callback to import and execute. Examples: clitoo help your.mod.funcname Print out the function docstring. clitoo debug your.func -a --b --something='to see' how it=parses Dry run of your.mod with arguments, dump out actual calls. clitoo your.mod.funcname with your=args Call your.mod.funcname('with', your='args').
演示:
$ clitoo debug your.func -a --b --something='to see' how it=parses Could not import your.func nor clitoo.your.func Args: ('how',) Kwargs: {'it': 'parses'} Context args: ['a', 'b'] Context kwargs: {'something': 'to see'}
回退
clitoo将尝试回退自己的包。如果它找不到
git.clone
从git
包回调,或找不到git
包
它本身会找到clitoo.git.clone
,这是我们使用的内置命令
在ci中。
制定自己的命令
有关打包为 独立的,但看起来像:
# Declare the following as CLI entry_point def cli(): clitoo.context.default_module = __name__ return clitoo.main()