| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 fasta.command_line; | 5 library fasta.command_line; |
| 6 | 6 |
| 7 import 'fasta_codes.dart' |
| 8 show codeFastaCLIArgumentRequired, BoundFastaMessage, FastaMessage; |
| 9 |
| 7 import 'deprecated_problems.dart' | 10 import 'deprecated_problems.dart' |
| 8 show deprecated_inputError, deprecated_internalProblem; | 11 show deprecated_inputError, deprecated_internalProblem; |
| 9 | 12 |
| 10 deprecated_argumentError(String usage, String message) { | 13 deprecated_argumentError(FastaMessage usage, String message) { |
| 11 if (usage != null) print(usage); | 14 if (usage != null) print(usage.message); |
| 12 deprecated_inputError(null, null, message); | 15 deprecated_inputError(null, null, message); |
| 13 } | 16 } |
| 14 | 17 |
| 18 argumentError(FastaMessage usage, BoundFastaMessage bind) { |
| 19 if (usage != null) print(usage.message); |
| 20 deprecated_inputError(null, null, bind(null, -1).message); |
| 21 } |
| 22 |
| 15 class ParsedArguments { | 23 class ParsedArguments { |
| 16 final Map<String, dynamic> options = <String, dynamic>{}; | 24 final Map<String, dynamic> options = <String, dynamic>{}; |
| 17 final List<String> arguments = <String>[]; | 25 final List<String> arguments = <String>[]; |
| 18 | 26 |
| 19 toString() => "ParsedArguments($options, $arguments)"; | 27 toString() => "ParsedArguments($options, $arguments)"; |
| 20 } | 28 } |
| 21 | 29 |
| 22 class CommandLine { | 30 class CommandLine { |
| 23 final Map<String, dynamic> options; | 31 final Map<String, dynamic> options; |
| 24 | 32 |
| 25 final List<String> arguments; | 33 final List<String> arguments; |
| 26 | 34 |
| 27 final String usage; | 35 final FastaMessage usage; |
| 28 | 36 |
| 29 CommandLine.parsed(ParsedArguments p, this.usage) | 37 CommandLine.parsed(ParsedArguments p, this.usage) |
| 30 : this.options = p.options, | 38 : this.options = p.options, |
| 31 this.arguments = p.arguments { | 39 this.arguments = p.arguments { |
| 32 validate(); | 40 validate(); |
| 33 if (verbose) { | 41 if (verbose) { |
| 34 print(p); | 42 print(p); |
| 35 } | 43 } |
| 36 } | 44 } |
| 37 | 45 |
| 38 CommandLine(List<String> arguments, | 46 CommandLine(List<String> arguments, |
| 39 {Map<String, dynamic> specification, String usage}) | 47 {Map<String, dynamic> specification, FastaMessage usage}) |
| 40 : this.parsed(parse(arguments, specification, usage), usage); | 48 : this.parsed(parse(arguments, specification, usage), usage); |
| 41 | 49 |
| 42 bool get verbose { | 50 bool get verbose { |
| 43 return options.containsKey("-v") || options.containsKey("--verbose"); | 51 return options.containsKey("-v") || options.containsKey("--verbose"); |
| 44 } | 52 } |
| 45 | 53 |
| 46 /// Override to validate arguments and options. | 54 /// Override to validate arguments and options. |
| 47 void validate() {} | 55 void validate() {} |
| 48 | 56 |
| 49 /// Parses a list of command-line [arguments] into options and arguments. | 57 /// Parses a list of command-line [arguments] into options and arguments. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 65 /// with, for example, a `-`). | 73 /// with, for example, a `-`). |
| 66 /// | 74 /// |
| 67 /// Anything that looks like an option is assumed to be a `bool` option set | 75 /// Anything that looks like an option is assumed to be a `bool` option set |
| 68 /// to true, unless it's mentioned in [specification] in which case the | 76 /// to true, unless it's mentioned in [specification] in which case the |
| 69 /// option requires a value, either on the form `--option value` or | 77 /// option requires a value, either on the form `--option value` or |
| 70 /// `--option=value`. | 78 /// `--option=value`. |
| 71 /// | 79 /// |
| 72 /// This method performs only a limited amount of validation, but if an error | 80 /// This method performs only a limited amount of validation, but if an error |
| 73 /// occurs, it will print [usage] along with a specific error message. | 81 /// occurs, it will print [usage] along with a specific error message. |
| 74 static ParsedArguments parse(List<String> arguments, | 82 static ParsedArguments parse(List<String> arguments, |
| 75 Map<String, dynamic> specification, String usage) { | 83 Map<String, dynamic> specification, FastaMessage usage) { |
| 76 specification ??= const <String, dynamic>{}; | 84 specification ??= const <String, dynamic>{}; |
| 77 ParsedArguments result = new ParsedArguments(); | 85 ParsedArguments result = new ParsedArguments(); |
| 78 int index = arguments.indexOf("--"); | 86 int index = arguments.indexOf("--"); |
| 79 Iterable<String> nonOptions = const <String>[]; | 87 Iterable<String> nonOptions = const <String>[]; |
| 80 Iterator<String> iterator = arguments.iterator; | 88 Iterator<String> iterator = arguments.iterator; |
| 81 if (index != -1) { | 89 if (index != -1) { |
| 82 nonOptions = arguments.skip(index + 1); | 90 nonOptions = arguments.skip(index + 1); |
| 83 iterator = arguments.take(index).iterator; | 91 iterator = arguments.take(index).iterator; |
| 84 } | 92 } |
| 85 while (iterator.moveNext()) { | 93 while (iterator.moveNext()) { |
| 86 String argument = iterator.current; | 94 String argument = iterator.current; |
| 87 if (argument.startsWith("-")) { | 95 if (argument.startsWith("-")) { |
| 88 var valueSpecification = specification[argument]; | 96 var valueSpecification = specification[argument]; |
| 89 String value; | 97 String value; |
| 90 if (valueSpecification != null) { | 98 if (valueSpecification != null) { |
| 91 if (!iterator.moveNext()) { | 99 if (!iterator.moveNext()) { |
| 92 return deprecated_argumentError( | 100 return argumentError( |
| 93 usage, "Expected value after '$argument'."); | 101 usage, codeFastaCLIArgumentRequired.bind(argument)); |
| 94 } | 102 } |
| 95 value = iterator.current; | 103 value = iterator.current; |
| 96 } else { | 104 } else { |
| 97 index = argument.indexOf("="); | 105 index = argument.indexOf("="); |
| 98 if (index != -1) { | 106 if (index != -1) { |
| 99 value = argument.substring(index + 1); | 107 value = argument.substring(index + 1); |
| 100 argument = argument.substring(0, index); | 108 argument = argument.substring(0, index); |
| 101 valueSpecification = specification[argument]; | 109 valueSpecification = specification[argument]; |
| 102 } | 110 } |
| 103 } | 111 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 } else if (argument == "/?" || argument == "/h") { | 180 } else if (argument == "/?" || argument == "/h") { |
| 173 result.options[argument] = true; | 181 result.options[argument] = true; |
| 174 } else { | 182 } else { |
| 175 result.arguments.add(argument); | 183 result.arguments.add(argument); |
| 176 } | 184 } |
| 177 } | 185 } |
| 178 result.arguments.addAll(nonOptions); | 186 result.arguments.addAll(nonOptions); |
| 179 return result; | 187 return result; |
| 180 } | 188 } |
| 181 } | 189 } |
| OLD | NEW |