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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 for fn in dir(self.module) if fn.startswith('CMD')) | 112 for fn in dir(self.module) if fn.startswith('CMD')) |
113 cmds.setdefault('help', CMDhelp) | 113 cmds.setdefault('help', CMDhelp) |
114 return cmds | 114 return cmds |
115 | 115 |
116 def find_nearest_command(self, name): | 116 def find_nearest_command(self, name): |
117 """Retrieves the function to handle a command. | 117 """Retrieves the function to handle a command. |
118 | 118 |
119 It automatically tries to guess the intended command by handling typos or | 119 It automatically tries to guess the intended command by handling typos or |
120 incomplete names. | 120 incomplete names. |
121 """ | 121 """ |
| 122 # Implicitly replace foo-bar to foo_bar since foo-bar is not a valid python |
| 123 # symbol but it's faster to type. |
| 124 name = name.replace('-', '_') |
122 commands = self.enumerate_commands() | 125 commands = self.enumerate_commands() |
123 if name in commands: | 126 if name in commands: |
124 return commands[name] | 127 return commands[name] |
125 | 128 |
126 # An exact match was not found. Try to be smart and look if there's | 129 # An exact match was not found. Try to be smart and look if there's |
127 # something similar. | 130 # something similar. |
128 commands_with_prefix = [c for c in commands if c.startswith(name)] | 131 commands_with_prefix = [c for c in commands if c.startswith(name)] |
129 if len(commands_with_prefix) == 1: | 132 if len(commands_with_prefix) == 1: |
130 return commands[commands_with_prefix[0]] | 133 return commands[commands_with_prefix[0]] |
131 | 134 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 return command(parser, args[1:]) | 245 return command(parser, args[1:]) |
243 | 246 |
244 cmdhelp = self.enumerate_commands().get('help') | 247 cmdhelp = self.enumerate_commands().get('help') |
245 if cmdhelp: | 248 if cmdhelp: |
246 # Not a known command. Default to help. | 249 # Not a known command. Default to help. |
247 self._add_command_usage(parser, cmdhelp) | 250 self._add_command_usage(parser, cmdhelp) |
248 return cmdhelp(parser, args) | 251 return cmdhelp(parser, args) |
249 | 252 |
250 # Nothing can be done. | 253 # Nothing can be done. |
251 return 2 | 254 return 2 |
OLD | NEW |