Coverage for docs_src/parameter_types/custom_types/tutorial002.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 18:26 +0000

1import click 1habcdefg

2import typer 1habcdefg

3 

4 

5class CustomClass: 1habcdefg

6 def __init__(self, value: str): 1habcdefg

7 self.value = value 1habcdefg

8 

9 def __repr__(self): 1habcdefg

10 return f"<CustomClass: value={self.value}>" 1habcdefg

11 

12 

13class CustomClassParser(click.ParamType): 1habcdefg

14 name = "CustomClass" 1habcdefg

15 

16 def convert(self, value, param, ctx): 1habcdefg

17 return CustomClass(value * 3) 1habcdefg

18 

19 

20def main( 1abcdefg

21 custom_arg: CustomClass = typer.Argument(click_type=CustomClassParser()), 

22 custom_opt: CustomClass = typer.Option("Foo", click_type=CustomClassParser()), 

23): 

24 print(f"custom_arg is {custom_arg}") 1habcdefg

25 print(f"--custom-opt is {custom_opt}") 1habcdefg

26 

27 

28if __name__ == "__main__": 1habcdefg

29 typer.run(main) 1habcdefg