pallets / pallets/click

Add output of get_usage() to get_info_dict()

Open
#2,992 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

parsing
Dominant language
Python
Stars
17.7k
Forks
2.3k
Avg merge
1d 30m
Merged PRs (30d)
18

Description

Context

I am generating some of my user-facing documentation using get_info_dict(), and was looking for a way to automate the usage as well without having to hardcode it in the docstrings.

Potential solution

Using the calls of get_info_dict() to add a usage key with the value being the output of get_usage().

Other relevant issues: #2762

Workaround

For now I'm using get_info_dict(), then looping through the context to get groups and commands, and using get_usage() on each, then adding it to that dictionary. The code looks something like this (with the for loop matching the structure of my CLI):

    info = ctx.to_info_dict()
    for cmd_n1 in ctx.command.list_commands(ctx):
        cmd2 = ctx.command.get_command(ctx, cmd_n1)
        sub_ctx = ctx._make_sub_context(cmd2)
        usage = cmd2.get_usage(sub_ctx).replace("Usage: ", "")
        info["command"]["commands"][cmd_n1]["usage"] = usage
        if isinstance(cmd2, click.Group):
            # Groups
            for cmd_n2 in cmd2.list_commands(ctx):
                cmd3 = cmd2.get_command(ctx, cmd_n2)
                sub_ctx3 = sub_ctx._make_sub_context(cmd3)
                usage = cmd3.get_usage(sub_ctx3).replace("Usage: ", "")
                info["command"]["commands"][cmd_n1]["commands"][cmd_n2]["usage"] = usage

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing get_info_dict() and get_usage() in Click's command and group APIs, then inspect the returned info-dict structure and related issue #2762. Done means the dictionary includes usage output for commands and nested commands without requiring the documented workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.