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

21 statements  

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

1import subprocess 1abcdefghij

2import sys 1abcdefghij

3 

4import typer 1abcdefghij

5from typer.testing import CliRunner 1abcdefghij

6 

7runner = CliRunner() 1abcdefghij

8 

9 

10def test_double_dash() -> None: 1abcdefghij

11 result = subprocess.run( 1abcdefghij

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 1abcdefghij

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

31 

32 

33def test_unknown_short_option() -> None: 1abcdefghij

34 result = subprocess.run( 1abcdefghij

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 1abcdefghij

51 

52 

53def test_ignore_unknown_short_option() -> None: 1abcdefghij

54 app = typer.Typer( 1abcdefghij

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

56 ) 

57 

58 @app.command() 1abcdefghij

59 def main( 1abcdefghij

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

61 ) -> None: 

62 assert all_ 1abcdefghij

63 print(ctx.args) 1abcdefghij

64 

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

66 assert result.exit_code == 0 1abcdefghij

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