Coverage for tests / test_cli / test_parser.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-09 00:15 +0000

1import subprocess 1abcdefg

2import sys 1abcdefg

3 

4import typer 1abcdefg

5from typer.testing import CliRunner 1abcdefg

6 

7runner = CliRunner() 1abcdefg

8 

9 

10def test_double_dash() -> None: 1abcdefg

11 result = subprocess.run( 1abcdefg

12 [ 

13 sys.executable, 

14 "-m", 

15 "coverage", 

16 "run", 

17 "-m", 

18 "typer", 

19 "tests/assets/cli/sample.py", 

20 "run", 

21 "hello", 

22 "--", 

23 "--name", 

24 "Camila", 

25 ], 

26 capture_output=True, 

27 encoding="utf-8", 

28 ) 

29 assert "Got unexpected extra argument" in result.stderr 1abcdefg

30 assert "--name Camila" in result.stderr 1abcdefg

31 

32 

33def test_unknown_short_option() -> None: 1abcdefg

34 result = subprocess.run( 1abcdefg

35 [ 

36 sys.executable, 

37 "-m", 

38 "coverage", 

39 "run", 

40 "-m", 

41 "typer", 

42 "tests/assets/cli/sample.py", 

43 "run", 

44 "hello", 

45 "-x", 

46 ], 

47 capture_output=True, 

48 encoding="utf-8", 

49 ) 

50 assert "No such option: -x" in result.stderr 1abcdefg

51 

52 

53def test_ignore_unknown_short_option() -> None: 1abcdefg

54 app = typer.Typer( 1abcdefg

55 context_settings={"ignore_unknown_options": True, "allow_extra_args": True} 

56 ) 

57 

58 @app.command() 1abcdefg

59 def main( 1abcdefg

60 ctx: typer.Context, all_: bool = typer.Option(False, "--all", "-a") 

61 ) -> None: 

62 assert all_ 1abcdefg

63 print(ctx.args) 1abcdefg

64 

65 result = runner.invoke(app, ["-azq"]) 1abcdefg

66 assert result.exit_code == 0 1abcdefg

67 assert "['-zq']" in result.output 1abcdefg