Skip to content

Commit 59dd4e0

Browse files
dbiebercopybara-github
authored andcommitted
Adds support for groups to helptext and removes showing trace by default in favor of just showing a single line error by default.
PiperOrigin-RevId: 252074360 Change-Id: I0e99eb6b07f47815dce6e9098998b139e9e61f86
1 parent 93431c7 commit 59dd4e0

4 files changed

Lines changed: 49 additions & 30 deletions

File tree

‎fire/core.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def _IsHelpShortcut(component_trace, remaining_args):
240240
def _PrintResult(component_trace, verbose=False):
241241
"""Prints the result of the Fire call to stdout in a human readable way."""
242242
# TODO(dbieber): Design human readable deserializable serialization method
243-
# and move serialization to it's own module.
243+
# and move serialization to its own module.
244244
result = component_trace.GetResult()
245245

246246
if isinstance(result, (list, set, types.GeneratorType)):
@@ -267,7 +267,7 @@ def _DisplayError(component_trace):
267267
message = 'INFO: Showing help with the command {cmd}.\n'.format(
268268
cmd=pipes.quote(command))
269269
output.append(message)
270-
output.append('Fire trace:\n{trace}\n'.format(trace=component_trace))
270+
output.append('ERROR: ' + component_trace.elements[-1].ErrorAsStr())
271271
result = component_trace.GetResult()
272272
help_string = helputils.HelpString(result, component_trace,
273273
component_trace.verbose)

‎fire/helptext.py‎

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,9 @@ def HelpTextForObject(component, info, trace=None, verbose=False):
324324
possible_flags = ''
325325

326326
if groups:
327-
# TODO(joejoevictor): Add missing GROUPS section handling
328327
possible_actions.append('GROUP')
328+
usage_details_section = GroupUsageDetailsSection(groups)
329+
usage_details_sections.append(usage_details_section)
329330
if commands:
330331
possible_actions.append('COMMAND')
331332
usage_details_section = CommandUsageDetailsSection(commands)
@@ -362,6 +363,36 @@ def HelpTextForObject(component, info, trace=None, verbose=False):
362363
)
363364

364365

366+
def GroupUsageDetailsSection(groups):
367+
"""Creates a section tuple for the groups section of the usage details."""
368+
group_item_strings = []
369+
for group_name, group in groups:
370+
group_info = inspectutils.Info(group)
371+
group_item = group_name
372+
if 'docstring_info' in group_info:
373+
group_docstring_info = group_info['docstring_info']
374+
if group_docstring_info and group_docstring_info.summary:
375+
group_item = _CreateItem(group_name,
376+
group_docstring_info.summary)
377+
group_item_strings.append(group_item)
378+
return ('GROUPS', _NewChoicesSection('GROUP', group_item_strings))
379+
380+
381+
def CommandUsageDetailsSection(commands):
382+
"""Creates a section tuple for the commands section of the usage details."""
383+
command_item_strings = []
384+
for command_name, command in commands:
385+
command_info = inspectutils.Info(command)
386+
command_item = command_name
387+
if 'docstring_info' in command_info:
388+
command_docstring_info = command_info['docstring_info']
389+
if command_docstring_info and command_docstring_info.summary:
390+
command_item = _CreateItem(command_name,
391+
command_docstring_info.summary)
392+
command_item_strings.append(command_item)
393+
return ('COMMANDS', _NewChoicesSection('COMMAND', command_item_strings))
394+
395+
365396
def ValuesUsageDetailsSection(component, values):
366397
"""Creates a section tuple for the values section of the usage details."""
367398
value_item_strings = []
@@ -379,24 +410,9 @@ def ValuesUsageDetailsSection(component, values):
379410
return ('VALUES', _NewChoicesSection('VALUE', value_item_strings))
380411

381412

382-
def CommandUsageDetailsSection(commands):
383-
"""Creates a section tuple for the commands section of the usage details."""
384-
command_item_strings = []
385-
for command_name, command in commands:
386-
command_info = inspectutils.Info(command)
387-
command_item = formatting.Bold(command_name)
388-
if 'docstring_info' in command_info:
389-
command_docstring_info = command_info['docstring_info']
390-
if command_docstring_info and command_docstring_info.summary:
391-
command_item = _CreateItem(
392-
formatting.Bold(command_name), command_docstring_info.summary)
393-
command_item_strings.append(command_item)
394-
return ('COMMANDS', _NewChoicesSection('COMMAND', command_item_strings))
395-
396-
397413
def _NewChoicesSection(name, choices):
398414
return _CreateItem(
399-
'{name} is one of the followings:'.format(
415+
'{name} is one of the following:'.format(
400416
name=formatting.Bold(formatting.Underline(name))),
401417
'\n' + '\n\n'.join(choices),
402418
indent=1)

‎fire/helptext_test.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def testHelpTextNoDefaultsObject(self):
5656
self.assertIn('NAME\n NoDefaults', help_screen)
5757
self.assertIn('SYNOPSIS\n NoDefaults COMMAND', help_screen)
5858
self.assertNotIn('DESCRIPTION', help_screen)
59-
self.assertIn('COMMANDS\n COMMAND is one of the followings:',
59+
self.assertIn('COMMANDS\n COMMAND is one of the following:',
6060
help_screen)
6161
self.assertIn('double', help_screen)
6262
self.assertIn('triple', help_screen)
@@ -129,7 +129,7 @@ def testHelpTextEmptyList(self):
129129
self.assertIn('DESCRIPTION\n', help_screen)
130130
# We don't check the listed commands either since the list API could
131131
# potentially change between Python versions.
132-
self.assertIn('COMMANDS\n COMMAND is one of the followings:\n',
132+
self.assertIn('COMMANDS\n COMMAND is one of the following:\n',
133133
help_screen)
134134

135135
def testHelpTextShortList(self):
@@ -148,7 +148,7 @@ def testHelpTextShortList(self):
148148
# We don't check the listed commands comprehensively since the list API
149149
# could potentially change between Python versions. Check a few
150150
# functions(command) that we're confident likely remain available.
151-
self.assertIn('COMMANDS\n COMMAND is one of the followings:\n',
151+
self.assertIn('COMMANDS\n COMMAND is one of the following:\n',
152152
help_screen)
153153
self.assertIn(' append\n', help_screen)
154154

@@ -160,9 +160,9 @@ def testHelpTextInt(self):
160160
self.assertIn('NAME\n 7', help_screen)
161161
self.assertIn('SYNOPSIS\n 7 COMMAND | VALUE', help_screen)
162162
self.assertIn('DESCRIPTION\n', help_screen)
163-
self.assertIn('COMMANDS\n COMMAND is one of the followings:\n',
163+
self.assertIn('COMMANDS\n COMMAND is one of the following:\n',
164164
help_screen)
165-
self.assertIn('VALUES\n VALUE is one of the followings:\n', help_screen)
165+
self.assertIn('VALUES\n VALUE is one of the following:\n', help_screen)
166166

167167
def testHelpTextNoInit(self):
168168
component = tc.OldStyleEmpty
@@ -190,13 +190,13 @@ def testHelpScreen(self):
190190
This is some detail description of this test class.
191191
192192
COMMANDS
193-
COMMAND is one of the followings:
193+
COMMAND is one of the following:
194194
195195
print_msg
196196
Prints a message.
197197
198198
VALUES
199-
VALUE is one of the followings:
199+
VALUE is one of the following:
200200
201201
message
202202
The default message to print.
@@ -273,7 +273,7 @@ def testHelpTextBoldCommandName(self):
273273
formatting.Bold('NAME') + '\n ClassWithDocstring', help_screen)
274274
self.assertIn(formatting.Bold('COMMANDS') + '\n', help_screen)
275275
self.assertIn(
276-
formatting.BoldUnderline('COMMAND') + ' is one of the followings:\n',
276+
formatting.BoldUnderline('COMMAND') + ' is one of the following:\n',
277277
help_screen)
278278
self.assertIn(formatting.Bold('print_msg') + '\n', help_screen)
279279

‎fire/trace.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,13 @@ def HasSeparator(self):
291291
def AddSeparator(self):
292292
self._separator = True
293293

294+
def ErrorAsStr(self):
295+
return ' '.join(str(arg) for arg in self._error.args)
296+
294297
def __str__(self):
295-
if not self.HasError():
298+
if self.HasError():
299+
return self.ErrorAsStr()
300+
else:
296301
# Format is: {action} "{target}" ({filename}:{lineno})
297302
string = self._action
298303
if self._target is not None:
@@ -304,5 +309,3 @@ def __str__(self):
304309

305310
string += ' ({path})'.format(path=path)
306311
return string
307-
else:
308-
return str(self._error)

0 commit comments

Comments
 (0)