Click

Python 可合成的命令行接口工具包。(Python composable command line interface toolkit)

Github stars Tracking Chart

$ click_

Click 是一个 Python 包,可以用尽可能少的代码以可组合的方式创建漂亮的命令行界面。它是 "命令行界面创建工具包"。它的可配置性很高,但开箱即有合理的默认值。

它的目的是让编写命令行工具的过程变得快速而有趣,同时也防止因无法实现预定的CLI API而造成的任何挫折感。

Click 的特性表现在三个方面:

  • 命令的任意嵌套。
  • 自动生成帮助页面。
  • 支持在运行时懒惰加载子命令。

安装

使用pip进行安装和更新。

$ pip install -U click

一个简单的示例

import click
@click.command()
@click.option("--count", default=1, help="Number of greetings.")
@click.option("--name", prompt="Your name", help="The person to greet.")
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for _ in range(count):
        click.echo(f"Hello, {name}!")
if __name__ == '__main__':
    hello()
$ python hello.py --count=3
Your name: Click
Hello, Click!
Hello, Click!
Hello, Click!

捐献

Pallets 组织开发并支持Click和其他流行的软件包。为了发展贡献者和用户的社区,让维护者有更多的时间投入到项目中,请今天就捐款

链接


Main metrics

Overview
Name With Ownerpallets/click
Primary LanguagePython
Program languagePython (Language Count: 2)
PlatformLinux, Mac, Windows
License:BSD 3-Clause "New" or "Revised" License
所有者活动
Created At2014-04-24 09:52:19
Pushed At2025-06-02 03:32:38
Last Commit At
Release Count62
Last Release Name8.2.1 (Posted on 2025-05-20 19:10:46)
First Release Name0.1 (Posted on )
用户参与
Stargazers Count16.5k
Watchers Count183
Fork Count1.4k
Commits Count2.7k
Has Issues Enabled
Issues Count1648
Issue Open Count106
Pull Requests Count831
Pull Requests Open Count19
Pull Requests Close Count356
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

$ click_

Click is a Python package for creating beautiful command line interfaces
in a composable way with as little code as necessary. It's the "Command
Line Interface Creation Kit". It's highly configurable but comes with
sensible defaults out of the box.

It aims to make the process of writing command line tools quick and fun
while also preventing any frustration caused by the inability to
implement an intended CLI API.

Click in three points:

  • Arbitrary nesting of commands
  • Automatic help page generation
  • Supports lazy loading of subcommands at runtime

Installing

Install and update using pip_:

.. code-block:: text

$ pip install click

Click supports Python 3.4 and newer, Python 2.7, and PyPy.

.. _pip: https://pip.pypa.io/en/stable/quickstart/

A Simple Example

What does it look like? Here is an example of a simple Click program:

.. code-block:: python

import click

@click.command()
@click.option("--count", default=1, help="Number of greetings.")
@click.option("--name", prompt="Your name",
              help="The person to greet.")
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for _ in range(count):
        click.echo("Hello, %s!" % name)

if __name__ == '__main__':
    hello()

And what it looks like when run:

.. code-block:: text

$ python hello.py --count=3
Your name: Click
Hello, Click!
Hello, Click!
Hello, Click!

The Pallets organization develops and supports Click and other popular
packages. In order to grow the community of contributors and users, and
allow the maintainers to devote more time to the projects, please donate today_.

.. _please donate today: https://palletsprojects.com/donate