blob: 8bf02e38942e06b1c02919f800c9bca821a16bda [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
Chris Sosa12a09182012-10-11 13:42:39 -07007# Older versions of Python might be missing argparse.
Chris Sosa90c78502012-10-05 17:07:42 -07008try:
9 import argparse
10except ImportError:
Chris Sosa12a09182012-10-11 13:42:39 -070011 from chromite.third_party import argparse
Chris Sosa90c78502012-10-05 17:07:42 -070012
13from chromite.cros import commands
14
15
Brian Harring984988f2012-10-10 22:53:30 -070016def GetOptions(my_commands):
Chris Sosa90c78502012-10-05 17:07:42 -070017 """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 Harring984988f2012-10-10 22:53:30 -070023 for cmd_name, class_def in my_commands.iteritems():
Chris Sosa90c78502012-10-05 17:07:42 -070024 sub_parser = subparsers.add_parser(cmd_name, help=class_def.__doc__)
25 class_def.AddParser(sub_parser)
26
27 return parser
28
29
30def main(args):
Brian Harring984988f2012-10-10 22:53:30 -070031 parser = GetOptions(commands.ListCommands())
Chris Sosa90c78502012-10-05 17:07:42 -070032 # 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