Skip to content
Myr

Merge pull request #1990 from dannysepler/dsepler/enforce-required-flag

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Enforce required flag

authored by David Lord · committed by GitHub · parents 422cb20, 4083008 · Browse files · 3 files+12 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+12 added−1 removed

  • tests:+9 added−0 removed
  • other:+3 added−1 removed

3 files+12 added−1 removed

modified:src/click/core.py+1 added-1 removed
2509 unmodified lines
2510 # Not a flag, and prompt is not enabled, can be used as a2511 # flag if flag_value is set.2512 self._flag_needs_value = flag_value is not None25132514 if is_flag and default_is_missing:2514 if is_flag and default_is_missing and not self.required:2515 self.default: t.Union[t.Any, t.Callable[[], t.Any]] = False25162517 if flag_value is None:2518 flag_value = not self.default
456 unmodified lines
modified:tests/test_options.py+9 added
500 unmodified lines
501502 assert str(excinfo.value) == "Missing parameter: a"503504505def test_missing_required_flag(runner):506 cli = click.Command(507 "cli", params=[click.Option(["--on/--off"], is_flag=True, required=True)]508 )509 result = runner.invoke(cli)510 assert result.exit_code == 2511 assert "Error: Missing option '--on'." in result.output512513514def test_missing_choice(runner):515 @click.command()516 @click.option("--foo", type=click.Choice(["foo", "bar"]), required=True)517 def cmd(foo):
388 unmodified lines
modified:CHANGES.rst+2 added
29 unmodified lines
30- Indentation in help text passed to ``Option`` and ``Command`` is31 cleaned the same as using the ``@option`` and ``@command``32 decorators does. A command's ``epilog`` and ``short_help`` are also33 processed. :issue:`1985`34- A flag option with ``required=True`` requires that the flag is35 passed instead of choosing the implicit default value. :issue:`1978`363738Version 8.0.439-------------
948 unmodified lines