Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: build/android/pylib/utils/command_option_parser.py

Issue 20210002: [Android] Sets up a coverage system for java using EMMA (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Break apart native and java coverage flags for gyp Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 """An option parser which handles the first arg as a command.
6
7 Add other nice functionality such as printing a list of commands
8 and an example in usage.
9 """
10
11 import optparse
12 import sys
13
14
15 class CommandOptionParser(optparse.OptionParser):
16 """Wrapper class for OptionParser to help with listing commands."""
17
18 def __init__(self, *args, **kwargs):
19 """Creates a CommandOptionParser.
20
21 Args:
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.
27 """
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)
33
34 #override
35 def get_usage(self):
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)
40
41 #override
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()))
46 return ''
47
48 def get_example(self):
49 if self.example:
50 return '\nExample:\n %s\n' % self.example
51 return ''
52
53
54 def ParseAndExecute(option_parser, argv=None):
55 """Parses options/args from argv and runs the specified command.
56
57 Args:
58 option_parser: A CommandOptionParser object.
59 argv: Command line arguments. If None, automatically draw from sys.argv.
frankf 2013/08/13 23:33:39 2 spaces, same below
frankf 2013/08/13 23:33:39 Can you just pass argv everywhere instead of relyi
gkanwar1 2013/08/15 19:49:20 Done.
gkanwar1 2013/08/15 19:49:20 I did this to parallel optparser.OptionParser.pars
60
61 Returns:
62 An exit code.
63 """
64 if not argv:
65 argv = sys.argv
66
67 if len(argv) < 2 or argv[1] not in option_parser.commands_dict:
68 option_parser.error('Invalid command.')
69
70 command = argv[1]
71 option_parser.commands_dict[command].add_options_func(option_parser)
72 options, args = option_parser.parse_args(argv)
73 return option_parser.commands_dict[command].run_command_func(
74 command, options, args, option_parser)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698