OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 part of cli; | 5 part of cli; |
6 | 6 |
7 // Splits a line into a list of string args. Each arg retains any | 7 // Splits a line into a list of string args. Each arg retains any |
8 // trailing whitespace so that we can reconstruct the original command | 8 // trailing whitespace so that we can reconstruct the original command |
9 // line from the pieces. | 9 // line from the pieces. |
10 List<String> _splitLine(String line) { | 10 List<String> _splitLine(String line) { |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 } | 169 } |
170 return new Future.value(completions); | 170 return new Future.value(completions); |
171 } | 171 } |
172 | 172 |
173 // Runs a command. | 173 // Runs a command. |
174 Future runCommand(String line) { | 174 Future runCommand(String line) { |
175 _historyAdvance(line); | 175 _historyAdvance(line); |
176 var args = _splitLine(line); | 176 var args = _splitLine(line); |
177 var commands = _match(args, true); | 177 var commands = _match(args, true); |
178 if (commands.isEmpty) { | 178 if (commands.isEmpty) { |
179 // TODO(turnidge): Add a proper exception class for this. | 179 return new Future.error(new NoSuchCommandException(line)); |
180 return new Future.error('No such command'); | |
181 } else if (commands.length == 1) { | 180 } else if (commands.length == 1) { |
182 return commands[0].run(args.sublist(commands[0]._depth)); | 181 return commands[0].run(args.sublist(commands[0]._depth)); |
183 } else { | 182 } else { |
184 // TODO(turnidge): Add a proper exception class for this. | 183 return new Future.error(new AmbiguousCommandException(line, commands)); |
185 return new Future.error('Ambiguous command'); | |
186 } | 184 } |
187 } | 185 } |
188 | 186 |
189 // Find all matching commands. Useful for implementing help systems. | 187 // Find all matching commands. Useful for implementing help systems. |
190 List<Command> matchCommand(List<String> args, bool preferExact) { | 188 List<Command> matchCommand(List<String> args, bool preferExact) { |
191 if (args.isEmpty) { | 189 if (args.isEmpty) { |
192 // Adding an empty string to the end causes us to match all | 190 // Adding an empty string to the end causes us to match all |
193 // subcommands of the last command. | 191 // subcommands of the last command. |
194 args.add(''); | 192 args.add(''); |
195 } | 193 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 if (_parent is RootCommand) { | 245 if (_parent is RootCommand) { |
248 return name; | 246 return name; |
249 } else { | 247 } else { |
250 Command parent = _parent; | 248 Command parent = _parent; |
251 return '${parent.fullName} $name'; | 249 return '${parent.fullName} $name'; |
252 } | 250 } |
253 } | 251 } |
254 | 252 |
255 toString() => 'Command(${name})'; | 253 toString() => 'Command(${name})'; |
256 } | 254 } |
| 255 |
| 256 abstract class CommandException implements Exception { |
| 257 } |
| 258 |
| 259 class AmbiguousCommandException extends CommandException { |
| 260 AmbiguousCommandException(this.command, this.matches); |
| 261 |
| 262 final String command; |
| 263 final List<Command> matches; |
| 264 |
| 265 @override |
| 266 String toString() { |
| 267 List<String> matchNames = matches.map( |
| 268 (Command command) => '${command.fullName}').toList(); |
| 269 return "Command '$command' is ambiguous: $matchNames"; |
| 270 } |
| 271 } |
| 272 |
| 273 class NoSuchCommandException extends CommandException { |
| 274 NoSuchCommandException(this.command); |
| 275 |
| 276 final String command; |
| 277 |
| 278 @override |
| 279 String toString() => "No such command: '$command'"; |
| 280 } |
OLD | NEW |