|
15 | 15 | from click import Option |
16 | 16 | from click import UNPROCESSED |
17 | 17 | from click._utils import UNSET |
| 18 | +from click.testing import CliRunner |
18 | 19 |
|
19 | 20 |
|
20 | 21 | def test_prefixes(runner): |
@@ -2275,3 +2276,46 @@ def rcli(scm_ignore_files): |
2275 | 2276 | result = runner.invoke(rcli, ["--without-scm-ignore-files"]) |
2276 | 2277 | assert result.stdout == "frozenset()" |
2277 | 2278 | assert result.exit_code == 0 |
| 2279 | + |
| 2280 | + |
| 2281 | +@pytest.mark.parametrize( |
| 2282 | + ("flag_type", "args", "expect_output"), |
| 2283 | + [ |
| 2284 | + (str, [], "Default\n"), |
| 2285 | + (str, ["--theflag"], "FlagValue\n"), |
| 2286 | + (str, ["--theflag", "value"], "value\n"), |
| 2287 | + (int, [], "0\n"), |
| 2288 | + (int, ["--theflag"], "1\n"), |
| 2289 | + (int, ["--theflag", "2"], "2\n"), |
| 2290 | + ], |
| 2291 | +) |
| 2292 | +def test_flag_value_on_option_with_zero_or_one_args(flag_type, args, expect_output): |
| 2293 | + """An option with flag_value and is_flag=False can be |
| 2294 | + omitted or used with 0 or 1 args. |
| 2295 | +
|
| 2296 | + Regression test for https://github.com/pallets/click/issues/3084 |
| 2297 | + """ |
| 2298 | + if flag_type is str: |
| 2299 | + flagopt = click.option( |
| 2300 | + "--theflag", |
| 2301 | + type=str, |
| 2302 | + is_flag=False, |
| 2303 | + flag_value="FlagValue", |
| 2304 | + default="Default", |
| 2305 | + ) |
| 2306 | + elif flag_type is int: |
| 2307 | + flagopt = click.option( |
| 2308 | + "--theflag", type=int, is_flag=False, flag_value=1, default=0 |
| 2309 | + ) |
| 2310 | + else: |
| 2311 | + raise NotImplementedError(flag_type) |
| 2312 | + |
| 2313 | + @click.command() |
| 2314 | + @flagopt |
| 2315 | + def cmd(theflag): |
| 2316 | + click.echo(theflag) |
| 2317 | + |
| 2318 | + runner = CliRunner() |
| 2319 | + result = runner.invoke(cmd, args) |
| 2320 | + assert result.exit_code == 0 |
| 2321 | + assert result.output == expect_output |
0 commit comments