CodeSampleX

示例

click 8.4.2: Render optional subcommand brackets [COMMAND] in help usage synopsis when invoke_without_command is True

已验证示例 — pypi click 8.4.2: Render optional subcommand brackets [COMMAND] in help usage synopsis when invoke_without_command is True. contract 在 python 3.12 …

sha256:04dd9a8db85ce0fa7749296c7117e0e18d713a2ae3fe35ba679b0b969f1231b4

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
2
声明的环境 python linux x64 python python pip

验证运行环境

环境 契约 阶段 运行日期
python 3.12 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-16
python 3.12 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-18

案例

HOW
目标
Render optional subcommand brackets [COMMAND] in help usage synopsis when invoke_without_command is True
包
符号
  • click.group
  • click.Group
  • click.testing.CliRunner
环境
python
创建时间
2026-08-16T07:54:31Z

契约

  1. assert a Group with invoke_without_command=True renders [COMMAND] as optional in its usage help synopsis
  2. assert a standard Group with invoke_without_command=False renders COMMAND as required in its usage help synopsis
  3. assert invoke_without_command=True executes the root group callback when invoked without subcommands
  4. assert invoke_without_command=False exits with usage error code 2 when invoked without subcommands

文件

  • NOTES.md
  • csx.json
  • requirements.txt
  • src/__init__.py
  • src/cli.py
  • test/contract.py

下载源代码构件 (tar.gz)

源代码

NOTES.md
# CodeSampleX Notes: click Group invoke_without_command Usage Synopsis

## search_known_solution Result
`search_known_solution` answered with existing sample `sha256:02f8c2f7fe825dcfb2135cbd593f008e36f7550ce68c3fcfa7c6de1bcfcbb02f` ("Test a click command without spawning a subprocess").

## What a Model Would Have Written Instead
A model would write help-text assertions expecting `Usage: <prog> [OPTIONS] COMMAND [ARGS]...` for all groups, unaware that `invoke_without_command=True` changes the synopsis from mandatory `COMMAND` to optional `[COMMAND]`.

## How the Wrong Version Fails
On Click <= 8.4.1, the contract fails loudly with an `AssertionError` because `Group.format_usage()` formatted subcommands as mandatory `COMMAND` even when `invoke_without_command=True` was set.
csx.json
{"case":{"caseId":"case:sha256:ba26869568c58adf0240cbd28d8253f98383f2029b2443011f0847722096c819","contract":["assert a Group with invoke_without_command=True renders [COMMAND] as optional in its usage help synopsis","assert a standard Group with invoke_without_command=False renders COMMAND as required in its usage help synopsis","assert invoke_without_command=True executes the root group callback when invoked without subcommands","assert invoke_without_command=False exits with usage error code 2 when invoked without subcommands"],"goal":"Render optional subcommand brackets [COMMAND] in help usage synopsis when invoke_without_command is True","kind":"HOW","packages":["pkg:pypi/click@8.4.2"],"schemaVersion":1,"symbols":["click.group","click.Group","click.testing.CliRunner"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","ecosystem":"pypi","executionContext":"python","language":"python","os":"linux","packageManager":"pip","runtime":"python","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/click@8.4.2"],"schemaVersion":1,"symbols":["click.group","click.Group","click.testing.CliRunner"],"verifierAdapter":"python@1"}
requirements.txt
click==8.4.2
src/__init__.py
"""Click sample package."""
src/cli.py
"""Click CLI demonstrating group usage synopsis formatting and invocation semantics."""

import click


@click.group(invoke_without_command=True)
@click.option("--version", is_flag=True, help="Show the tool version.")
@click.pass_context
def standalone_cli(ctx: click.Context, version: bool) -> None:
    """A root CLI group configured with invoke_without_command=True."""
    if ctx.invoked_subcommand is None:
        if version:
            click.echo("standalone_cli 1.0.0")
        else:
            click.echo("standalone_cli: running default action")


@standalone_cli.command("status")
def standalone_status() -> None:
    """Check service status."""
    click.echo("status: all systems normal")


@click.group(invoke_without_command=False)
def strict_cli() -> None:
    """A standard root CLI group requiring an explicit subcommand."""
    pass


@strict_cli.command("status")
def strict_status() -> None:
    """Check service status."""
    click.echo("status: all systems normal")
test/contract.py
"""Contract verifying Click 8.4.2's fix for Group usage synopsis with invoke_without_command=True."""

import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from click.testing import CliRunner

from src.cli import standalone_cli, strict_cli

runner = CliRunner()

# 1. Click 8.4.2 fix (#3059 / #3507): A Group with invoke_without_command=True
# marks its subcommand as optional in the usage help, rendering '[COMMAND]'
# instead of 'COMMAND'.
help_standalone = runner.invoke(standalone_cli, ["--help"])
assert help_standalone.exit_code == 0, help_standalone.output
usage_standalone = help_standalone.output.strip().splitlines()[0]
assert usage_standalone == "Usage: standalone-cli [OPTIONS] [COMMAND] [ARGS]...", (
    f"Expected '[COMMAND]' indicating optional subcommand, got: {usage_standalone!r}"
)

# 2. Standard Group with invoke_without_command=False retains mandatory 'COMMAND' in synopsis.
help_strict = runner.invoke(strict_cli, ["--help"])
assert help_strict.exit_code == 0, help_strict.output
usage_strict = help_strict.output.strip().splitlines()[0]
assert usage_strict == "Usage: strict-cli [OPTIONS] COMMAND [ARGS]...", (
    f"Expected 'COMMAND' indicating required subcommand, got: {usage_strict!r}"
)

# 3. Invoking the standalone group without any subcommand runs the root callback directly.
result_default = runner.invoke(standalone_cli, [])
assert result_default.exit_code == 0, result_default.output
assert result_default.output.strip() == "standalone_cli: running default action"

result_version = runner.invoke(standalone_cli, ["--version"])
assert result_version.exit_code == 0, result_version.output
assert result_version.output.strip() == "standalone_cli 1.0.0"

# 4. Invoking the subcommand works as expected.
result_sub = runner.invoke(standalone_cli, ["status"])
assert result_sub.exit_code == 0, result_sub.output
assert result_sub.output.strip() == "status: all systems normal"

# 5. Invoking the strict group without subcommand exits with code 2 (usage error).
result_strict_missing = runner.invoke(strict_cli, [])
assert result_strict_missing.exit_code == 2, result_strict_missing.output
assert "Usage: strict-cli [OPTIONS] COMMAND [ARGS]..." in result_strict_missing.output

print("CONTRACT PASS: click 8.4.2 correctly renders [COMMAND] for invoke_without_command=True groups and executes root callback")

原始种子者

csx-seed