Skip to content

Sentinel available in context during option callbacks #3136

@Rowlando13

Description

@Rowlando13

Sentinel is an implementation detail that is not intentionally exposed. However, it is in the context params during a callback: sentinel_in_callback.py

import click

def _validate(ctx, param, value):

    you_sentinel = ctx.params.get("you")
    you_too_sentinel = ctx.params.get("you_too")
    print(f"you: {you_sentinel}")
    print(f"you_too: {you_too_sentinel}")
    print(f"param: {param}")
    print(f"value: {value}")
    return value

@click.group()
def basic():
    pass

@basic.command()
@click.argument('you', required=False)
@click.option('--you-too', )
@click.option('--yall', callback=_validate)
@click.pass_context
def hello(ctx, you, you_too, yall):
    click.echo('Hello World!')

if __name__ == '__main__':
    basic()
    # python -m sentinel_in_callback hello
> python -m sentinel_in_callback hello
you: Sentinel.UNSET
you_too: Sentinel.UNSET
param: <Option yall>
value: None
Hello World!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions