跳转到主要内容

从CLI执行Python可调用对象!

项目描述

clitoo: 从CLI执行Python可调用对象!

有时候我想在命令行上执行一个带有一些参数的Python回调,但我总是很沮丧,因为它总是需要将我的命令包装在一些包装器中,或者做更多的事情,只是从CLI调用一个参数化的回调。

Clitoo让我不再沮丧。

安装

$ 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包的git.clone回调,或者找不到git包本身,它会找到clitoo.git.clone,这是一个我们在CI中使用的内置命令。

创建自己的命令

查看djcli存储库中包装为独立命令的示例,但它看起来像

# Declare the following as CLI entry_point
def cli():
    clitoo.context.default_module = __name__
    return clitoo.main()

项目详情


下载文件

下载适用于您平台的文件。如果您不确定该选择哪一个,请了解更多关于安装包的信息

源代码分发

clitoo-1.0.2.tar.gz (5.3 kB 查看哈希值)

上传时间 源代码

支持