Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS 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 | import sys |
| 6 | |
Chris Sosa | 12a0918 | 2012-10-11 13:42:39 -0700 | [diff] [blame] | 7 | # Older versions of Python might be missing argparse. |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 8 | try: |
| 9 | import argparse |
| 10 | except ImportError: |
Chris Sosa | 12a0918 | 2012-10-11 13:42:39 -0700 | [diff] [blame] | 11 | from chromite.third_party import argparse |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 12 | |
| 13 | from chromite.cros import commands |
| 14 | |
| 15 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 16 | def GetOptions(my_commands): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 17 | """Returns the argparse to use for Cros.""" |
| 18 | parser = argparse.ArgumentParser() |
| 19 | if not commands: |
| 20 | return parser |
| 21 | |
| 22 | subparsers = parser.add_subparsers(title='cros commands') |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 23 | for cmd_name, class_def in my_commands.iteritems(): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 24 | sub_parser = subparsers.add_parser(cmd_name, help=class_def.__doc__) |
| 25 | class_def.AddParser(sub_parser) |
| 26 | |
| 27 | return parser |
| 28 | |
| 29 | |
| 30 | def main(args): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 31 | parser = GetOptions(commands.ListCommands()) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 32 | # Cros currently does nothing without a subcmd. Print help if no args are |
| 33 | # specified. |
| 34 | if not args: |
| 35 | parser.print_help() |
| 36 | return 1 |
| 37 | |
| 38 | namespace = parser.parse_args(args) |
| 39 | subcommand = namespace.cros_class(namespace) |
| 40 | subcommand.Run() |
| 41 | return 0 |