单击

其他 社区

技能解读

当前原文的自动摘译,尚未人工审校。

命令

对单个命令使用 @click.command(),对多命令 CLI 使用 @click.group()。使用 @click.option() 声明选项,并使用 @click.argument() 声明位置参数。在每个选项和命令上使用 help= 来自动生成帮助文本。使用 envvar= 允许敏感选项的环境变量回退。

团体

用 @click.group() 和 group.add_command() 组织子命令。使用 @click.pass_context 在组和子命令之间共享状态。

类型安全

使用 Click 的内置类型(click.Path(exists=True)、click.Choice([...])、click.IntRange())。使用回调进行自定义验证。

测试

使用 click.testing.CliRunner() 测试命令,无需子进程开销。对result.exit_code和result.output断言。使用mix_stderr=False单独测试stderr。

陷阱

不要使用 sys.exit() — 使用 click.exceptions.Exit 或从命令中返回。不要使用 print() — 使用 click.echo() 进行正确的编码处理。始终优雅地处理 KeyboardInterrupt / 中止提示。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
通用
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
94 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@microsoft · NOASSERTION
上下文用量评级
低消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 命令执行
  • 读取环境变量
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# Skill: Click

Best practices for building CLI applications with Click including commands, groups, options, and testing.

## When to Use

Apply this skill when building command-line interfaces with Click — commands, groups, options, arguments, and prompts.

## Commands

-   Use `@click.command()` for single commands, `@click.group()` for multi-command CLIs.
-   Declare options with `@click.option()` and positional args with `@click.argument()`.
-   Use `help=` on every option and command for auto-generated help text.
-   Use `envvar=` to allow environment variable fallback for sensitive options.

## Groups

-   Organize subcommands with `@click.group()` and `group.add_command()`.
-   Use `@click.pass_context` to share state between group and subcommands.

## Type Safety

-   Use Click's built-in types (`click.Path(exists=True)`, `click.Choice([...])`, `click.IntRange()`).
-   Use callbacks for custom validation.

## Testing

-   Use `click.testing.CliRunner()` for testing commands without subprocess overhead.
-   Assert on `result.exit_code` and `result.output`.
-   Use `mix_stderr=False` to test stderr separately.

## Pitfalls

-   Don't use `sys.exit()` — use `click.exceptions.Exit` or return from the command.
-   Don't use `print()` — use `click.echo()` for proper encoding handling.
-   Always handle `KeyboardInterrupt` / abort prompts gracefully.

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。