• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Module to hold the Command plugin."""
5
6import argparse
7
8import cr
9
10
11class Command(cr.Plugin, cr.Plugin.Type):
12  """Base class for implementing cr commands.
13
14  These are the sub-commands on the command line, and modify the
15  accepted remaining arguments.
16  Commands in general do not implement the functionality directly, instead they
17  run a sequence of actions.
18  """
19
20  @classmethod
21  def Select(cls, context):
22    """Called to select which command is active.
23
24    This picks a command based on the first non - argument on the command
25    line.
26    Args:
27      context: The context to select the command for.
28    Returns:
29      the selected command, or None if not specified on the command line.
30    """
31    if context.args:
32      return getattr(context.args, '_command', None)
33    return None
34
35  def __init__(self):
36    super(Command, self).__init__()
37    self.help = 'Missing help: {0}'.format(self.__class__.__name__)
38    self.description = None
39    self.epilog = None
40    self.parser = None
41    self.requires_build_dir = True
42
43  def AddArguments(self, subparsers):
44    """Add arguments to the command line parser.
45
46    Called by the main function to add the command to the command line parser.
47    Commands that override this function to add more arguments must invoke
48    this method.
49    Args:
50      subparsers: The argparse subparser manager to add this command to.
51    Returns:
52      the parser that was built for the command.
53    """
54    self.parser = subparsers.add_parser(
55        self.name,
56        add_help=False,
57        help=self.help,
58        description=self.description or self.help,
59        epilog=self.epilog,
60    )
61    self.parser.set_defaults(_command=self)
62    cr.Context.AddCommonArguments(self.parser)
63    cr.base.client.AddArguments(self.parser)
64    return self.parser
65
66  def ConsumeArgs(self, parser, reason):
67    """Adds a remaining argument consumer to the parser.
68
69    A helper method that commands can use to consume all remaining arguments.
70    Use for things like lists of targets.
71    Args:
72      parser: The parser to consume remains for.
73      reason: The reason to give the user in the help text.
74    """
75    parser.add_argument(
76        '_remains', metavar='arguments',
77        nargs=argparse.REMAINDER,
78        help='The additional arguments to {0}.'.format(reason)
79    )
80
81  def EarlyArgProcessing(self, context):
82    """Called to make decisions based on speculative argument parsing.
83
84    When this method is called, enough of the command line parsing has been
85    done that the command is selected. This allows the command to make any
86    modifications needed before the final argument parsing is done.
87
88    Args:
89      context: The context that is parsing the arguments.
90    """
91    cr.base.client.ApplyOutArgument(context)
92
93  @cr.Plugin.activemethod
94  def Run(self, context):
95    """The main method of the command.
96
97    This is the only thing that a command has to implement, and it should not
98    call this base version.
99    Args:
100      context: The context to run the command in.
101    """
102    _ = context
103    raise NotImplementedError('Must be overridden.')
104
105