diff options
author | Danny Sepler <dannysepler@gmail.com> | 2021-07-05 12:33:40 -0400 |
---|---|---|
committer | David Lord <davidism@gmail.com> | 2022-02-20 11:08:18 -0800 |
commit | 40830087dd9e9023dbcf75befe0ca9056127a064 (patch) | |
tree | c0550260fe4a4e1f8229638425bd6221dec676b3 | |
parent | 422cb2064eed146dbe03ba3aed5be35daeb70414 (diff) | |
download | click-40830087dd9e9023dbcf75befe0ca9056127a064.tar.gz |
enforce required flag
-rw-r--r-- | CHANGES.rst | 2 | ||||
-rw-r--r-- | src/click/core.py | 2 | ||||
-rw-r--r-- | tests/test_options.py | 9 |
3 files changed, 12 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index a01dd43..35307d4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -31,6 +31,8 @@ Version 8.1.0 cleaned the same as using the ``@option`` and ``@command`` decorators does. A command's ``epilog`` and ``short_help`` are also processed. :issue:`1985` +- A flag option with ``required=True`` requires that the flag is + passed instead of choosing the implicit default value. :issue:`1978` Version 8.0.4 diff --git a/src/click/core.py b/src/click/core.py index 97d699e..1ab2b20 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -2511,7 +2511,7 @@ class Option(Parameter): # flag if flag_value is set. self._flag_needs_value = flag_value is not None - if is_flag and default_is_missing: + if is_flag and default_is_missing and not self.required: self.default: t.Union[t.Any, t.Callable[[], t.Any]] = False if flag_value is None: diff --git a/tests/test_options.py b/tests/test_options.py index ab230c4..3beff11 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -502,6 +502,15 @@ def test_missing_option_string_cast(): assert str(excinfo.value) == "Missing parameter: a" +def test_missing_required_flag(runner): + cli = click.Command( + "cli", params=[click.Option(["--on/--off"], is_flag=True, required=True)] + ) + result = runner.invoke(cli) + assert result.exit_code == 2 + assert "Error: Missing option '--on'." in result.output + + def test_missing_choice(runner): @click.command() @click.option("--foo", type=click.Choice(["foo", "bar"]), required=True) |