-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
62 lines (48 loc) · 1.53 KB
/
Copy pathexample.py
File metadata and controls
62 lines (48 loc) · 1.53 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
import pycli
EXTTEXT_OPT = pycli.Option[list[str]]("extra", help="Add extra text", default="", nargs="+")
class Bar(pycli.Command):
short = "Prints 'bar'"
long = "Prints 'bar' to stdout"
opts = [
# defining predefined option
# it's value will be available from get method
EXTTEXT_OPT,
# defining name-referenced opt
# it's value will be available from get_by_name method
pycli.Option[int]("rc", help="Return code"),
]
def exec(self, vals: pycli.Values) -> int:
extra = vals.get(EXTTEXT_OPT)
if extra is None:
extra = []
print(f"bar {' '.join(extra)}")
rc = vals.get_by_name("rc", True)
if rc:
return rc
return 0
class Foo(pycli.Command):
short = "Prints 'foo'"
long = "Prints 'foo' to stdout"
# All attributes named like _opt_* are interpreted like local options
_opt_verbose = pycli.Option[bool](
"verbose", ["-v", "--verbose"], "Print more information", is_flag=True
)
opts = [
EXTTEXT_OPT
]
children = [Bar()]
def exec(self, vals: pycli.Values) -> int:
verbose = vals.get(self._opt_verbose)
if verbose:
print("Printing 'foo'")
print("foo")
return 0
if __name__ == "__main__":
rc = (
pycli.Application("example", "Example application made with PyCLI")
.with_commands(Foo(), Bar())
.run()
)
if rc != 0:
print(f"non-zero return code: {rc}")
exit(rc)