| OLD | NEW |
| 1 # Copyright (c) 2016 Google Inc. All rights reserved. | 1 # Copyright (c) 2016 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 if self.long_help: | 114 if self.long_help: |
| 115 help_text += "%s\n\n" % self.long_help | 115 help_text += "%s\n\n" % self.long_help |
| 116 help_text += self.option_parser.format_option_help(optparse.IndentedHelp
Formatter()) | 116 help_text += self.option_parser.format_option_help(optparse.IndentedHelp
Formatter()) |
| 117 return help_text | 117 return help_text |
| 118 | 118 |
| 119 def execute(self, options, args, tool): | 119 def execute(self, options, args, tool): |
| 120 raise NotImplementedError("subclasses must implement") | 120 raise NotImplementedError("subclasses must implement") |
| 121 | 121 |
| 122 # main() exists so that Commands can be turned into stand-alone scripts. | 122 # main() exists so that Commands can be turned into stand-alone scripts. |
| 123 # Other parts of the code will likely require modification to work stand-alo
ne. | 123 # Other parts of the code will likely require modification to work stand-alo
ne. |
| 124 def main(self, args=sys.argv): | 124 def main(self, args=None): |
| 125 (options, args) = self.parse_args(args) | 125 options, args = self.parse_args(args) |
| 126 # Some commands might require a dummy tool | 126 # Some commands might require a dummy tool |
| 127 return self.check_arguments_and_execute(options, args) | 127 return self.check_arguments_and_execute(options, args) |
| 128 | 128 |
| 129 | 129 |
| 130 class HelpPrintingOptionParser(optparse.OptionParser): | 130 class HelpPrintingOptionParser(optparse.OptionParser): |
| 131 | 131 |
| 132 def __init__(self, epilog_method=None, *args, **kwargs): | 132 def __init__(self, epilog_method=None, *args, **kwargs): |
| 133 self.epilog_method = epilog_method | 133 self.epilog_method = epilog_method |
| 134 optparse.OptionParser.__init__(self, *args, **kwargs) | 134 optparse.OptionParser.__init__(self, *args, **kwargs) |
| 135 | 135 |
| 136 def error(self, msg): | 136 def error(self, msg): |
| 137 self.print_usage(sys.stderr) | 137 self.print_usage(sys.stderr) |
| 138 error_message = "%s: error: %s\n" % (self.get_prog_name(), msg) | 138 error_message = "%s: error: %s\n" % (self.get_prog_name(), msg) |
| 139 # This method is overridden to add this one line to the output: | 139 # This method is overridden to add this one line to the output: |
| 140 error_message += "\nType \"%s --help\" to see usage.\n" % self.get_prog_
name() | 140 error_message += "\nType \"%s --help\" to see usage.\n" % self.get_prog_
name() |
| 141 self.exit(1, error_message) | 141 self.exit(1, error_message) |
| 142 | 142 |
| 143 # We override format_epilog to avoid the default formatting which would para
graph-wrap the epilog | 143 # We override format_epilog to avoid the default formatting which would para
graph-wrap the epilog |
| 144 # and also to allow us to compute the epilog lazily instead of in the constr
uctor (allowing it to be context sensitive). | 144 # and also to allow us to compute the epilog lazily instead of in the constr
uctor (allowing it to be context sensitive). |
| 145 def format_epilog(self, epilog): # pylint: disable=unused-argument | 145 def format_epilog(self, epilog): # pylint: disable=unused-argument |
| 146 if self.epilog_method: | 146 if self.epilog_method: |
| 147 return "\n%s\n" % self.epilog_method() | 147 return "\n%s\n" % self.epilog_method() |
| 148 return "" | 148 return "" |
| OLD | NEW |