-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands_example
More file actions
executable file
·105 lines (82 loc) · 3.19 KB
/
commands_example
File metadata and controls
executable file
·105 lines (82 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
#
# An example script demonstrating typical usage of commands.py
from argparse import ArgumentParser, Namespace
from commands import BaseCommandSequenceStep, CommandSequenceProgram
class HelloWorldStep(BaseCommandSequenceStep):
# TODO(timzwiebel): Add @override when it becomes available in Python 3.12
def _run(self, args: Namespace) -> None:
self._run_command('Print "Hello"', 'echo \'Hello\'')
self._run_command('Print "World!"', 'echo \'World!\'')
class PrintWelcomeMessageStep(BaseCommandSequenceStep):
def __init__(self):
super().__init__('1')
# TODO(timzwiebel): Add @override when it becomes available in Python 3.12
def _add_args(self, argument_parser: ArgumentParser) -> None:
argument_parser.add_argument(
'-n',
'--name',
required=True,
help=f'your name (required)')
# TODO(timzwiebel): Add @override when it becomes available in Python 3.12
def _run(self, args: Namespace) -> None:
self._run_command(
'Print a welcome message',
f'echo \'Welcome {args.name}!\'')
class PrintValuesStep(BaseCommandSequenceStep):
def __init__(self):
super().__init__('2', 'Print the values')
# TODO(timzwiebel): Add @override when it becomes available in Python 3.12
def _add_args(self, argument_parser: ArgumentParser) -> None:
default_string_value = 'foo'
argument_parser.add_argument(
'--string-value',
default=default_string_value,
help=f'set the string value (default: {default_string_value})')
default_int_value = 42
argument_parser.add_argument(
'--int-value',
default=default_int_value,
type=int,
help=f'set the int value (default: {default_int_value})')
argument_parser.add_argument(
'--bool-flag',
action='store_true',
help=f'set the boolean flag')
# TODO(timzwiebel): Add @override when it becomes available in Python 3.12
def _run(self, args: Namespace) -> None:
self._run_command(
'Print the string value',
f'echo \'The string value is "{args.string_value}"\'')
self._run_command(
'Print the int value',
f'echo \'The int value is {args.int_value}\'')
if args.bool_flag:
self._run_command(
'Print that the bool flag is set',
f'echo \'The bool flag is set!\'')
else:
self._run_command(
'Print that the bool flag is NOT set',
f'echo \'The bool flag is NOT set!\'')
class PrintGoodbyeMessageStep(BaseCommandSequenceStep):
def __init__(self):
super().__init__(step_description='Say goodbye')
# TODO(timzwiebel): Add @override when it becomes available in Python 3.12
def _run(self, args: Namespace) -> None:
self._run_command('Print a goodbye message', 'echo \'Bye!\'')
class InvalidStep(BaseCommandSequenceStep):
def __init__(self):
super().__init__(None, 'This step failed to override the _run method')
def main():
program = CommandSequenceProgram(
'An example script demonstrating typical usage of commands.py',
[
HelloWorldStep(),
PrintWelcomeMessageStep(),
PrintValuesStep(),
PrintGoodbyeMessageStep(),
# InvalidStep(),
])
program.run()
main()