blob: 7d6f4244a12930d5cd3669c7acdb4db905f370bf [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
17def 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
31def 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