-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskin_a_cat.py
66 lines (47 loc) · 1.33 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
"""
Demo of different ways to skin a cat.
"""
import shellish
##############
# Decorators #
##############
@shellish.autocommand
def cat1():
cat1.shell()
@shellish.autocommand
def sub1(optional:int=1):
print("ran subcommand1", optional)
cat1.add_subcommand(sub1)
###############
# Composition #
###############
cat2 = shellish.Command(name='cat2', doc='composition cat')
cat2.run = lambda args: cat2.shell()
sub2 = shellish.Command(name='sub2', doc='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 run(self, args):
self.shell()
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..
main = shellish.Command(name='main', doc='harness')
main.add_subcommand(cat1)
main.add_subcommand(cat2)
main.add_subcommand(Cat3)
main.shell()