blob: 532897d0d0abcc8c387c552fd0045361183b8ea4 [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
Chris Sosa90c78502012-10-05 17:07:42 -07005from chromite.cros import commands
Chris Sosab445f792012-10-11 15:26:39 -07006from chromite.lib import commandline
Chris Sosa90c78502012-10-05 17:07:42 -07007
8
Brian Harring984988f2012-10-10 22:53:30 -07009def GetOptions(my_commands):
Chris Sosa90c78502012-10-05 17:07:42 -070010 """Returns the argparse to use for Cros."""
Chris Sosab445f792012-10-11 15:26:39 -070011 parser = commandline.ArgumentParser()
Chris Sosa90c78502012-10-05 17:07:42 -070012 if not commands:
13 return parser
14
15 subparsers = parser.add_subparsers(title='cros commands')
Brian Harringcb68df32012-12-05 04:26:29 -080016 for cmd_name, class_def in sorted(my_commands.iteritems(), key=lambda x:x[0]):
Chris Sosa90c78502012-10-05 17:07:42 -070017 sub_parser = subparsers.add_parser(cmd_name, help=class_def.__doc__)
18 class_def.AddParser(sub_parser)
19
20 return parser
21
22
23def main(args):
Brian Harring984988f2012-10-10 22:53:30 -070024 parser = GetOptions(commands.ListCommands())
Chris Sosa90c78502012-10-05 17:07:42 -070025 # Cros currently does nothing without a subcmd. Print help if no args are
26 # specified.
27 if not args:
28 parser.print_help()
29 return 1
30
31 namespace = parser.parse_args(args)
32 subcommand = namespace.cros_class(namespace)
33 subcommand.Run()
34 return 0