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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
8 | 8 |
9 import 'src/arg_parser.dart'; | 9 import 'src/arg_parser.dart'; |
10 import 'src/arg_results.dart'; | 10 import 'src/arg_results.dart'; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 /// A command is known as a "leaf command" if it has no subcommands and is meant | 183 /// A command is known as a "leaf command" if it has no subcommands and is meant |
184 /// to be run. Leaf commands must override [run]. | 184 /// to be run. Leaf commands must override [run]. |
185 /// | 185 /// |
186 /// A command with subcommands is known as a "branch command" and cannot be run | 186 /// A command with subcommands is known as a "branch command" and cannot be run |
187 /// itself. It should call [addSubcommand] (often from the constructor) to | 187 /// itself. It should call [addSubcommand] (often from the constructor) to |
188 /// register subcommands. | 188 /// register subcommands. |
189 abstract class Command { | 189 abstract class Command { |
190 /// The name of this command. | 190 /// The name of this command. |
191 String get name; | 191 String get name; |
192 | 192 |
193 /// A short description of this command. | 193 /// A description of this command, included in [usage]. |
194 String get description; | 194 String get description; |
195 | 195 |
| 196 /// A short description of this command, included in [parent]'s |
| 197 /// [CommandRunner.usage]. |
| 198 /// |
| 199 /// This defaults to the first line of [description]. |
| 200 String get summary => description.split("\n").first; |
| 201 |
196 /// A single-line template for how to invoke this command (e.g. `"pub get | 202 /// A single-line template for how to invoke this command (e.g. `"pub get |
197 /// [package]"`). | 203 /// [package]"`). |
198 String get invocation { | 204 String get invocation { |
199 var parents = [name]; | 205 var parents = [name]; |
200 for (var command = parent; command != null; command = command.parent) { | 206 for (var command = parent; command != null; command = command.parent) { |
201 parents.add(command.name); | 207 parents.add(command.name); |
202 } | 208 } |
203 parents.add(runner.executableName); | 209 parents.add(runner.executableName); |
204 | 210 |
205 var invocation = parents.reversed.join(" "); | 211 var invocation = parents.reversed.join(" "); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 var visible = names.where((name) => !commands[name].hidden); | 370 var visible = names.where((name) => !commands[name].hidden); |
365 if (visible.isNotEmpty) names = visible; | 371 if (visible.isNotEmpty) names = visible; |
366 | 372 |
367 // Show the commands alphabetically. | 373 // Show the commands alphabetically. |
368 names = names.toList()..sort(); | 374 names = names.toList()..sort(); |
369 var length = names.map((name) => name.length).reduce(math.max); | 375 var length = names.map((name) => name.length).reduce(math.max); |
370 | 376 |
371 var buffer = | 377 var buffer = |
372 new StringBuffer('Available ${isSubcommand ? "sub" : ""}commands:'); | 378 new StringBuffer('Available ${isSubcommand ? "sub" : ""}commands:'); |
373 for (var name in names) { | 379 for (var name in names) { |
374 var lines = commands[name].description.split("\n"); | 380 var lines = commands[name].summary.split("\n"); |
375 buffer.writeln(); | 381 buffer.writeln(); |
376 buffer.write(' ${padRight(name, length)} ${lines.first}'); | 382 buffer.write(' ${padRight(name, length)} ${lines.first}'); |
377 | 383 |
378 for (var line in lines.skip(1)) { | 384 for (var line in lines.skip(1)) { |
379 buffer.writeln(); | 385 buffer.writeln(); |
380 buffer.write(' ' * (length + 5)); | 386 buffer.write(' ' * (length + 5)); |
381 buffer.write(line); | 387 buffer.write(line); |
382 } | 388 } |
383 } | 389 } |
384 | 390 |
385 return buffer.toString(); | 391 return buffer.toString(); |
386 } | 392 } |
OLD | NEW |