-
Notifications
You must be signed in to change notification settings - Fork 0
/
skin_a_cat.py
71 lines (48 loc) · 1.36 KB
/
skin_a_cat.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
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
"""
Demo of different ways to skin a cat.
"""
import shellish
##############
# Decorators #
##############
@shellish.autocommand
def cat1():
""" Auto command cannot interact. """
pass
@shellish.autocommand
def sub1(optional: int=1):
print("ran subcommand1", optional)
cat1.add_subcommand(sub1)
###############
# Composition #
###############
cat2 = shellish.Command(name='cat2', title='composition cat')
sub2 = shellish.Command(name='sub2', title='composition cat sub')
sub2.add_argument('--optional', type=int, default=2)
sub2.run = lambda args: print("ran subcommand2", args.optional)
cat2.add_subcommand(sub2)
###############
# Inheritance #
###############
class Cat3(shellish.Command):
""" Inheritance cat. """
name = 'cat3'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_subcommand(Sub3)
class Sub3(shellish.Command):
""" Inheritance cat sub. """
name = 'sub3'
def setup_args(self, parser):
self.add_argument('--optional', type=int, default=3)
def run(self, args):
print("ran subcommand3", args.optional)
# Putting it together for a demo..
class Main(shellish.Command):
def run(self, args):
self.session.run_loop()
main = Main(name='main', title='harness')
main.add_subcommand(cat1)
main.add_subcommand(cat2)
main.add_subcommand(Cat3)
main()