blob: 2d29061a08010f5f1628ef44b3a00b1a5707c8b6 [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."""
Ryan Cui445658e2012-12-14 18:28:05 -080011 parser = commandline.ArgumentParser(caching=True)
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]):
David James57d82a22012-12-04 11:24:58 -080017 epilog = getattr(class_def, 'EPILOG', None)
18 sub_parser = subparsers.add_parser(
19 cmd_name, help=class_def.__doc__, epilog=epilog,
20 formatter_class=commandline.argparse.RawDescriptionHelpFormatter)
Chris Sosa90c78502012-10-05 17:07:42 -070021 class_def.AddParser(sub_parser)
22
23 return parser
24
25
26def main(args):
Brian Harring984988f2012-10-10 22:53:30 -070027 parser = GetOptions(commands.ListCommands())
Chris Sosa90c78502012-10-05 17:07:42 -070028 # Cros currently does nothing without a subcmd. Print help if no args are
29 # specified.
30 if not args:
31 parser.print_help()
32 return 1
33
34 namespace = parser.parse_args(args)
35 subcommand = namespace.cros_class(namespace)
36 subcommand.Run()
37 return 0