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 | |
| 7 | # Older version of Python might be missing argparse. |
| 8 | try: |
| 9 | import argparse |
| 10 | except ImportError: |
| 11 | print >> sys.stderr, "argparse missing. sudo apt-get install python-argparse" |
| 12 | sys.exit(1) |
| 13 | |
| 14 | from chromite.cros import commands |
| 15 | |
| 16 | |
| 17 | def GetOptions(commands): |
| 18 | """Returns the argparse to use for Cros.""" |
| 19 | parser = argparse.ArgumentParser() |
| 20 | if not commands: |
| 21 | return parser |
| 22 | |
| 23 | subparsers = parser.add_subparsers(title='cros commands') |
| 24 | for cmd_name, class_def in commands.iteritems(): |
| 25 | sub_parser = subparsers.add_parser(cmd_name, help=class_def.__doc__) |
| 26 | class_def.AddParser(sub_parser) |
| 27 | |
| 28 | return parser |
| 29 | |
| 30 | |
| 31 | def main(args): |
| 32 | parser = GetOptions(commands.commands) |
| 33 | # Cros currently does nothing without a subcmd. Print help if no args are |
| 34 | # specified. |
| 35 | if not args: |
| 36 | parser.print_help() |
| 37 | return 1 |
| 38 | |
| 39 | namespace = parser.parse_args(args) |
| 40 | subcommand = namespace.cros_class(namespace) |
| 41 | subcommand.Run() |
| 42 | return 0 |