| 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 18 matching lines...) Expand all Loading... |
| 29 import optparse | 29 import optparse |
| 30 | 30 |
| 31 from webkitpy.tool.commands.command import Command | 31 from webkitpy.tool.commands.command import Command |
| 32 | 32 |
| 33 | 33 |
| 34 class HelpCommand(Command): | 34 class HelpCommand(Command): |
| 35 name = "help" | 35 name = "help" |
| 36 help_text = "Display information about this program or its subcommands" | 36 help_text = "Display information about this program or its subcommands" |
| 37 argument_names = "[COMMAND]" | 37 argument_names = "[COMMAND]" |
| 38 | 38 |
| 39 def __init__(self): | 39 def __init__(self, tool=None): |
| 40 options = [ | 40 options = [ |
| 41 optparse.make_option( | 41 optparse.make_option( |
| 42 "-a", | 42 "-a", |
| 43 "--all-commands", | 43 "--all-commands", |
| 44 action="store_true", | 44 action="store_true", |
| 45 dest="show_all_commands", | 45 dest="show_all_commands", |
| 46 help="Print all available commands"), | 46 help="Print all available commands"), |
| 47 ] | 47 ] |
| 48 super(HelpCommand, self).__init__(options) | 48 super(HelpCommand, self).__init__(options) |
| 49 # A hack used to pass --all-commands to _help_epilog even though it's ca
lled by the OptionParser. | 49 # A hack used to pass --all-commands to help_epilog even though it's cal
led by the OptionParser. |
| 50 self.show_all_commands = False | 50 self.show_all_commands = False |
| 51 self._tool = None | 51 # self._tool is used in help_epilog, so it's passed in the initializer r
ather than set in the execute method. |
| 52 self._tool = tool |
| 52 | 53 |
| 53 def _help_epilog(self): | 54 def help_epilog(self): |
| 54 # Only show commands which are relevant to this checkout's SCM system.
Might this be confusing to some users? | 55 # Only show commands which are relevant to this checkout's SCM system.
Might this be confusing to some users? |
| 55 if self.show_all_commands: | 56 if self.show_all_commands: |
| 56 epilog = "All %prog commands:\n" | 57 epilog = "All %prog commands:\n" |
| 57 relevant_commands = self._tool.commands[:] | 58 relevant_commands = self._tool.commands[:] |
| 58 else: | 59 else: |
| 59 epilog = "Common %prog commands:\n" | 60 epilog = "Common %prog commands:\n" |
| 60 relevant_commands = filter(self._tool.should_show_in_main_help, self
._tool.commands) | 61 relevant_commands = filter(self._tool.should_show_in_main_help, self
._tool.commands) |
| 61 longest_name_length = max(len(command.name) for command in relevant_comm
ands) | 62 longest_name_length = max(len(command.name) for command in relevant_comm
ands) |
| 62 relevant_commands.sort(lambda a, b: cmp(a.name, b.name)) | 63 relevant_commands.sort(lambda a, b: cmp(a.name, b.name)) |
| 63 command_help_texts = [" %s %s\n" % (command.name.ljust(longest_name_
length), command.help_text) | 64 command_help_texts = [" %s %s\n" % (command.name.ljust(longest_name_
length), command.help_text) |
| 64 for command in relevant_commands] | 65 for command in relevant_commands] |
| 65 epilog += "%s\n" % "".join(command_help_texts) | 66 epilog += "%s\n" % "".join(command_help_texts) |
| 66 epilog += "See '%prog help --all-commands' to list all commands.\n" | 67 epilog += "See '%prog help --all-commands' to list all commands.\n" |
| 67 epilog += "See '%prog help COMMAND' for more information on a specific c
ommand.\n" | 68 epilog += "See '%prog help COMMAND' for more information on a specific c
ommand.\n" |
| 68 return epilog.replace("%prog", self._tool.name()) # Use of %prog here m
imics OptionParser.expand_prog_name(). | 69 return epilog.replace("%prog", self._tool.name()) # Use of %prog here m
imics OptionParser.expand_prog_name(). |
| 69 | 70 |
| 70 # FIXME: This is a hack so that we don't show --all-commands as a global opt
ion: | 71 # FIXME: This is a hack so that we don't show --all-commands as a global opt
ion: |
| 71 def _remove_help_options(self): | 72 def _remove_help_options(self): |
| 72 for option in self.options: | 73 for option in self.options: |
| 73 self.option_parser.remove_option(option.get_opt_string()) | 74 self.option_parser.remove_option(option.get_opt_string()) |
| 74 | 75 |
| 75 def execute(self, options, args, tool): | 76 def execute(self, options, args, tool): |
| 76 self._tool = tool | |
| 77 if args: | 77 if args: |
| 78 command = self._tool.command_by_name(args[0]) | 78 command = self._tool.command_by_name(args[0]) |
| 79 if command: | 79 if command: |
| 80 print command.standalone_help() | 80 print command.standalone_help() |
| 81 return 0 | 81 return 0 |
| 82 | 82 |
| 83 self.show_all_commands = options.show_all_commands | 83 self.show_all_commands = options.show_all_commands |
| 84 self._remove_help_options() | 84 self._remove_help_options() |
| 85 self.option_parser.print_help() | 85 self.option_parser.print_help() |
| 86 return 0 | 86 return 0 |
| OLD | NEW |