96
97
help=_('What message information to return. Options are: %s') %
97
98
', '.join(choices))
98
99
self.parser.add_option_group(filters)
101
(self.options, args) = self.parser.parse_args()
100
(self.options, self.args) = self.parser.parse_args()
102
101
if self.options.commands:
103
102
self.print_help()
105
self.print_help(_('No command given'))
106
103
if self.options.files is None:
109
106
files = self.options.files.split(', ')
110
107
self.client = burrow.Client(url=self.options.url, config_files=files)
111
for section in self.sections:
112
if args[0] in section['commands']:
113
self.run_command(section, args[0], args[1:])
114
self.print_help(_('Command not found'))
116
def run_command(self, section, command, args):
110
'''Run the command given in arguments or enter an interactive shell.'''
111
if len(self.args) == 0:
112
for command in self._get_command():
113
self.run_command(command[0], command[1:])
115
self.run_command(self.args[0], self.args[1:])
117
def _get_command(self):
118
'''Get a command from stdin, printing a prompt out if stdin
119
is attached to a TTY.'''
121
if os.isatty(sys.stdin.fileno()):
124
# Try importing readline to make raw_input functionality more
131
command = raw_input(prompt)
133
if os.isatty(sys.stdin.fileno()):
136
command = command.split()
137
if len(command) == 0:
139
if command[0] == 'exit' or command[0] == 'quit':
143
def run_command(self, command, args):
144
'''Try running a command with the given arguments.'''
145
section = self._get_section(command)
147
print _('Command not found: %s') % command
117
149
if len(args) != len(section['args']):
118
self.print_help(_('Wrong number of arguments'))
150
print _('Wrong number of arguments')
119
152
if section.get('account', None):
120
153
args.insert(0, self.options.account)
121
154
if command in self.stdin_commands:
137
170
filters['match_hidden'] = self.options.all
138
171
args.append(filters)
139
172
getattr(self.client.backend, command)(*args)
174
def _get_section(self, command):
175
'''Lookup command in the defined command sections.'''
176
for section in self.sections:
177
if command in section['commands']:
142
181
def print_help(self, message=None):
182
'''Print the parser generated help along with burrow command help.'''
148
188
for section in self.sections:
149
189
print '%s commands:' % section['name']
150
190
for command in section['commands']:
152
192
if section.get('filters', None):
193
help_string += ' [filters]'
154
194
if command in self.attribute_commands:
155
help += ' [attributes]'
195
help_string += ' [attributes]'
156
196
for arg in section['args']:
157
help += ' <%s>' % arg
158
print ' %s%s' % (command, help)
197
help_string += ' <%s>' % arg
198
print ' %s%s' % (command, help_string)