Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: packages/args/lib/command_runner.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/args/lib/args.dart ('k') | packages/args/lib/src/arg_parser.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.command_runner;
6
7 import 'dart:async'; 5 import 'dart:async';
8 import 'dart:collection'; 6 import 'dart:collection';
9 import 'dart:math' as math; 7 import 'dart:math' as math;
10 8
11 import 'src/arg_parser.dart'; 9 import 'src/arg_parser.dart';
10 import 'src/arg_parser_exception.dart';
12 import 'src/arg_results.dart'; 11 import 'src/arg_results.dart';
13 import 'src/help_command.dart'; 12 import 'src/help_command.dart';
14 import 'src/usage_exception.dart'; 13 import 'src/usage_exception.dart';
15 import 'src/utils.dart'; 14 import 'src/utils.dart';
16 15
17 export 'src/usage_exception.dart'; 16 export 'src/usage_exception.dart';
18 17
19 /// A class for invoking [Commands] based on raw command-line arguments. 18 /// A class for invoking [Commands] based on raw command-line arguments.
20 class CommandRunner { 19 ///
20 /// The type argument `T` represents the type returned by [Command.run] and
21 /// [CommandRunner.run]; it can be ommitted if you're not using the return
22 /// values.
23 class CommandRunner<T> {
21 /// The name of the executable being run. 24 /// The name of the executable being run.
22 /// 25 ///
23 /// Used for error reporting and [usage]. 26 /// Used for error reporting and [usage].
24 final String executableName; 27 final String executableName;
25 28
26 /// A short description of this executable. 29 /// A short description of this executable.
27 final String description; 30 final String description;
28 31
29 /// A single-line template for how to invoke this executable. 32 /// A single-line template for how to invoke this executable.
30 /// 33 ///
31 /// Defaults to "$executableName <command> [arguments]". Subclasses can 34 /// Defaults to "$executableName <command> [arguments]". Subclasses can
32 /// override this for a more specific template. 35 /// override this for a more specific template.
33 String get invocation => "$executableName <command> [arguments]"; 36 String get invocation => "$executableName <command> [arguments]";
34 37
35 /// Generates a string displaying usage information for the executable. 38 /// Generates a string displaying usage information for the executable.
36 /// 39 ///
37 /// This includes usage for the global arguments as well as a list of 40 /// This includes usage for the global arguments as well as a list of
38 /// top-level commands. 41 /// top-level commands.
39 String get usage => "$description\n\n$_usageWithoutDescription"; 42 String get usage => "$description\n\n$_usageWithoutDescription";
40 43
41 /// An optional footer for [usage]. 44 /// An optional footer for [usage].
42 /// 45 ///
43 /// If a subclass overrides this to return a string, it will automatically be 46 /// If a subclass overrides this to return a string, it will automatically be
44 /// added to the end of [usage]. 47 /// added to the end of [usage].
45 final String usageFooter = null; 48 String get usageFooter => null;
46 49
47 /// Returns [usage] with [description] removed from the beginning. 50 /// Returns [usage] with [description] removed from the beginning.
48 String get _usageWithoutDescription { 51 String get _usageWithoutDescription {
49 var usage = ''' 52 var usage = '''
50 Usage: $invocation 53 Usage: $invocation
51 54
52 Global options: 55 Global options:
53 ${argParser.usage} 56 ${argParser.usage}
54 57
55 ${_getCommandUsage(_commands)} 58 ${_getCommandUsage(_commands)}
56 59
57 Run "$executableName help <command>" for more information about a command.'''; 60 Run "$executableName help <command>" for more information about a command.''';
58 61
59 if (usageFooter != null) usage += "\n$usageFooter"; 62 if (usageFooter != null) usage += "\n$usageFooter";
60 return usage; 63 return usage;
61 } 64 }
62 65
63 /// An unmodifiable view of all top-level commands defined for this runner. 66 /// An unmodifiable view of all top-level commands defined for this runner.
64 Map<String, Command> get commands => new UnmodifiableMapView(_commands); 67 Map<String, Command<T>> get commands => new UnmodifiableMapView(_commands);
65 final _commands = new Map<String, Command>(); 68 final _commands = <String, Command<T>>{};
66 69
67 /// The top-level argument parser. 70 /// The top-level argument parser.
68 /// 71 ///
69 /// Global options should be registered with this parser; they'll end up 72 /// Global options should be registered with this parser; they'll end up
70 /// available via [Command.globalResults]. Commands should be registered with 73 /// available via [Command.globalResults]. Commands should be registered with
71 /// [addCommand] rather than directly on the parser. 74 /// [addCommand] rather than directly on the parser.
72 final argParser = new ArgParser(); 75 ArgParser get argParser => _argParser;
76 final _argParser = new ArgParser();
73 77
74 CommandRunner(this.executableName, this.description) { 78 CommandRunner(this.executableName, this.description) {
75 argParser.addFlag('help', 79 argParser.addFlag('help',
76 abbr: 'h', negatable: false, help: 'Print this usage information.'); 80 abbr: 'h', negatable: false, help: 'Print this usage information.');
77 addCommand(new HelpCommand()); 81 addCommand(new HelpCommand<T>());
78 } 82 }
79 83
80 /// Prints the usage information for this runner. 84 /// Prints the usage information for this runner.
81 /// 85 ///
82 /// This is called internally by [run] and can be overridden by subclasses to 86 /// This is called internally by [run] and can be overridden by subclasses to
83 /// control how output is displayed or integrate with a logging system. 87 /// control how output is displayed or integrate with a logging system.
84 void printUsage() => print(usage); 88 void printUsage() => print(usage);
85 89
86 /// Throws a [UsageException] with [message]. 90 /// Throws a [UsageException] with [message].
87 void usageException(String message) => 91 void usageException(String message) =>
88 throw new UsageException(message, _usageWithoutDescription); 92 throw new UsageException(message, _usageWithoutDescription);
89 93
90 /// Adds [Command] as a top-level command to this runner. 94 /// Adds [Command] as a top-level command to this runner.
91 void addCommand(Command command) { 95 void addCommand(Command<T> command) {
92 var names = [command.name]..addAll(command.aliases); 96 var names = [command.name]..addAll(command.aliases);
93 for (var name in names) { 97 for (var name in names) {
94 _commands[name] = command; 98 _commands[name] = command;
95 argParser.addCommand(name, command.argParser); 99 argParser.addCommand(name, command.argParser);
96 } 100 }
97 command._runner = this; 101 command._runner = this;
98 } 102 }
99 103
100 /// Parses [args] and invokes [Command.run] on the chosen command. 104 /// Parses [args] and invokes [Command.run] on the chosen command.
101 /// 105 ///
102 /// This always returns a [Future] in case the command is asynchronous. The 106 /// This always returns a [Future] in case the command is asynchronous. The
103 /// [Future] will throw a [UsageError] if [args] was invalid. 107 /// [Future] will throw a [UsageException] if [args] was invalid.
104 Future run(Iterable<String> args) => 108 Future<T> run(Iterable<String> args) =>
105 new Future.sync(() => runCommand(parse(args))); 109 new Future.sync(() => runCommand(parse(args)));
106 110
107 /// Parses [args] and returns the result, converting a [FormatException] to a 111 /// Parses [args] and returns the result, converting an [ArgParserException]
108 /// [UsageException]. 112 /// to a [UsageException].
109 /// 113 ///
110 /// This is notionally a protected method. It may be overridden or called from 114 /// This is notionally a protected method. It may be overridden or called from
111 /// subclasses, but it shouldn't be called externally. 115 /// subclasses, but it shouldn't be called externally.
112 ArgResults parse(Iterable<String> args) { 116 ArgResults parse(Iterable<String> args) {
113 try { 117 try {
114 // TODO(nweiz): if arg parsing fails for a command, print that command's
115 // usage, not the global usage.
116 return argParser.parse(args); 118 return argParser.parse(args);
117 } on FormatException catch (error) { 119 } on ArgParserException catch (error) {
118 usageException(error.message); 120 if (error.commands.isEmpty) usageException(error.message);
121
122 var command = commands[error.commands.first];
123 for (var commandName in error.commands.skip(1)) {
124 command = command.subcommands[commandName];
125 }
126
127 command.usageException(error.message);
128 return null;
119 } 129 }
120 } 130 }
121 131
122 /// Runs the command specified by [topLevelResults]. 132 /// Runs the command specified by [topLevelResults].
123 /// 133 ///
124 /// This is notionally a protected method. It may be overridden or called from 134 /// This is notionally a protected method. It may be overridden or called from
125 /// subclasses, but it shouldn't be called externally. 135 /// subclasses, but it shouldn't be called externally.
126 /// 136 ///
127 /// It's useful to override this to handle global flags and/or wrap the entire 137 /// It's useful to override this to handle global flags and/or wrap the entire
128 /// command in a block. For example, you might handle the `--verbose` flag 138 /// command in a block. For example, you might handle the `--verbose` flag
129 /// here to enable verbose logging before running the command. 139 /// here to enable verbose logging before running the command.
130 Future runCommand(ArgResults topLevelResults) { 140 ///
131 return new Future.sync(() { 141 /// This returns the return value of [Command.run].
132 var argResults = topLevelResults; 142 Future<T> runCommand(ArgResults topLevelResults) async {
133 var commands = _commands; 143 var argResults = topLevelResults;
134 var command; 144 var commands = _commands;
135 var commandString = executableName; 145 Command command;
146 var commandString = executableName;
136 147
137 while (commands.isNotEmpty) { 148 while (commands.isNotEmpty) {
138 if (argResults.command == null) { 149 if (argResults.command == null) {
139 if (argResults.rest.isEmpty) { 150 if (argResults.rest.isEmpty) {
140 if (command == null) { 151 if (command == null) {
141 // No top-level command was chosen. 152 // No top-level command was chosen.
142 printUsage(); 153 printUsage();
143 return new Future.value(); 154 return null;
144 } 155 }
145 156
146 command.usageException('Missing subcommand for "$commandString".'); 157 command.usageException('Missing subcommand for "$commandString".');
147 } else { 158 } else {
148 if (command == null) { 159 if (command == null) {
149 usageException( 160 usageException(
150 'Could not find a command named "${argResults.rest[0]}".'); 161 'Could not find a command named "${argResults.rest[0]}".');
151 } 162 }
152 163
153 command.usageException('Could not find a subcommand named ' 164 command.usageException('Could not find a subcommand named '
154 '"${argResults.rest[0]}" for "$commandString".'); 165 '"${argResults.rest[0]}" for "$commandString".');
155 }
156 }
157
158 // Step into the command.
159 argResults = argResults.command;
160 command = commands[argResults.name];
161 command._globalResults = topLevelResults;
162 command._argResults = argResults;
163 commands = command._subcommands;
164 commandString += " ${argResults.name}";
165
166 if (argResults['help']) {
167 command.printUsage();
168 return new Future.value();
169 } 166 }
170 } 167 }
171 168
172 // Make sure there aren't unexpected arguments. 169 // Step into the command.
173 if (!command.takesArguments && argResults.rest.isNotEmpty) { 170 argResults = argResults.command;
174 command.usageException( 171 command = commands[argResults.name];
175 'Command "${argResults.name}" does not take any arguments.'); 172 command._globalResults = topLevelResults;
173 command._argResults = argResults;
174 commands = command._subcommands;
175 commandString += " ${argResults.name}";
176
177 if (argResults['help']) {
178 command.printUsage();
179 return null;
176 } 180 }
181 }
177 182
178 return command.run(); 183 if (topLevelResults['help']) {
179 }); 184 command.printUsage();
185 return null;
186 }
187
188 // Make sure there aren't unexpected arguments.
189 if (!command.takesArguments && argResults.rest.isNotEmpty) {
190 command.usageException(
191 'Command "${argResults.name}" does not take any arguments.');
192 }
193
194 return (await command.run()) as T;
180 } 195 }
181 } 196 }
182 197
183 /// A single command. 198 /// A single command.
184 /// 199 ///
185 /// A command is known as a "leaf command" if it has no subcommands and is meant 200 /// A command is known as a "leaf command" if it has no subcommands and is meant
186 /// to be run. Leaf commands must override [run]. 201 /// to be run. Leaf commands must override [run].
187 /// 202 ///
188 /// A command with subcommands is known as a "branch command" and cannot be run 203 /// A command with subcommands is known as a "branch command" and cannot be run
189 /// itself. It should call [addSubcommand] (often from the constructor) to 204 /// itself. It should call [addSubcommand] (often from the constructor) to
190 /// register subcommands. 205 /// register subcommands.
191 abstract class Command { 206 abstract class Command<T> {
192 /// The name of this command. 207 /// The name of this command.
193 String get name; 208 String get name;
194 209
195 /// A short description of this command. 210 /// A description of this command, included in [usage].
196 String get description; 211 String get description;
197 212
213 /// A short description of this command, included in [parent]'s
214 /// [CommandRunner.usage].
215 ///
216 /// This defaults to the first line of [description].
217 String get summary => description.split("\n").first;
218
198 /// A single-line template for how to invoke this command (e.g. `"pub get 219 /// A single-line template for how to invoke this command (e.g. `"pub get
199 /// [package]"`). 220 /// [package]"`).
200 String get invocation { 221 String get invocation {
201 var parents = [name]; 222 var parents = [name];
202 for (var command = parent; command != null; command = command.parent) { 223 for (var command = parent; command != null; command = command.parent) {
203 parents.add(command.name); 224 parents.add(command.name);
204 } 225 }
205 parents.add(runner.executableName); 226 parents.add(runner.executableName);
206 227
207 var invocation = parents.reversed.join(" "); 228 var invocation = parents.reversed.join(" ");
208 return _subcommands.isNotEmpty 229 return _subcommands.isNotEmpty
209 ? "$invocation <subcommand> [arguments]" 230 ? "$invocation <subcommand> [arguments]"
210 : "$invocation [arguments]"; 231 : "$invocation [arguments]";
211 } 232 }
212 233
213 /// The command's parent command, if this is a subcommand. 234 /// The command's parent command, if this is a subcommand.
214 /// 235 ///
215 /// This will be `null` until [Command.addSubcommmand] has been called with 236 /// This will be `null` until [Command.addSubcommmand] has been called with
216 /// this command. 237 /// this command.
217 Command get parent => _parent; 238 Command<T> get parent => _parent;
218 Command _parent; 239 Command<T> _parent;
219 240
220 /// The command runner for this command. 241 /// The command runner for this command.
221 /// 242 ///
222 /// This will be `null` until [CommandRunner.addCommand] has been called with 243 /// This will be `null` until [CommandRunner.addCommand] has been called with
223 /// this command or one of its parents. 244 /// this command or one of its parents.
224 CommandRunner get runner { 245 CommandRunner<T> get runner {
225 if (parent == null) return _runner; 246 if (parent == null) return _runner;
226 return parent.runner; 247 return parent.runner;
227 } 248 }
228 CommandRunner _runner; 249 CommandRunner<T> _runner;
229 250
230 /// The parsed global argument results. 251 /// The parsed global argument results.
231 /// 252 ///
232 /// This will be `null` until just before [Command.run] is called. 253 /// This will be `null` until just before [Command.run] is called.
233 ArgResults get globalResults => _globalResults; 254 ArgResults get globalResults => _globalResults;
234 ArgResults _globalResults; 255 ArgResults _globalResults;
235 256
236 /// The parsed argument results for this command. 257 /// The parsed argument results for this command.
237 /// 258 ///
238 /// This will be `null` until just before [Command.run] is called. 259 /// This will be `null` until just before [Command.run] is called.
239 ArgResults get argResults => _argResults; 260 ArgResults get argResults => _argResults;
240 ArgResults _argResults; 261 ArgResults _argResults;
241 262
242 /// The argument parser for this command. 263 /// The argument parser for this command.
243 /// 264 ///
244 /// Options for this command should be registered with this parser (often in 265 /// Options for this command should be registered with this parser (often in
245 /// the constructor); they'll end up available via [argResults]. Subcommands 266 /// the constructor); they'll end up available via [argResults]. Subcommands
246 /// should be registered with [addSubcommand] rather than directly on the 267 /// should be registered with [addSubcommand] rather than directly on the
247 /// parser. 268 /// parser.
248 final argParser = new ArgParser(); 269 ArgParser get argParser => _argParser;
270 final _argParser = new ArgParser();
249 271
250 /// Generates a string displaying usage information for this command. 272 /// Generates a string displaying usage information for this command.
251 /// 273 ///
252 /// This includes usage for the command's arguments as well as a list of 274 /// This includes usage for the command's arguments as well as a list of
253 /// subcommands, if there are any. 275 /// subcommands, if there are any.
254 String get usage => "$description\n\n$_usageWithoutDescription"; 276 String get usage => "$description\n\n$_usageWithoutDescription";
255 277
256 /// An optional footer for [usage]. 278 /// An optional footer for [usage].
257 /// 279 ///
258 /// If a subclass overrides this to return a string, it will automatically be 280 /// If a subclass overrides this to return a string, it will automatically be
259 /// added to the end of [usage]. 281 /// added to the end of [usage].
260 final String usageFooter = null; 282 String get usageFooter => null;
261 283
262 /// Returns [usage] with [description] removed from the beginning. 284 /// Returns [usage] with [description] removed from the beginning.
263 String get _usageWithoutDescription { 285 String get _usageWithoutDescription {
264 var buffer = new StringBuffer() 286 var buffer = new StringBuffer()
265 ..writeln('Usage: $invocation') 287 ..writeln('Usage: $invocation')
266 ..writeln(argParser.usage); 288 ..writeln(argParser.usage);
267 289
268 if (_subcommands.isNotEmpty) { 290 if (_subcommands.isNotEmpty) {
269 buffer.writeln(); 291 buffer.writeln();
270 buffer.writeln(_getCommandUsage(_subcommands, isSubcommand: true)); 292 buffer.writeln(_getCommandUsage(_subcommands, isSubcommand: true));
271 } 293 }
272 294
273 buffer.writeln(); 295 buffer.writeln();
274 buffer.write('Run "${runner.executableName} help" to see global options.'); 296 buffer.write('Run "${runner.executableName} help" to see global options.');
275 297
276 if (usageFooter != null) { 298 if (usageFooter != null) {
277 buffer.writeln(); 299 buffer.writeln();
278 buffer.write(usageFooter); 300 buffer.write(usageFooter);
279 } 301 }
280 302
281 return buffer.toString(); 303 return buffer.toString();
282 } 304 }
283 305
284 /// An unmodifiable view of all sublevel commands of this command. 306 /// An unmodifiable view of all sublevel commands of this command.
285 Map<String, Command> get subcommands => new UnmodifiableMapView(_subcommands); 307 Map<String, Command<T>> get subcommands =>
286 final _subcommands = new Map<String, Command>(); 308 new UnmodifiableMapView(_subcommands);
309 final _subcommands = <String, Command<T>>{};
287 310
288 /// Whether or not this command should be hidden from help listings. 311 /// Whether or not this command should be hidden from help listings.
289 /// 312 ///
290 /// This is intended to be overridden by commands that want to mark themselves 313 /// This is intended to be overridden by commands that want to mark themselves
291 /// hidden. 314 /// hidden.
292 /// 315 ///
293 /// By default, leaf commands are always visible. Branch commands are visible 316 /// By default, leaf commands are always visible. Branch commands are visible
294 /// as long as any of their leaf commands are visible. 317 /// as long as any of their leaf commands are visible.
295 bool get hidden { 318 bool get hidden {
296 // Leaf commands are visible by default. 319 // Leaf commands are visible by default.
297 if (_subcommands.isEmpty) return false; 320 if (_subcommands.isEmpty) return false;
298 321
299 // Otherwise, a command is hidden if all of its subcommands are. 322 // Otherwise, a command is hidden if all of its subcommands are.
300 return _subcommands.values.every((subcommand) => subcommand.hidden); 323 return _subcommands.values.every((subcommand) => subcommand.hidden);
301 } 324 }
302 325
303 /// Whether or not this command takes positional arguments in addition to 326 /// Whether or not this command takes positional arguments in addition to
304 /// options. 327 /// options.
305 /// 328 ///
306 /// If false, [CommandRunner.run] will throw a [UsageException] if arguments 329 /// If false, [CommandRunner.run] will throw a [UsageException] if arguments
307 /// are provided. Defaults to true. 330 /// are provided. Defaults to true.
308 /// 331 ///
309 /// This is intended to be overridden by commands that don't want to receive 332 /// This is intended to be overridden by commands that don't want to receive
310 /// arguments. It has no effect for branch commands. 333 /// arguments. It has no effect for branch commands.
311 final takesArguments = true; 334 bool get takesArguments => true;
312 335
313 /// Alternate names for this command. 336 /// Alternate names for this command.
314 /// 337 ///
315 /// These names won't be used in the documentation, but they will work when 338 /// These names won't be used in the documentation, but they will work when
316 /// invoked on the command line. 339 /// invoked on the command line.
317 /// 340 ///
318 /// This is intended to be overridden. 341 /// This is intended to be overridden.
319 final aliases = const <String>[]; 342 List<String> get aliases => const [];
320 343
321 Command() { 344 Command() {
322 argParser.addFlag('help', 345 argParser.addFlag('help',
323 abbr: 'h', negatable: false, help: 'Print this usage information.'); 346 abbr: 'h', negatable: false, help: 'Print this usage information.');
324 } 347 }
325 348
326 /// Runs this command. 349 /// Runs this command.
327 /// 350 ///
328 /// If this returns a [Future], [CommandRunner.run] won't complete until the 351 /// This must return a `T`, a `Future<T>`, or `null`. The value is returned by
329 /// returned [Future] does. Otherwise, the return value is ignored. 352 /// [CommandRunner.runCommand]. Subclasses must explicitly declare a return
353 /// type for `run()`, and may not use `void` if `T` is defined.
330 run() { 354 run() {
331 throw new UnimplementedError("Leaf command $this must implement run()."); 355 throw new UnimplementedError("Leaf command $this must implement run().");
332 } 356 }
333 357
334 /// Adds [Command] as a subcommand of this. 358 /// Adds [Command] as a subcommand of this.
335 void addSubcommand(Command command) { 359 void addSubcommand(Command<T> command) {
336 var names = [command.name]..addAll(command.aliases); 360 var names = [command.name]..addAll(command.aliases);
337 for (var name in names) { 361 for (var name in names) {
338 _subcommands[name] = command; 362 _subcommands[name] = command;
339 argParser.addCommand(name, command.argParser); 363 argParser.addCommand(name, command.argParser);
340 } 364 }
341 command._parent = this; 365 command._parent = this;
342 } 366 }
343 367
344 /// Prints the usage information for this command. 368 /// Prints the usage information for this command.
345 /// 369 ///
346 /// This is called internally by [run] and can be overridden by subclasses to 370 /// This is called internally by [run] and can be overridden by subclasses to
347 /// control how output is displayed or integrate with a logging system. 371 /// control how output is displayed or integrate with a logging system.
348 void printUsage() => print(usage); 372 void printUsage() => print(usage);
349 373
350 /// Throws a [UsageException] with [message]. 374 /// Throws a [UsageException] with [message].
351 void usageException(String message) => 375 void usageException(String message) =>
352 throw new UsageException(message, _usageWithoutDescription); 376 throw new UsageException(message, _usageWithoutDescription);
353 } 377 }
354 378
355 /// Returns a string representation of [commands] fit for use in a usage string. 379 /// Returns a string representation of [commands] fit for use in a usage string.
356 /// 380 ///
357 /// [isSubcommand] indicates whether the commands should be called "commands" or 381 /// [isSubcommand] indicates whether the commands should be called "commands" or
358 /// "subcommands". 382 /// "subcommands".
359 String _getCommandUsage(Map<String, Command> commands, 383 String _getCommandUsage(Map<String, Command> commands,
360 {bool isSubcommand: false}) { 384 {bool isSubcommand: false}) {
361 // Don't include aliases. 385 // Don't include aliases.
362 var names = 386 var names =
363 commands.keys.where((name) => !commands[name].aliases.contains(name)); 387 commands.keys.where((name) => !commands[name].aliases.contains(name));
364 388
365 // Filter out hidden ones, unless they are all hidden. 389 // Filter out hidden ones, unless they are all hidden.
366 var visible = names.where((name) => !commands[name].hidden); 390 var visible = names.where((name) => !commands[name].hidden);
367 if (visible.isNotEmpty) names = visible; 391 if (visible.isNotEmpty) names = visible;
368 392
369 // Show the commands alphabetically. 393 // Show the commands alphabetically.
370 names = names.toList()..sort(); 394 names = names.toList()..sort();
371 var length = names.map((name) => name.length).reduce(math.max); 395 var length = names.map((name) => name.length).reduce(math.max);
372 396
373 var buffer = 397 var buffer =
374 new StringBuffer('Available ${isSubcommand ? "sub" : ""}commands:'); 398 new StringBuffer('Available ${isSubcommand ? "sub" : ""}commands:');
375 for (var name in names) { 399 for (var name in names) {
400 var lines = commands[name].summary.split("\n");
376 buffer.writeln(); 401 buffer.writeln();
377 buffer.write(' ${padRight(name, length)} ' 402 buffer.write(' ${padRight(name, length)} ${lines.first}');
378 '${commands[name].description.split("\n").first}'); 403
404 for (var line in lines.skip(1)) {
405 buffer.writeln();
406 buffer.write(' ' * (length + 5));
407 buffer.write(line);
408 }
379 } 409 }
380 410
381 return buffer.toString(); 411 return buffer.toString();
382 } 412 }
OLDNEW
« no previous file with comments | « packages/args/lib/args.dart ('k') | packages/args/lib/src/arg_parser.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698