| 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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 .then((localCompletions) { | 144 .then((localCompletions) { |
| 145 completions.addAll(localCompletions); | 145 completions.addAll(localCompletions); |
| 146 return completions; | 146 return completions; |
| 147 }); | 147 }); |
| 148 } | 148 } |
| 149 return new Future.value(completions); | 149 return new Future.value(completions); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Runs a command. | 152 // Runs a command. |
| 153 Future runCommand(String line) { | 153 Future runCommand(String line) { |
| 154 _historyAdvance(line); |
| 154 var args = _splitLine(line); | 155 var args = _splitLine(line); |
| 155 var commands = _match(args, true); | 156 var commands = _match(args, true); |
| 156 if (commands.isEmpty) { | 157 if (commands.isEmpty) { |
| 157 // TODO(turnidge): Add a proper exception class for this. | 158 // TODO(turnidge): Add a proper exception class for this. |
| 158 return new Future.error('notfound'); | 159 return new Future.error('notfound'); |
| 159 } else if (commands.length == 1) { | 160 } else if (commands.length == 1) { |
| 160 return commands[0].run(args.sublist(commands[0]._depth)); | 161 return commands[0].run(args.sublist(commands[0]._depth)); |
| 161 } else { | 162 } else { |
| 162 // TODO(turnidge): Add a proper exception class for this. | 163 // TODO(turnidge): Add a proper exception class for this. |
| 163 return new Future.error('ambiguous'); | 164 return new Future.error('ambiguous'); |
| 164 } | 165 } |
| 165 } | 166 } |
| 166 | 167 |
| 167 // Find all matching commands. Useful for implementing help systems. | 168 // Find all matching commands. Useful for implementing help systems. |
| 168 List<Command> matchCommand(List<String> args, bool preferExact) { | 169 List<Command> matchCommand(List<String> args, bool preferExact) { |
| 169 if (args.isEmpty) { | 170 if (args.isEmpty) { |
| 170 // Adding an empty string to the end causes us to match all | 171 // Adding an empty string to the end causes us to match all |
| 171 // subcommands of the last command. | 172 // subcommands of the last command. |
| 172 args.add(''); | 173 args.add(''); |
| 173 } | 174 } |
| 174 return _match(args, preferExact); | 175 return _match(args, preferExact); |
| 175 } | 176 } |
| 176 | 177 |
| 178 // Command line history always contains one slot to hold the current |
| 179 // line, so we start off with one entry. |
| 180 List<String> history = ['']; |
| 181 int historyPos = 0; |
| 182 |
| 183 String historyPrev(String line) { |
| 184 if (historyPos == 0) { |
| 185 return line; |
| 186 } |
| 187 history[historyPos] = line; |
| 188 historyPos--; |
| 189 return history[historyPos]; |
| 190 } |
| 191 |
| 192 String historyNext(String line) { |
| 193 if (historyPos == history.length - 1) { |
| 194 return line; |
| 195 } |
| 196 history[historyPos] = line; |
| 197 historyPos++; |
| 198 return history[historyPos]; |
| 199 } |
| 200 |
| 201 void _historyAdvance(String line) { |
| 202 // Replace the last history line. |
| 203 historyPos = history.length - 1; |
| 204 history[historyPos] = line; |
| 205 |
| 206 // Create an empty spot for the next line. |
| 207 history.add(''); |
| 208 historyPos++; |
| 209 } |
| 210 |
| 177 Future run(List<String> args) { | 211 Future run(List<String> args) { |
| 178 throw 'should-not-execute-the-root-command'; | 212 throw 'should-not-execute-the-root-command'; |
| 179 } | 213 } |
| 180 | 214 |
| 181 toString() => 'RootCommand'; | 215 toString() => 'RootCommand'; |
| 182 } | 216 } |
| 183 | 217 |
| 184 // A node in the command tree. | 218 // A node in the command tree. |
| 185 abstract class Command extends _CommandBase { | 219 abstract class Command extends _CommandBase { |
| 186 Command(this.name, List<Command> children) : super(children); | 220 Command(this.name, List<Command> children) : super(children); |
| 187 | 221 |
| 188 final String name; | 222 final String name; |
| 189 String get fullName { | 223 String get fullName { |
| 190 if (_parent is RootCommand) { | 224 if (_parent is RootCommand) { |
| 191 return name; | 225 return name; |
| 192 } else { | 226 } else { |
| 193 Command parent = _parent; | 227 Command parent = _parent; |
| 194 return '${parent.fullName} $name'; | 228 return '${parent.fullName} $name'; |
| 195 } | 229 } |
| 196 } | 230 } |
| 197 | 231 |
| 198 toString() => 'Command(${name})'; | 232 toString() => 'Command(${name})'; |
| 199 } | 233 } |
| OLD | NEW |