OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Manages subcommands in a script. | 5 """Manages subcommands in a script. |
6 | 6 |
7 Each subcommand should look like this: | 7 Each subcommand should look like this: |
8 @usage('[pet name]') | 8 @usage('[pet name]') |
9 def CMDpet(parser, args): | 9 def CMDpet(parser, args): |
10 '''Prints a pet. | 10 '''Prints a pet. |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 cmds.setdefault('help', CMDhelp) | 121 cmds.setdefault('help', CMDhelp) |
122 return cmds | 122 return cmds |
123 | 123 |
124 def find_nearest_command(self, name_asked): | 124 def find_nearest_command(self, name_asked): |
125 """Retrieves the function to handle a command as supplied by the user. | 125 """Retrieves the function to handle a command as supplied by the user. |
126 | 126 |
127 It automatically tries to guess the _intended command_ by handling typos | 127 It automatically tries to guess the _intended command_ by handling typos |
128 and/or incomplete names. | 128 and/or incomplete names. |
129 """ | 129 """ |
130 commands = self.enumerate_commands() | 130 commands = self.enumerate_commands() |
131 if name_asked in commands: | 131 name_to_dash = name_asked.replace('_', '-') |
132 return commands[name_asked] | 132 if name_to_dash in commands: |
| 133 return commands[name_to_dash] |
133 | 134 |
134 # An exact match was not found. Try to be smart and look if there's | 135 # An exact match was not found. Try to be smart and look if there's |
135 # something similar. | 136 # something similar. |
136 commands_with_prefix = [c for c in commands if c.startswith(name_asked)] | 137 commands_with_prefix = [c for c in commands if c.startswith(name_asked)] |
137 if len(commands_with_prefix) == 1: | 138 if len(commands_with_prefix) == 1: |
138 return commands[commands_with_prefix[0]] | 139 return commands[commands_with_prefix[0]] |
139 | 140 |
140 # A #closeenough approximation of levenshtein distance. | 141 # A #closeenough approximation of levenshtein distance. |
141 def close_enough(a, b): | 142 def close_enough(a, b): |
142 return difflib.SequenceMatcher(a=a, b=b).ratio() | 143 return difflib.SequenceMatcher(a=a, b=b).ratio() |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 return command(parser, args[1:]) | 252 return command(parser, args[1:]) |
252 | 253 |
253 cmdhelp = self.enumerate_commands().get('help') | 254 cmdhelp = self.enumerate_commands().get('help') |
254 if cmdhelp: | 255 if cmdhelp: |
255 # Not a known command. Default to help. | 256 # Not a known command. Default to help. |
256 self._add_command_usage(parser, cmdhelp) | 257 self._add_command_usage(parser, cmdhelp) |
257 return cmdhelp(parser, args) | 258 return cmdhelp(parser, args) |
258 | 259 |
259 # Nothing can be done. | 260 # Nothing can be done. |
260 return 2 | 261 return 2 |
OLD | NEW |