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 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 parser.description += self._gen_commands_list() | 185 parser.description += self._gen_commands_list() |
186 # Do not touch epilog. | 186 # Do not touch epilog. |
187 else: | 187 else: |
188 # Use the command's docstring if available. For commands, unlike module | 188 # Use the command's docstring if available. For commands, unlike module |
189 # docstring, realign. | 189 # docstring, realign. |
190 lines = (command.__doc__ or '').rstrip().splitlines() | 190 lines = (command.__doc__ or '').rstrip().splitlines() |
191 if lines[:1]: | 191 if lines[:1]: |
192 rest = textwrap.dedent('\n'.join(lines[1:])) | 192 rest = textwrap.dedent('\n'.join(lines[1:])) |
193 parser.description = '\n'.join((lines[0], rest)) | 193 parser.description = '\n'.join((lines[0], rest)) |
194 else: | 194 else: |
195 parser.description = lines[0] | 195 parser.description = lines[0] if lines else '' |
196 if parser.description: | 196 if parser.description: |
197 parser.description += '\n' | 197 parser.description += '\n' |
198 parser.epilog = getattr(command, 'epilog', None) | 198 parser.epilog = getattr(command, 'epilog', None) |
199 if parser.epilog: | 199 if parser.epilog: |
200 parser.epilog = '\n' + parser.epilog.strip() + '\n' | 200 parser.epilog = '\n' + parser.epilog.strip() + '\n' |
201 | 201 |
202 more = getattr(command, 'usage_more', '') | 202 more = getattr(command, 'usage_more', '') |
203 parser.set_usage( | 203 parser.set_usage( |
204 'usage: %%prog %s [options]%s' % (name, '' if not more else ' ' + more)) | 204 'usage: %%prog %s [options]%s' % (name, '' if not more else ' ' + more)) |
205 | 205 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 return command(parser, args[1:]) | 245 return command(parser, args[1:]) |
246 | 246 |
247 cmdhelp = self.enumerate_commands().get('help') | 247 cmdhelp = self.enumerate_commands().get('help') |
248 if cmdhelp: | 248 if cmdhelp: |
249 # Not a known command. Default to help. | 249 # Not a known command. Default to help. |
250 self._add_command_usage(parser, cmdhelp) | 250 self._add_command_usage(parser, cmdhelp) |
251 return cmdhelp(parser, args) | 251 return cmdhelp(parser, args) |
252 | 252 |
253 # Nothing can be done. | 253 # Nothing can be done. |
254 return 2 | 254 return 2 |
OLD | NEW |