| 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 library rasta.options; | 5 library rasta.options; |
| 6 | 6 |
| 7 import 'dart:async' show | 7 import 'dart:async' show |
| 8 Future; | 8 Future; |
| 9 | 9 |
| 10 import 'dart:isolate' show | 10 import 'dart:isolate' show |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 "--batch": const OptionDescription(Option.batch), | 24 "--batch": const OptionDescription(Option.batch), |
| 25 | 25 |
| 26 "--pattern": const OptionDescription(Option.pattern, hasArgument: true), | 26 "--pattern": const OptionDescription(Option.pattern, hasArgument: true), |
| 27 | 27 |
| 28 "--target": const OptionDescription(Option.target, hasArgument: true), | 28 "--target": const OptionDescription(Option.target, hasArgument: true), |
| 29 | 29 |
| 30 "--throw-on-error": const OptionDescription( | 30 "--throw-on-error": const OptionDescription( |
| 31 Option.throwOnError, hasOptionalArgument: true), | 31 Option.throwOnError, hasOptionalArgument: true), |
| 32 | 32 |
| 33 "--analyze": const OptionDescription(Option.analyze), | 33 "--analyze": const OptionDescription(Option.analyze), |
| 34 |
| 35 "--no-output": const OptionDescription(Option.noOutput), |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 const List<Option> supportedBatchOptions = const <Option>[ | 38 const List<Option> supportedBatchOptions = const <Option>[ |
| 37 Option.batch, | 39 Option.batch, |
| 38 Option.verbose, | 40 Option.verbose, |
| 39 Option.target, | 41 Option.target, |
| 40 Option.throwOnError, | 42 Option.throwOnError, |
| 41 Option.analyze, | 43 Option.analyze, |
| 42 | 44 |
| 43 // TODO(ahe): These options shouldn't be supported in batch server | 45 // TODO(ahe): These options shouldn't be supported in batch server |
| (...skipping 25 matching lines...) Expand all Loading... |
| 69 | 71 |
| 70 /// `--target`. | 72 /// `--target`. |
| 71 target, | 73 target, |
| 72 | 74 |
| 73 /// `--throw-on-error`. | 75 /// `--throw-on-error`. |
| 74 throwOnError, | 76 throwOnError, |
| 75 | 77 |
| 76 /// `--analyze`. | 78 /// `--analyze`. |
| 77 analyze, | 79 analyze, |
| 78 | 80 |
| 81 /// `--no-output`. |
| 82 noOutput, |
| 83 |
| 79 // `--` which signals option parsing should end (the rest are arguments). | 84 // `--` which signals option parsing should end (the rest are arguments). |
| 80 skip, | 85 skip, |
| 81 | 86 |
| 82 // Non-option argument, for example, a file name. | 87 // Non-option argument, for example, a file name. |
| 83 argument, | 88 argument, |
| 84 } | 89 } |
| 85 | 90 |
| 86 class OptionDescription { | 91 class OptionDescription { |
| 87 final Option option; | 92 final Option option; |
| 88 final bool hasArgument; | 93 final bool hasArgument; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 104 final Uri output; | 109 final Uri output; |
| 105 final Uri dependenciesFile; | 110 final Uri dependenciesFile; |
| 106 final Uri targetSpecification; | 111 final Uri targetSpecification; |
| 107 final bool generateLibrary; | 112 final bool generateLibrary; |
| 108 final bool isVerbose; | 113 final bool isVerbose; |
| 109 final bool isBatch; | 114 final bool isBatch; |
| 110 final Pattern pattern; | 115 final Pattern pattern; |
| 111 final bool throwOnError; | 116 final bool throwOnError; |
| 112 final int throwOnErrorCount; | 117 final int throwOnErrorCount; |
| 113 final bool analyze; | 118 final bool analyze; |
| 119 final bool noOutput; |
| 114 | 120 |
| 115 Options(this.input, this.output, | 121 Options(this.input, this.output, |
| 116 {this.dependenciesFile, this.targetSpecification, | 122 {this.dependenciesFile, this.targetSpecification, |
| 117 this.generateLibrary, this.isVerbose, this.isBatch, this.pattern, | 123 this.generateLibrary, this.isVerbose, this.isBatch, this.pattern, |
| 118 this.throwOnError, this.throwOnErrorCount, this.analyze}); | 124 this.throwOnError, this.throwOnErrorCount, this.analyze, |
| 125 this.noOutput}); |
| 119 } | 126 } |
| 120 | 127 |
| 121 class OptionParser { | 128 class OptionParser { |
| 122 final Iterator<String> arguments; | 129 final Iterator<String> arguments; |
| 123 final Uri base; | 130 final Uri base; |
| 124 | 131 |
| 125 OptionParser(Iterable<String> arguments, this.base) | 132 OptionParser(Iterable<String> arguments, this.base) |
| 126 : arguments = arguments.iterator; | 133 : arguments = arguments.iterator; |
| 127 | 134 |
| 128 Future<Options> parse() async { | 135 Future<Options> parse() async { |
| 129 List<OptionValue> values = parseValues(); | 136 List<OptionValue> values = parseValues(); |
| 130 | 137 |
| 131 // Null means not specified. | 138 // Null means not specified. |
| 132 bool generateLibrary; | 139 bool generateLibrary; |
| 133 bool isVerbose; | 140 bool isVerbose; |
| 134 bool isBatch; | 141 bool isBatch; |
| 135 bool throwOnError; | 142 bool throwOnError; |
| 136 int throwOnErrorCount = 1; | 143 int throwOnErrorCount = 1; |
| 137 bool analyze; | 144 bool analyze; |
| 138 Uri dependenciesFile; | 145 Uri dependenciesFile; |
| 139 Uri targetSpecification; | 146 Uri targetSpecification; |
| 140 List<String> uris = <String>[]; | 147 List<String> uris = <String>[]; |
| 141 Pattern pattern; | 148 Pattern pattern; |
| 149 bool noOutput; |
| 142 for (OptionValue value in values) { | 150 for (OptionValue value in values) { |
| 143 switch (value.option) { | 151 switch (value.option) { |
| 144 case Option.library: | 152 case Option.library: |
| 145 generateLibrary = true; | 153 generateLibrary = true; |
| 146 break; | 154 break; |
| 147 | 155 |
| 148 case Option.program: | 156 case Option.program: |
| 149 generateLibrary = false; | 157 generateLibrary = false; |
| 150 break; | 158 break; |
| 151 | 159 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 175 throwOnError = true; | 183 throwOnError = true; |
| 176 if (value.argument != null) { | 184 if (value.argument != null) { |
| 177 throwOnErrorCount = int.parse(value.argument); | 185 throwOnErrorCount = int.parse(value.argument); |
| 178 } | 186 } |
| 179 break; | 187 break; |
| 180 | 188 |
| 181 case Option.analyze: | 189 case Option.analyze: |
| 182 analyze = true; | 190 analyze = true; |
| 183 break; | 191 break; |
| 184 | 192 |
| 193 case Option.noOutput: |
| 194 noOutput = true; |
| 195 break; |
| 196 |
| 185 case Option.skip: | 197 case Option.skip: |
| 186 throw "internal error"; | 198 throw "internal error"; |
| 187 | 199 |
| 188 case Option.argument: | 200 case Option.argument: |
| 189 uris.add(value.argument); | 201 uris.add(value.argument); |
| 190 } | 202 } |
| 191 } | 203 } |
| 192 if (targetSpecification == null) { | 204 if (targetSpecification == null) { |
| 193 Uri script = await Isolate.resolvePackageUri( | 205 Uri script = await Isolate.resolvePackageUri( |
| 194 Uri.parse("package:rasta/src/options.dart")); | 206 Uri.parse("package:rasta/src/options.dart")); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 assert(uris.length > 2); | 249 assert(uris.length > 2); |
| 238 String extraArguments = uris.join("' '"); | 250 String extraArguments = uris.join("' '"); |
| 239 throw "Additional argument: '$extraArguments'."; | 251 throw "Additional argument: '$extraArguments'."; |
| 240 } | 252 } |
| 241 return new Options( | 253 return new Options( |
| 242 input, output, dependenciesFile: dependenciesFile, | 254 input, output, dependenciesFile: dependenciesFile, |
| 243 targetSpecification: targetSpecification, | 255 targetSpecification: targetSpecification, |
| 244 generateLibrary: generateLibrary, isVerbose: isVerbose ?? false, | 256 generateLibrary: generateLibrary, isVerbose: isVerbose ?? false, |
| 245 isBatch: isBatch ?? false, pattern: pattern, | 257 isBatch: isBatch ?? false, pattern: pattern, |
| 246 throwOnError: throwOnError ?? false, | 258 throwOnError: throwOnError ?? false, |
| 247 throwOnErrorCount: throwOnErrorCount, analyze: analyze ?? false); | 259 throwOnErrorCount: throwOnErrorCount, analyze: analyze ?? false, |
| 260 noOutput: noOutput ?? false); |
| 248 } | 261 } |
| 249 | 262 |
| 250 List<OptionValue> parseValues() { | 263 List<OptionValue> parseValues() { |
| 251 OptionValue value; | 264 OptionValue value; |
| 252 List<OptionValue> values = <OptionValue>[]; | 265 List<OptionValue> values = <OptionValue>[]; |
| 253 while ((value = nextOptionValue()) != null) { | 266 while ((value = nextOptionValue()) != null) { |
| 254 if (value.option == Option.skip) break; | 267 if (value.option == Option.skip) break; |
| 255 values.add(value); | 268 values.add(value); |
| 256 } | 269 } |
| 257 while (arguments.moveNext()) { | 270 while (arguments.moveNext()) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 return new OptionValue(description.option, argument); | 303 return new OptionValue(description.option, argument); |
| 291 } | 304 } |
| 292 | 305 |
| 293 String getArgument(String name) { | 306 String getArgument(String name) { |
| 294 if (!arguments.moveNext()) { | 307 if (!arguments.moveNext()) { |
| 295 throw "Missing argument to option '$name'"; | 308 throw "Missing argument to option '$name'"; |
| 296 } | 309 } |
| 297 return arguments.current; | 310 return arguments.current; |
| 298 } | 311 } |
| 299 } | 312 } |
| OLD | NEW |