Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 1 | # 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 Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 5 | from chromite.cros import commands |
Chris Sosa | b445f79 | 2012-10-11 15:26:39 -0700 | [diff] [blame] | 6 | from chromite.lib import commandline |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 7 | |
| 8 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 9 | def GetOptions(my_commands): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 10 | """Returns the argparse to use for Cros.""" |
Chris Sosa | b445f79 | 2012-10-11 15:26:39 -0700 | [diff] [blame] | 11 | parser = commandline.ArgumentParser() |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 12 | if not commands: |
| 13 | return parser |
| 14 | |
| 15 | subparsers = parser.add_subparsers(title='cros commands') |
Brian Harring | cb68df3 | 2012-12-05 04:26:29 -0800 | [diff] [blame^] | 16 | for cmd_name, class_def in sorted(my_commands.iteritems(), key=lambda x:x[0]): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 17 | sub_parser = subparsers.add_parser(cmd_name, help=class_def.__doc__) |
| 18 | class_def.AddParser(sub_parser) |
| 19 | |
| 20 | return parser |
| 21 | |
| 22 | |
| 23 | def main(args): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 24 | parser = GetOptions(commands.ListCommands()) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 25 | # 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 |