@@ -230,7 +230,35 @@ class sets this value unconditionally.
230230
231231def dispatch (command_class , argv = sys .argv , input_file = sys .stdin , output_file =
232232 sys .stdout , module_name = None ):
233- """ TODO: Documentation
233+ """ Dispatches a search command
234+
235+ This function implements a [conditional script stanza](http://goo.gl/OFaox6)
236+ based on the value of `module_name`. If you would like the module calling
237+ this function to act as either a reusable module or a standalone program,
238+ call it at module scope and pass `__name__` as the value of `module_name`.
239+ Otherwise, if you wish this function to unconditionally execute a command,
240+ set the value of `module_name` to `None`. This is the default.
241+
242+ **Example:**
243+
244+ ```python
245+ #!/usr/bin/env python
246+ ...
247+ class CountMatchesCommand(StreamingCommand):
248+ ...
249+
250+ dispatch(CountMatchesCommand, module_name=__name__)
251+ ```
252+
253+ Dispatches the CountMatchesCommand, if and only if `__name__` is equal to
254+ `__main__`.
255+
256+ :param command_class: Search command class to instantiate and execute.
257+ :param argv: List of arguments to the command.
258+ :param input_file: File-like object from which the command will read data.
259+ :param output_file: File-like object to which the command will write data.
260+ :param module_name: Name of module calling dispatch (the value contained in
261+ `__name__`) or `None`.
234262
235263 """
236264 if module_name is not None and module_name != '__main__' :
@@ -239,8 +267,8 @@ def dispatch(command_class, argv=sys.argv, input_file=sys.stdin, output_file=
239267 try :
240268 command_class ().process (argv , input_file , output_file )
241269 except :
242- import logging , traceback
243-
270+ import logging
271+ import traceback
244272 logging .fatal (traceback .format_exc ())
245273
246274 return
0 commit comments