@@ -78,7 +78,7 @@ def main(argv):
7878 import asyncio # pylint: disable=import-error,g-import-not-at-top # pytype: disable=import-error
7979
8080
81- def Fire (component = None , command = None , name = None ):
81+ def Fire (component = None , command = None , name = None , serialize = None ):
8282 """This function, Fire, is the main entrypoint for Python Fire.
8383
8484 Executes a command either from the `command` argument or from sys.argv by
@@ -164,7 +164,7 @@ def Fire(component=None, command=None, name=None):
164164 raise FireExit (0 , component_trace )
165165
166166 # The command succeeded normally; print the result.
167- _PrintResult (component_trace , verbose = component_trace .verbose )
167+ _PrintResult (component_trace , verbose = component_trace .verbose , serialize = serialize )
168168 result = component_trace .GetResult ()
169169 return result
170170
@@ -241,12 +241,19 @@ def _IsHelpShortcut(component_trace, remaining_args):
241241 return show_help
242242
243243
244- def _PrintResult (component_trace , verbose = False ):
244+ def _PrintResult (component_trace , verbose = False , serialize = None ):
245245 """Prints the result of the Fire call to stdout in a human readable way."""
246246 # TODO(dbieber): Design human readable deserializable serialization method
247247 # and move serialization to its own module.
248248 result = component_trace .GetResult ()
249249
250+ # Allow users to modify the return value of the component and provide
251+ # custom formatting.
252+ if serialize :
253+ if not callable (serialize ):
254+ raise FireError ("serialize argument {} must be empty or callable." .format (serialize ))
255+ result = serialize (result )
256+
250257 if value_types .HasCustomStr (result ):
251258 # If the object has a custom __str__ method, rather than one inherited from
252259 # object, then we use that to serialize the object.
0 commit comments