mkdocs-click icon indicating copy to clipboard operation
mkdocs-click copied to clipboard

pass command.context_settings to new context

Open alkasm opened this issue 1 year ago • 0 comments

When creating a context from the command, the context settings were not propagated, which means that some options set on the command would be ignored.

To illustrate:

import click
@click.command(context_settings={"help_option_names": ["-h", "--help"], "show_default": True})
@click.option("-t", "--test", type=int, default=42)
def cli(test):
    print("the number is", test)

Now compare ctx.get_help() when the context is and is not passed through:

>>> ctx = click.Context(cli)
>>> print(ctx.get_help())
Usage:  [OPTIONS]

Options:
  -t, --test INTEGER
  --help              Show this message and exit.
>>> ctx = click.Context(cli, **cli.context_settings)
>>> print(ctx.get_help())
Usage:  [OPTIONS]

Options:
  -t, --test INTEGER  [default: 42]
  -h, --help          Show this message and exit.

You'll see the additional -h short flag for help, and the default argument for -t which wasn't shown before.

With this fix, these settings are now respected through :style: plain so that the output matches what is shown at the command line after building with mkdocs.

alkasm avatar Sep 13 '24 18:09 alkasm