OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library args.help_command; | |
6 | |
7 import '../command_runner.dart'; | 5 import '../command_runner.dart'; |
8 | 6 |
9 /// The built-in help command that's added to every [CommandRunner]. | 7 /// The built-in help command that's added to every [CommandRunner]. |
10 /// | 8 /// |
11 /// This command displays help information for the various subcommands. | 9 /// This command displays help information for the various subcommands. |
12 class HelpCommand extends Command { | 10 class HelpCommand<T> extends Command<T> { |
13 final name = "help"; | 11 final name = "help"; |
14 String get description => | 12 String get description => |
15 "Display help information for ${runner.executableName}."; | 13 "Display help information for ${runner.executableName}."; |
16 String get invocation => "${runner.executableName} help [command]"; | 14 String get invocation => "${runner.executableName} help [command]"; |
17 | 15 |
18 void run() { | 16 T run() { |
19 // Show the default help if no command was specified. | 17 // Show the default help if no command was specified. |
20 if (argResults.rest.isEmpty) { | 18 if (argResults.rest.isEmpty) { |
21 runner.printUsage(); | 19 runner.printUsage(); |
22 return; | 20 return null; |
23 } | 21 } |
24 | 22 |
25 // Walk the command tree to show help for the selected command or | 23 // Walk the command tree to show help for the selected command or |
26 // subcommand. | 24 // subcommand. |
27 var commands = runner.commands; | 25 var commands = runner.commands; |
28 var command = null; | 26 Command command; |
29 var commandString = runner.executableName; | 27 var commandString = runner.executableName; |
30 | 28 |
31 for (var name in argResults.rest) { | 29 for (var name in argResults.rest) { |
32 if (commands.isEmpty) { | 30 if (commands.isEmpty) { |
33 command.usageException( | 31 command.usageException( |
34 'Command "$commandString" does not expect a subcommand.'); | 32 'Command "$commandString" does not expect a subcommand.'); |
35 } | 33 } |
36 | 34 |
37 if (commands[name] == null) { | 35 if (commands[name] == null) { |
38 if (command == null) { | 36 if (command == null) { |
39 runner.usageException('Could not find a command named "$name".'); | 37 runner.usageException('Could not find a command named "$name".'); |
40 } | 38 } |
41 | 39 |
42 command.usageException( | 40 command.usageException( |
43 'Could not find a subcommand named "$name" for "$commandString".'); | 41 'Could not find a subcommand named "$name" for "$commandString".'); |
44 } | 42 } |
45 | 43 |
46 command = commands[name]; | 44 command = commands[name]; |
47 commands = command.subcommands; | 45 commands = command.subcommands; |
48 commandString += " $name"; | 46 commandString += " $name"; |
49 } | 47 } |
50 | 48 |
51 command.printUsage(); | 49 command.printUsage(); |
| 50 return null; |
52 } | 51 } |
53 } | 52 } |
OLD | NEW |