Coverage for src/su6_plugin_demo/cli.py: 91%

1"""

2 This module contains an example of both methods of adding commands to su6.

3"""

4 import typing

5

6 from su6.plugins import register, run_tool, PluginConfig, print

7

8# or: from su6 import register_plugin, run_tool

9 from typer import Typer

10

11

12# method 1: adding top-level commands

13

14

15@register(with_state=True, config_key="demo.extra")

16 class MoreDemoConfig(PluginConfig):

17 more: bool

18

19

20@register

21 class DemoConfig(PluginConfig):

22 required_arg: str

23 boolean_arg: bool

24 optional_with_default: typing.Optional[str] = None

25 more: PluginConfig = MoreDemoConfig(more=False)

26

27

28 config = DemoConfig()

29

30

31@register

32 def first() -> int:

33 """

34 Register a top-level command.

35

36 @register works without ()

37 """

38 print("This is a demo command!")

39 return 0

40

41

42@register()

43 def second() -> int:

44 """

45 Register a top-level command.

46

47 @register also works with ()

48 """

49 print("This is another demo command (with exit code)!")

50 run_tool("echo", "args", "go", "here")

51 return 1

52

53

54@register(name="third")

55 def yet_another() -> bool:

56 """

57 Register a top-level command.

58

59 @register works with extra Typer arguments.

60 """

61 print("This is another demo command (with bool exit)!")

62 return True

63

64

65@register()

66 def with_arguments(required_arg: str, boolean_arg: bool = False) -> None:

67 """

68 Register a top-level command.

69

70 @register works with extra Typer arguments.

71 """

72 config.update(required_arg=required_arg, boolean_arg=boolean_arg)

73 print(config)

74 assert config.more.state.config.pyproject == config.more.extras['state'].config.pyproject

75

76

77# method 2: adding a namespace (based on the plugin package name)

78

79 app = Typer()

80

81

82@app.command()

83 def subcommand() -> None:

84 """

85 Register a plugin-level command.

86

87 Can be used as `su6 demo subcommand` (in this case, the plugin name is demo)

88 """

89 print("this lives in a namespace")