-
Notifications
You must be signed in to change notification settings - Fork 133
/
cli.py
145 lines (112 loc) · 4.28 KB
/
cli.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
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
#!/usr/bin/python
# Authors:
# Endi S. Dewata <edewata@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2015 Red Hat, Inc.
# All rights reserved.
#
import sys
import collections
class CLI(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.parent = None
self.verbose = False
self.modules = collections.OrderedDict()
def set_verbose(self, verbose):
self.verbose = verbose
if self.parent:
self.parent.set_verbose(verbose)
def get_full_name(self):
if self.parent:
return self.parent.get_full_module_name(self.name)
return self.name
def get_full_module_name(self, module_name):
return self.get_full_name() + '-' + module_name
def add_module(self, module):
self.modules[module.name] = module
module.parent = self
def get_module(self, name):
return self.modules.get(name)
def print_message(self, message):
print '-' * len(message)
print message
print '-' * len(message)
def print_help(self):
print 'Commands:'
for module in self.modules.itervalues():
full_name = module.get_full_name()
print ' {:30}{:30}'.format(full_name, module.description)
def init(self):
pass
def execute(self, args):
if len(args) == 0:
self.print_help()
sys.exit()
# A command consists of parts joined by dashes: <part 1>-<part 2>-...-<part N>.
# For example: cert-request-find
command = args[0]
# The command will be split into module name and sub command, for example:
# - module name: cert
# - sub command: request-find
module_name = None
sub_command = None
# Search the module by incrementally adding parts into module name.
# Repeat until it finds the module or until there is no more parts to add.
module = None
position = 0
while True:
# Find the next dash.
i = command.find('-', position)
if i >= 0:
# Dash found. Split command into module name and sub command.
module_name = command[0:i]
sub_command = command[i+1:]
else:
# Dash not found. Use the whole command.
module_name = command
sub_command = None
if self.verbose:
print 'Module: %s' % module_name
m = self.get_module(module_name)
if m:
# Module found. Check sub command.
if not sub_command:
# No sub command. Use this module.
module = m
break
# There is a sub command. It must be processed by module's children.
if len(m.modules) > 0:
# Module has children. Use this module.
module = m
break
# Module doesn't have children. Keep looking.
# If there's no more dashes, stop.
if i < 0:
break
position = i + 1
if not module:
raise Exception('Invalid module "%s".' % self.get_full_module_name(module_name))
# Prepare module arguments.
if sub_command:
# If module command exists, include it as arguments: <module command> <args>...
module_args = [sub_command] + args[1:]
else:
# Otherwise, pass the original arguments: <args>...
module_args = args[1:]
module.init()
module.execute(module_args)