How to tell Click to always show option defaults
Asked Answered
D

2

15

In Python's Click package I can define a default for an option:

@click.option("--count", default=1, help="Number of greetings.")

and I can specify that the default should be shown in the help:

@click.option("--count", default=1, help="Number of greetings.", show_default=True)

If I have many options

@click.option("--count-a", default=1, help="...")
@click.option("--count-b", default=2, help="...")
@click.option("--count-c", default=4, help="...")
.
.
.

how can I tell Click generically to always show defaults in the help (without explicitly adding show_default=True to the parameter list of each individual option)?

Diazotize answered 19/8, 2019 at 3:2 Comment(0)
B
24

The probably most suited way to do that is by changing the context settings in the @click.command decorator. Like this:

@click.command("cli", context_settings={'show_default': True})
@click.option("-x")
@click.option("-y", default="Custom Y")
@click.option("-z", default="Custom Z", help="Value of Z.")
def cli(x, y, z):
    """Test changing context settings"""
    return "OK"

if __name__ == '__main__':
    cli()

This prints the following:

Usage: test.py [OPTIONS]

  Test changing context settings

Options:
  -x TEXT
  -y TEXT  [default: Custom Y]
  -z TEXT  Value of Z.  [default: Custom Z]
  --help   Show this message and exit. 
Bessiebessy answered 1/10, 2020 at 11:19 Comment(2)
Is there a way to disable show_default for a specific option? @click.option(..., show_default=False) doesn't appear to work.Inge
Fixed since click version 8.1.0: Command.show_default overrides Context.show_default, instead of the other way around. #1963Burgle
B
3

You can pretty easily define your own function for that like:

Code

def click_option(*args, **kwargs):
    return click.option(*args, show_default=True, **kwargs)

Or if you want to override show_default in individual option decorators:

def click_option(*args, **kwargs):
    if not 'show_default' in kwargs:
        kwargs.update({'show_default': True})
    return click.option(*args, **kwargs)

Test Code:

import click

def click_option(*args, **kwargs):
    if not 'show_default' in kwargs:
        kwargs.update({'show_default': True})
    return click.option(*args, **kwargs)

@click.command()
@click_option('--an-option', default='The Default')
def cli(an_option):
    """An Awesome CLI"""

if __name__ == "__main__":
    cli(['--help'])

Results:

Usage: test.py [OPTIONS]

  An Awesome CLI

Options:
  --an-option TEXT  [default: The Default]
  --help            Show this message and exit.
Barfly answered 19/8, 2019 at 3:43 Comment(2)
Interesting solution. Thanks. (I was hoping for a "Click-native" way with a parameter in, say, the @command decorator, but this probably doesn't exist.)Diazotize
Done. Accepted it as well.Diazotize

© 2022 - 2024 — McMap. All rights reserved.