-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_script.py
More file actions
166 lines (126 loc) · 3.3 KB
/
example_script.py
File metadata and controls
166 lines (126 loc) · 3.3 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import asyncio
import decimal
from typing import Optional
from targ import CLI
def echo(message: str):
"""
Echo back the message.
:param message:
What will be printed out.
"""
print(message)
def add(a: int, b: int):
"""
Add the two numbers.
:param a:
The first number.
:param b:
The second number.
"""
print(a + b)
def say_hello(name: str, greeting: str = "hello"):
"""
Greet someone.
Example usage on the command line:
say_hello daniel --greeting='bonjour'
>>> bonjour daniel
:param name:
The person to greet.
:param greeting:
What to say to the person.
"""
print(f"{greeting} {name}")
# print_address --number=1 --street="Royal Avenue" --postcode="XYZ 123"
# --city=London
def print_address(
number: int, street: str, postcode: str, city: Optional[str] = None
):
"""
Print out the full address.
:param number:
House number, e.g. 8
:street:
Street name, e.g. "Royal Avenue"
:postcode:
e.g. "XYZ 123"
:city:
e.g. London
"""
address = f"{number} {street}"
if city is not None:
address += f", {city}"
address += f", {postcode}"
print(address)
def print_pi(precise: bool = False):
"""
Print out the digits of Pi.
:param precise:
If set, then more digits are printed out.
"""
if precise:
print("3.14159265")
else:
print("3.14")
def compound_interest(interest_rate: float, years: int):
"""
Work out the compound interest over the given number of years.
:param interest_rate:
The annual interest rate e.g. 0.05
:param years:
The number of years over which to compound.
"""
print(((interest_rate + 1) ** years) - 1)
def compound_interest_decimal(interest_rate: decimal.Decimal, years: int):
"""
Work out the compound interest over the given number of years.
:param interest_rate:
The annual interest rate e.g. 0.05
:param years:
The number of years over which to compound.
"""
print(((interest_rate + 1) ** years) - 1)
def create(username: str):
"""
Create a new user.
:param username:
The new user's username.
"""
print(f"Creating {username}")
async def timer(seconds: int):
"""
Countdown for a number of seconds.
:param seconds:
The number of seconds to countdown.
"""
print(f"Sleeping for {seconds}")
await asyncio.sleep(seconds)
print("Finished")
def raise_error():
"""
A command which raises an Exception.
"""
print("Raising an exception")
raise ValueError("Something went wrong!")
def optional_value(value: Optional[int] = None):
"""
A command which accepts an optional value.
"""
if value:
print(value + 1)
else:
print("Unknown")
if __name__ == "__main__":
cli = CLI()
cli.register(say_hello)
cli.register(echo)
cli.register(add, aliases=["+"])
cli.register(print_pi)
cli.register(compound_interest)
cli.register(compound_interest_decimal)
cli.register(create, group_name="user")
cli.register(timer)
cli.register(add, command_name="sum")
cli.register(print_address)
cli.register(raise_error)
cli.register(optional_value)
cli.run()