Skip to main content

将 python 函数注册为 bash 命令

项目描述

pybashrc: 自动将 python 函数注册为 bash 命令

这是一个非常简单的实用程序,它将创建一个~/.pybashrc.py文件,并且它包含的函数将作为 bash(或 zsh)命令访问。

默认情况下,pybashrc别名所有不以下划线开头并在其中创建的函数~/.pybashrc.py(即它们不能被导入)。例如,这是默认模板文件:

# Pybashrc file. Create your command-line accessible python functions here.
def _hidden_print_function(*args):
    """This function will be ignored by pybashrc, and will not be available from the
    command line.
    However, other functions can still use it.
    """
    print(*args)


def test_pybashrc(first_argument: str, second_argument: str = "second_argument"):
    """Default test function that simply prints its input arguments. This only serves as
    an example of how to define pybashrc functions.
    Arguments:
        - first_argument (str): The first argument.
        - second_argument (str): The second argument, which has a default value.
    """
    _hidden_print_function("This is the pybash default test function.")
    _hidden_print_function(f"Provided arguments: {first_argument}, {second_argument}")

运行时pybash,您将看到可用的功能:

$ pybash
Available functions:
- test_pybashrc(first_argument: str, second_argument: str = 'second_argument')
	Default test function that simply prints its input arguments. This only serves as an example of how to define pybashrc functions.

	Arguments:
		- first_argument (str): The first argument.
		- second_argument (str): The second argument, which has a default value.

您现在可以test_pybashrc直接从命令行执行:

$ test_pybashrc arg1 arg2
This is the pybash default test function.
Provided arguments: arg1, arg2

安装

只需运行pip install pybashrc,然后运行pybash-configure一次以设置 bash/zsh 别名文件等。之后,您就可以开始了!任何时候你更新你的.pybashrc.py,你的 shell 函数也将被更新(注意你需要重新启动 shell 才能使用新的别名)。如果您忘记了哪些功能可用或它们的用途是什么,只需运行pybash以获取概览。

点击功能

pybashrc还支持点击功能

import click

@click.command(
    help="A click command rather than a default python function."
    + " It has one argument with several options and a flag that can be toggled."
)
@click.argument(
    "first_arg",
    type=click.Choice(["option1", "option2", "option3"], case_sensitive=False),
)
@click.option(
    "-f",
    "--flag",
    is_flag=True,
    default=False,
    show_default=True,
    help="A command-line flag",
)
def click_function(first_arg, flag):
    print(f"Click function first arg: {first_arg}, flag: {flag}")
$ pybash
Available functions:
- click_function(first_arg, flag)
    Usage:  [OPTIONS] [option1|option2|option3]

      A click command rather than a default python function. It has one argument
      with several options and a flag that can be toggled.

    Options:
      -f, --flag  A command-line flag  [default: False]
      --help      Show this message and exit.

导出外部函数

您可能希望使不属于您的.pybashrc.py文件的 CLI 功能可用。例如,假设您想让re.sub命令行可访问。这可以很容易地通过简单地导入它,然后在中指定它来完成__all__

from re import sub

__all__ = ["sub"]

每当__all__指定 an 时,pybashrc将为这些函数起别名,无论它们是在您的定义中定义的.pybashrc.py还是从其他地方导入的。如果您现在运行pybash以查看可用命令:

$ pybash
Available functions:
- sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the Match object and must return
    a replacement string to be used.

所以你现在可以sub <pattern> <replacement> <string>直接从命令行调用!

$ sub "\s" "_" "some test string"
some_test_string

从不同的虚拟环境运行

要从不同的虚拟环境运行 pybash 脚本,只需确保已pybashrc安装该环境并pybash-configure从那里运行一次。完成后,pybashrc只需使用当前活动的虚拟环境执行您的脚本!

项目详情


下载文件

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

源分布

pybashrc-1.2.0.tar.gz (6.5 kB 查看哈希

已上传 source

内置分布

pybashrc-1.2.0-py3-none-any.whl (7.8 kB 查看哈希

已上传 py3