Skip to content

required=True does not work for flags #1978

Closed

Description

@rockwellw

When using a flag option, setting required=True has no effect unless I also include default=None. I would expect there to be no default if the flag is required.

Steps to reproduce:

import click

@click.command()
@click.option('--foo/--no_foo', is_flag=True, required=True)
def bar(foo):
    pass

if __name__ == '__main__':
    bar()

python test.py runs without raising an error.

I would expect python test.py to raise:

Usage: test.py [OPTIONS]
Try 'test.py --help' for help.

Error: Missing option '--foo'.

Environment:

  • Python version: 3.9.5
  • Click version: 8.0.1

Activity

  1. added this to the 8.1.0 milestone on Jul 7, 2021
  2. locked as resolved and limited conversation to collaborators on Mar 7, 2022
  3. added
    parsingParsing, parameters, commands, chaining, context
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    parsingParsing, parameters, commands, chaining, context

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions