| 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. Each arg retains any |
| 8 // trailing whitespace so that we can reconstruct the original command |
| 9 // line from the pieces. |
| 8 List<String> _splitLine(String line) { | 10 List<String> _splitLine(String line) { |
| 9 var args = line.split(' ').where((arg) { | 11 line = line.trimLeft(); |
| 10 return arg != ' ' && arg != ''; | 12 var args = []; |
| 11 }).toList(); | 13 var codes = line.codeUnits; |
| 14 |
| 15 int pos = 0; |
| 16 while (pos < line.length) { |
| 17 int startPos = pos; |
| 18 |
| 19 // Advance to end of word. |
| 20 for (; pos < line.length && line[pos] != ' '; pos++); |
| 21 |
| 22 // Advance to end of spaces. |
| 23 for (; pos < line.length && line[pos] == ' '; pos++); |
| 24 |
| 25 args.add(line.substring(startPos, pos)); |
| 26 } |
| 12 return args; | 27 return args; |
| 13 } | 28 } |
| 14 | 29 |
| 15 // Concatenates the first 'count' args. | 30 // Concatenates the first 'count' args. |
| 16 String _concatArgs(List<String> args, int count) { | 31 String _concatArgs(List<String> args, int count) { |
| 17 if (count == 0) { | 32 if (count == 0) { |
| 18 return ''; | 33 return ''; |
| 19 } | 34 } |
| 20 return '${args.sublist(0, count).join(" ")} '; | 35 return '${args.sublist(0, count).join('')}'; |
| 21 } | 36 } |
| 22 | 37 |
| 23 // Shared functionality for RootCommand and Command. | 38 // Shared functionality for RootCommand and Command. |
| 24 abstract class _CommandBase { | 39 abstract class _CommandBase { |
| 25 _CommandBase(List<Command> children) { | 40 _CommandBase(List<Command> children) { |
| 26 assert(children != null); | 41 assert(children != null); |
| 27 _children.addAll(children); | 42 _children.addAll(children); |
| 28 for (var child in _children) { | 43 for (var child in _children) { |
| 29 child._parent = this; | 44 child._parent = this; |
| 30 } | 45 } |
| 31 } | 46 } |
| 32 | 47 |
| 33 // A command may optionally have sub-commands. | 48 // A command may optionally have sub-commands. |
| 34 List<Command> _children = []; | 49 List<Command> _children = []; |
| 35 | 50 |
| 36 _CommandBase _parent; | 51 _CommandBase _parent; |
| 37 int get _depth => (_parent == null ? 0 : _parent._depth + 1); | 52 int get _depth => (_parent == null ? 0 : _parent._depth + 1); |
| 38 | 53 |
| 39 // Override in subclasses to provide command-specific argument completion. | 54 // Override in subclasses to provide command-specific argument completion. |
| 40 // | 55 // |
| 41 // Given a list of arguments to this command, provide a list of | 56 // Given a list of arguments to this command, provide a list of |
| 42 // possible completions for those arguments. | 57 // possible completions for those arguments. |
| 43 Future<List<String>> complete(List<String> args) => new Future.value([]); | 58 Future<List<String>> complete(List<String> args) => new Future.value([]); |
| 44 | 59 |
| 45 // Override in subclasses to provide command-specific execution. | 60 // Override in subclasses to provide command-specific execution. |
| 46 Future run(List<String> args); | 61 Future run(List<String> args); |
| 47 | 62 |
| 48 // Returns a list of local subcommands which match the args. | 63 // Returns a list of local subcommands which match the args. |
| 49 List<Command> _matchLocal(String arg, bool preferExact) { | 64 List<Command> _matchLocal(String argWithSpace, bool preferExact) { |
| 50 var matches = new List<Command>(); | 65 var matches = new List<Command>(); |
| 66 var arg = argWithSpace.trimRight(); |
| 51 for (var child in _children) { | 67 for (var child in _children) { |
| 52 if (child.name.startsWith(arg)) { | 68 if (child.name.startsWith(arg)) { |
| 53 if (preferExact && ((child.name == arg) || (child.alias == arg))) { | 69 if (preferExact && ((child.name == arg) || (child.alias == arg))) { |
| 54 return [child]; | 70 return [child]; |
| 55 } | 71 } |
| 56 matches.add(child); | 72 matches.add(child); |
| 57 } | 73 } |
| 58 } | 74 } |
| 59 return matches; | 75 return matches; |
| 60 } | 76 } |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 if (_parent is RootCommand) { | 242 if (_parent is RootCommand) { |
| 227 return name; | 243 return name; |
| 228 } else { | 244 } else { |
| 229 Command parent = _parent; | 245 Command parent = _parent; |
| 230 return '${parent.fullName} $name'; | 246 return '${parent.fullName} $name'; |
| 231 } | 247 } |
| 232 } | 248 } |
| 233 | 249 |
| 234 toString() => 'Command(${name})'; | 250 toString() => 'Command(${name})'; |
| 235 } | 251 } |
| OLD | NEW |