-
Notifications
You must be signed in to change notification settings - Fork 0
/
concept.py
43 lines (33 loc) · 1.59 KB
/
concept.py
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
from shelly.decorators import shell
# Custom type to show how one could use it to directly transform a raw string from the command line
class DoubleInt(int):
def __new__(cls, raw_value: str):
i = int.__new__(cls, raw_value)
return i * 2
# Not the most pythonic way to decorate a function, but it's the closest I could get to a fluent API
# Any suggestions are very welcome!
@shell()
@shell.option("-f", name="file_name", value_type=str, description="Example file name", required=True)
@shell.option("-t", name="double_int", value_type=DoubleInt, description="Double the integer input", required=True)
@shell.switch("--hash", name="hash_value", value_type=str, description="Example hash value", required=False)
@shell.flag("-v", name="verbosity_level", description="Verbosity level", required=False)
@shell.chain("-z", name="chain_example", value_type=int, description="Chain example", required=True)
def example(
file_name: shell.Option,
double_int: shell.Option,
hash_value: shell.Switch,
verbosity_level: shell.Flag,
chain_example: shell.Chain
):
print(file_name.description, file_name.value)
print(double_int.description, double_int.value)
print(hash_value.description, hash_value.value)
print(verbosity_level.description, verbosity_level.value)
print(chain_example.description, chain_example.value)
def main():
# This has to be called from the program itself... possible to remove?
shell.parse()
if __name__ == "__main__":
main()
# Example execution of this file:
# python concept.py -f "test.txt" -t 3 -v 5 -z 1 -z 3 -z 6 --hash=35t1251