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
3 # found in the LICENSE file.
5 """An option parser which handles the first arg as a command.
7 Add other nice functionality such as printing a list of commands
8 and an example in usage.
15 class CommandOptionParser(optparse
.OptionParser
):
16 """Wrapper class for OptionParser to help with listing commands."""
18 def __init__(self
, *args
, **kwargs
):
19 """Creates a CommandOptionParser.
22 commands_dict: A dictionary mapping command strings to an object defining
23 - add_options_func: Adds options to the option parser
24 - run_command_func: Runs the command itself.
25 example: An example command.
26 everything else: Passed to optparse.OptionParser contructor.
28 self
.commands_dict
= kwargs
.pop('commands_dict', {})
29 self
.example
= kwargs
.pop('example', '')
30 if not 'usage' in kwargs
:
31 kwargs
['usage'] = 'Usage: %prog <command> [options]'
32 optparse
.OptionParser
.__init
__(self
, *args
, **kwargs
)
36 normal_usage
= optparse
.OptionParser
.get_usage(self
)
37 command_list
= self
.get_command_list()
38 example
= self
.get_example()
39 return self
.expand_prog_name(normal_usage
+ example
+ command_list
)
42 def get_command_list(self
):
43 if self
.commands_dict
.keys():
44 return '\nCommands:\n %s\n' % '\n '.join(
45 sorted(self
.commands_dict
.keys()))
48 def get_example(self
):
50 return '\nExample:\n %s\n' % self
.example
54 def ParseAndExecute(option_parser
, argv
=None):
55 """Parses options/args from argv and runs the specified command.
58 option_parser: A CommandOptionParser object.
59 argv: Command line arguments. If None, automatically draw from sys.argv.
67 if len(argv
) < 2 or argv
[1] not in option_parser
.commands_dict
:
68 # Parse args first, if this is '--help', optparse will print help and exit
69 option_parser
.parse_args(argv
)
70 option_parser
.error('Invalid command.')
72 cmd
= option_parser
.commands_dict
[argv
[1]]
73 cmd
.add_options_func(option_parser
)
74 options
, args
= option_parser
.parse_args(argv
)
75 return cmd
.run_command_func(argv
[1], options
, args
, option_parser
)