blob: 10eca53b61e0af4876c38ce8bac0aea400db6030 [file] [log] [blame]
Chris Sosa90c78502012-10-05 17:07:42 -07001# 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
5import sys
6
7# Older version of Python might be missing argparse.
8try:
9 import argparse
10except ImportError:
11 print >> sys.stderr, "argparse missing. sudo apt-get install python-argparse"
12 sys.exit(1)
13
14from chromite.cros import commands
15
16
Brian Harring984988f2012-10-10 22:53:30 -070017def GetOptions(my_commands):
Chris Sosa90c78502012-10-05 17:07:42 -070018 """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')
Brian Harring984988f2012-10-10 22:53:30 -070024 for cmd_name, class_def in my_commands.iteritems():
Chris Sosa90c78502012-10-05 17:07:42 -070025 sub_parser = subparsers.add_parser(cmd_name, help=class_def.__doc__)
26 class_def.AddParser(sub_parser)
27
28 return parser
29
30
31def main(args):
Brian Harring984988f2012-10-10 22:53:30 -070032 parser = GetOptions(commands.ListCommands())
Chris Sosa90c78502012-10-05 17:07:42 -070033 # 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