| 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. | 7 // Splits a line into a list of string args. |
| 8 List<String> _splitLine(String line) { | 8 List<String> _splitLine(String line) { |
| 9 var args = line.split(' ').where((arg) { | 9 var args = line.split(' ').where((arg) { |
| 10 return arg != ' ' && arg != ''; | 10 return arg != ' ' && arg != ''; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // TODO(turnidge): Add a proper exception class for this. | 157 // TODO(turnidge): Add a proper exception class for this. |
| 158 return new Future.error('notfound'); | 158 return new Future.error('notfound'); |
| 159 } else if (commands.length == 1) { | 159 } else if (commands.length == 1) { |
| 160 return commands[0].run(args.sublist(commands[0]._depth)); | 160 return commands[0].run(args.sublist(commands[0]._depth)); |
| 161 } else { | 161 } else { |
| 162 // TODO(turnidge): Add a proper exception class for this. | 162 // TODO(turnidge): Add a proper exception class for this. |
| 163 return new Future.error('ambiguous'); | 163 return new Future.error('ambiguous'); |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 | 166 |
| 167 // Find all matching commands. Useful for implementing help systems. |
| 168 List<Command> matchCommand(List<String> args, bool preferExact) { |
| 169 if (args.isEmpty) { |
| 170 // Adding an empty string to the end causes us to match all |
| 171 // subcommands of the last command. |
| 172 args.add(''); |
| 173 } |
| 174 return _match(args, preferExact); |
| 175 } |
| 176 |
| 167 Future run(List<String> args) { | 177 Future run(List<String> args) { |
| 168 throw 'should-not-execute-the-root-command'; | 178 throw 'should-not-execute-the-root-command'; |
| 169 } | 179 } |
| 170 | 180 |
| 171 toString() => 'RootCommand'; | 181 toString() => 'RootCommand'; |
| 172 } | 182 } |
| 173 | 183 |
| 174 // A node in the command tree. | 184 // A node in the command tree. |
| 175 abstract class Command extends _CommandBase { | 185 abstract class Command extends _CommandBase { |
| 176 Command(this.name, List<Command> children) : super(children); | 186 Command(this.name, List<Command> children) : super(children); |
| 177 | 187 |
| 178 final name; | 188 final String name; |
| 189 String get fullName { |
| 190 if (_parent is RootCommand) { |
| 191 return name; |
| 192 } else { |
| 193 return '${_parent.fullName} $name'; |
| 194 } |
| 195 } |
| 179 | 196 |
| 180 toString() => 'Command(${name})'; | 197 toString() => 'Command(${name})'; |
| 181 } | 198 } |
| OLD | NEW |