| 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 29 matching lines...) Expand all Loading... |
| 40 name = None | 40 name = None |
| 41 show_in_main_help = False | 41 show_in_main_help = False |
| 42 help_text = None | 42 help_text = None |
| 43 argument_names = None | 43 argument_names = None |
| 44 long_help = None | 44 long_help = None |
| 45 | 45 |
| 46 def __init__(self, options=None, requires_local_commits=False): | 46 def __init__(self, options=None, requires_local_commits=False): |
| 47 self.required_arguments = self._parse_required_arguments(self.argument_n
ames) | 47 self.required_arguments = self._parse_required_arguments(self.argument_n
ames) |
| 48 self.options = options | 48 self.options = options |
| 49 self.requires_local_commits = requires_local_commits | 49 self.requires_local_commits = requires_local_commits |
| 50 self._tool = None | |
| 51 # option_parser can be overridden by the tool using set_option_parser | 50 # option_parser can be overridden by the tool using set_option_parser |
| 52 # This default parser will be used for standalone_help printing. | 51 # This default parser will be used for standalone_help printing. |
| 53 self.option_parser = HelpPrintingOptionParser( | 52 self.option_parser = HelpPrintingOptionParser( |
| 54 usage=optparse.SUPPRESS_USAGE, | 53 usage=optparse.SUPPRESS_USAGE, |
| 55 add_help_option=False, | 54 add_help_option=False, |
| 56 option_list=self.options) | 55 option_list=self.options) |
| 57 | 56 |
| 58 def _exit(self, code): | 57 def _exit(self, code): |
| 59 sys.exit(code) | 58 sys.exit(code) |
| 60 | 59 |
| 61 # This design is slightly awkward, but we need the | 60 # This design is slightly awkward, but we need the |
| 62 # the tool to be able to create and modify the option_parser | 61 # the tool to be able to create and modify the option_parser |
| 63 # before it knows what Command to run. | 62 # before it knows what Command to run. |
| 64 def set_option_parser(self, option_parser): | 63 def set_option_parser(self, option_parser): |
| 65 self.option_parser = option_parser | 64 self.option_parser = option_parser |
| 66 self._add_options_to_parser() | 65 self._add_options_to_parser() |
| 67 | 66 |
| 68 def _add_options_to_parser(self): | 67 def _add_options_to_parser(self): |
| 69 options = self.options or [] | 68 options = self.options or [] |
| 70 for option in options: | 69 for option in options: |
| 71 self.option_parser.add_option(option) | 70 self.option_parser.add_option(option) |
| 72 | 71 |
| 73 # The tool calls bind_to_tool on each Command after adding it to its list. | |
| 74 def bind_to_tool(self, tool): | |
| 75 # Command instances can only be bound to one tool at a time. | |
| 76 if self._tool and tool != self._tool: | |
| 77 raise Exception("Command already bound to tool!") | |
| 78 self._tool = tool | |
| 79 | |
| 80 @staticmethod | 72 @staticmethod |
| 81 def _parse_required_arguments(argument_names): | 73 def _parse_required_arguments(argument_names): |
| 82 required_args = [] | 74 required_args = [] |
| 83 if not argument_names: | 75 if not argument_names: |
| 84 return required_args | 76 return required_args |
| 85 split_args = argument_names.split(" ") | 77 split_args = argument_names.split(" ") |
| 86 for argument in split_args: | 78 for argument in split_args: |
| 87 if argument[0] == '[': | 79 if argument[0] == '[': |
| 88 # For now our parser is rather dumb. Do some minimal validation
that | 80 # For now our parser is rather dumb. Do some minimal validation
that |
| 89 # we haven't confused it. | 81 # we haven't confused it. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 # 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: |
| 148 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() |
| 149 self.exit(1, error_message) | 141 self.exit(1, error_message) |
| 150 | 142 |
| 151 # 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 |
| 152 # 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). |
| 153 def format_epilog(self, epilog): # pylint: disable=unused-argument | 145 def format_epilog(self, epilog): # pylint: disable=unused-argument |
| 154 if self.epilog_method: | 146 if self.epilog_method: |
| 155 return "\n%s\n" % self.epilog_method() | 147 return "\n%s\n" % self.epilog_method() |
| 156 return "" | 148 return "" |
| OLD | NEW |