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