click.confirm with default=None repeats prompt
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by yurihs · committed by David Lord · parent b419407 · Browse files · 3 files·+24 added−6 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+24 added−6 removed
- tests:+8 added−0 removed
- other:+16 added−6 removed
Provenance
- Commit
c2a5bb0- Tree
d17e49d- Author
yurihs <yurisalvador@hotmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
b419407
3 files changed+24 added−6 removed
modified:src/click/termui.py+14 added-6 removed
170 unmodified lines171 172 If the user aborts the input by sending a interrupt signal this173 function will catch it and raise a :exc:`Abort` exception.174 175- .. versionadded:: 4.0176- Added the `err` parameter.177-175 :param text: the question to ask.179- :param default: the default for the prompt.176+ :param default: The default value to use when no input is given. If177+ ``None``, repeat until input is given.178 :param abort: if this is set to `True` a negative answer aborts the179 exception by raising :exc:`Abort`.180 :param prompt_suffix: a suffix that should be added to the prompt.181 :param show_default: shows or hides the default value in the prompt.182 :param err: if set to true the file defaults to ``stderr`` instead of183 ``stdout``, the same as with echo.184+185+ .. versionchanged:: 8.0186+ Repeat until input is given if ``default`` is ``None``.187+188+ .. versionadded:: 4.0189+ Added the ``err`` parameter.190 """191 prompt = _build_prompt(188- text, prompt_suffix, show_default, "Y/n" if default else "y/N"192+ text,193+ prompt_suffix,194+ show_default,195+ "y/n" if default is None else ("Y/n" if default else "y/N"),196 )197+198 while 1:199 try:200 # Write the prompt separately so that we get nice201 # coloring through colorama on Windows4 unmodified lines206 if value in ("y", "yes"):207 rv = True208 elif value in ("n", "no"):209 rv = False202- elif value == "":210+ elif default is not None and value == "":211 rv = default212 else:213 echo("Error: invalid input", err=err)214 continue510 unmodified linesmodified:tests/test_utils.py+8 added
129 unmodified lines130 assert not result.exception131 assert result.output == "Foo [Y/n]: n\nno :(\n"132 133 134+def test_confirm_repeat(runner):135+ cli = click.Command(136+ "cli", params=[click.Option(["--a/--no-a"], default=None, prompt=True)]137+ )138+ result = runner.invoke(cli, input="\ny\n")139+ assert result.output == "A [y/n]: \nError: invalid input\nA [y/n]: y\n"140+141+142 @pytest.mark.skipif(WIN, reason="Different behavior on windows.")143 def test_prompts_abort(monkeypatch, capsys):144 def f(_):145 raise KeyboardInterrupt()286 unmodified linesmodified:CHANGES.rst+2 added
162 unmodified lines163 8.1. Use :func:`shutil.get_terminal_size` instead. :issue:`1736`164 - Control the location of the temporary directory created by165 ``CLIRunner.isolated_filesystem`` by passing ``temp_dir``. A custom166 directory will not be removed automatically. :issue:`395`167+- ``click.confirm()`` will prompt until input is given if called with168+ ``default=None``. :issue:`#1381`169 170 171 Version 7.1.2172 -------------635 unmodified lines