OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library options; |
| 6 |
| 7 import 'dart:io'; |
| 8 |
| 9 import 'package:args/args.dart'; |
| 10 |
| 11 const _BINARY_NAME = 'dartanalyzer'; |
| 12 |
| 13 /** |
| 14 * Analyzer commandline configuration options. |
| 15 */ |
| 16 class CommandLineOptions { |
| 17 /** The path to the dart SDK */ |
| 18 final String dartSdkPath; |
| 19 |
| 20 /** A table mapping the names of defined variables to their values. */ |
| 21 final Map<String, String> definedVariables; |
| 22 |
| 23 /** Whether to report hints */ |
| 24 final bool disableHints; |
| 25 |
| 26 /** Whether to display version information */ |
| 27 final bool displayVersion; |
| 28 |
| 29 /** Whether to enable support for the proposed async feature. */ |
| 30 final bool enableAsync; |
| 31 |
| 32 /** Whether to enable support for the proposed enum feature. */ |
| 33 final bool enableEnum; |
| 34 |
| 35 /** Whether to ignore unrecognized flags */ |
| 36 final bool ignoreUnrecognizedFlags; |
| 37 |
| 38 /** Whether to log additional analysis messages and exceptions */ |
| 39 final bool log; |
| 40 |
| 41 /** Whether to use machine format for error display */ |
| 42 final bool machineFormat; |
| 43 |
| 44 /** The path to the package root */ |
| 45 final String packageRootPath; |
| 46 |
| 47 /** Whether to show performance statistics */ |
| 48 final bool perf; |
| 49 |
| 50 /** Batch mode (for unit testing) */ |
| 51 final bool shouldBatch; |
| 52 |
| 53 /** Whether to show package: warnings */ |
| 54 final bool showPackageWarnings; |
| 55 |
| 56 /** Whether to show SDK warnings */ |
| 57 final bool showSdkWarnings; |
| 58 |
| 59 /** The source files to analyze */ |
| 60 final List<String> sourceFiles; |
| 61 |
| 62 /** Whether to show both cold and hot performance statistics */ |
| 63 final bool warmPerf; |
| 64 |
| 65 /** Whether to treat warnings as fatal */ |
| 66 final bool warningsAreFatal; |
| 67 |
| 68 /** |
| 69 * Initialize options from the given parsed [args]. |
| 70 */ |
| 71 CommandLineOptions._fromArgs(ArgResults args, Map<String, String> definedVaria
bles) |
| 72 : dartSdkPath = args['dart-sdk'], |
| 73 this.definedVariables = definedVariables, |
| 74 disableHints = args['no-hints'], |
| 75 displayVersion = args['version'], |
| 76 enableAsync = args['enable-async'], |
| 77 enableEnum = args['enable-enum'], |
| 78 ignoreUnrecognizedFlags = args['ignore-unrecognized-flags'], |
| 79 log = args['log'], |
| 80 machineFormat = args['machine'] || args['format'] == 'machine', |
| 81 packageRootPath = args['package-root'], |
| 82 perf = args['perf'], |
| 83 shouldBatch = args['batch'], |
| 84 showPackageWarnings = args['show-package-warnings'] || args['package-warni
ngs'], |
| 85 showSdkWarnings = args['show-sdk-warnings'] || args['warnings'], |
| 86 sourceFiles = args.rest, |
| 87 warmPerf = args['warm-perf'], |
| 88 warningsAreFatal = args['fatal-warnings']; |
| 89 |
| 90 /** |
| 91 * Parse [args] into [CommandLineOptions] describing the specified |
| 92 * analyzer options. In case of a format error, prints error and exists. |
| 93 */ |
| 94 static CommandLineOptions parse(List<String> args) { |
| 95 CommandLineOptions options = _parse(args); |
| 96 // check SDK |
| 97 { |
| 98 var sdkPath = options.dartSdkPath; |
| 99 // check that SDK is specified |
| 100 if (sdkPath == null) { |
| 101 print('Usage: $_BINARY_NAME: no Dart SDK found.'); |
| 102 exit(15); |
| 103 } |
| 104 // check that SDK is existing directory |
| 105 if (!(new Directory(sdkPath)).existsSync()) { |
| 106 print('Usage: $_BINARY_NAME: invalid Dart SDK path: $sdkPath'); |
| 107 exit(15); |
| 108 } |
| 109 } |
| 110 // OK |
| 111 return options; |
| 112 } |
| 113 |
| 114 static CommandLineOptions _parse(List<String> args) { |
| 115 args = args.expand((String arg) => arg.split('=')).toList(); |
| 116 var parser = new _CommandLineParser() |
| 117 ..addFlag('batch', abbr: 'b', help: 'Run in batch mode', |
| 118 defaultsTo: false, negatable: false) |
| 119 ..addOption('dart-sdk', help: 'The path to the Dart SDK') |
| 120 ..addOption('package-root', abbr: 'p', |
| 121 help: 'The path to the package root') |
| 122 ..addOption('format', |
| 123 help: 'Specifies the format in which errors are displayed') |
| 124 ..addFlag('machine', |
| 125 help: 'Print errors in a format suitable for parsing (deprecated)', |
| 126 defaultsTo: false, negatable: false) |
| 127 ..addFlag('version', help: 'Print the analyzer version', |
| 128 defaultsTo: false, negatable: false) |
| 129 ..addFlag('no-hints', help: 'Do not show hint results', |
| 130 defaultsTo: false, negatable: false) |
| 131 ..addFlag('ignore-unrecognized-flags', |
| 132 help: 'Ignore unrecognized command line flags', |
| 133 defaultsTo: false, negatable: false) |
| 134 ..addFlag('fatal-warnings', help: 'Treat non-type warnings as fatal', |
| 135 defaultsTo: false, negatable: false) |
| 136 ..addFlag('package-warnings', |
| 137 help: 'Show warnings from package: imports', |
| 138 defaultsTo: false, negatable: false) |
| 139 ..addFlag('show-package-warnings', |
| 140 help: 'Show warnings from package: imports (deprecated)', |
| 141 defaultsTo: false, negatable: false) |
| 142 ..addFlag('perf', |
| 143 help: 'Show performance statistics', |
| 144 defaultsTo: false, negatable: false) |
| 145 ..addFlag('warnings', help: 'Show warnings from SDK imports', |
| 146 defaultsTo: false, negatable: false) |
| 147 ..addFlag('show-sdk-warnings', help: 'Show warnings from SDK imports (depr
ecated)', |
| 148 defaultsTo: false, negatable: false) |
| 149 ..addFlag('help', abbr: 'h', help: 'Display this help message', |
| 150 defaultsTo: false, negatable: false) |
| 151 // |
| 152 // Hidden flags. |
| 153 // |
| 154 ..addFlag('enable-async', |
| 155 help: 'Enable support for the proposed async feature', |
| 156 defaultsTo: false, negatable: false, hide: true) |
| 157 ..addFlag('enable-enum', |
| 158 help: 'Enable support for the proposed enum feature', |
| 159 defaultsTo: false, negatable: false, hide: true) |
| 160 ..addFlag('log', help: 'Log additional messages and exceptions', |
| 161 defaultsTo: false, negatable: false, hide: true) |
| 162 ..addFlag('warm-perf', |
| 163 help: 'Show both cold and warm performance statistics', |
| 164 defaultsTo: false, negatable: false, hide: true); |
| 165 |
| 166 try { |
| 167 // TODO(scheglov) https://code.google.com/p/dart/issues/detail?id=11061 |
| 168 args = args.map((String arg) => arg == '-batch' ? '--batch' : arg).toList(
); |
| 169 Map<String, String> definedVariables = <String, String>{}; |
| 170 var results = parser.parse(args, definedVariables); |
| 171 // help requests |
| 172 if (results['help']) { |
| 173 _showUsage(parser); |
| 174 exit(0); |
| 175 } |
| 176 // batch mode and input files |
| 177 if (results['batch']) { |
| 178 if (results.rest.isNotEmpty) { |
| 179 print('No source files expected in the batch mode.'); |
| 180 _showUsage(parser); |
| 181 exit(15); |
| 182 } |
| 183 } else if (results['version']) { |
| 184 print('$_BINARY_NAME version ${_getVersion()}'); |
| 185 exit(0); |
| 186 } else { |
| 187 if (results.rest.isEmpty) { |
| 188 _showUsage(parser); |
| 189 exit(15); |
| 190 } |
| 191 } |
| 192 return new CommandLineOptions._fromArgs(results, definedVariables); |
| 193 } on FormatException catch (e) { |
| 194 print(e.message); |
| 195 _showUsage(parser); |
| 196 exit(15); |
| 197 } |
| 198 |
| 199 } |
| 200 |
| 201 static _showUsage(parser) { |
| 202 print('Usage: $_BINARY_NAME [options...] <libraries to analyze...>'); |
| 203 print(parser.getUsage()); |
| 204 print(''); |
| 205 print('For more information, see http://www.dartlang.org/tools/analyzer.'); |
| 206 } |
| 207 |
| 208 static String _getVersion() { |
| 209 try { |
| 210 // This is relative to bin/snapshot, so ../.. |
| 211 String versionPath = |
| 212 Platform.script.resolve('../../version').toFilePath(); |
| 213 File versionFile = new File(versionPath); |
| 214 return versionFile.readAsStringSync().trim(); |
| 215 } catch (_) { |
| 216 // This happens when the script is not running in the context of an SDK. |
| 217 return "<unknown>"; |
| 218 } |
| 219 } |
| 220 } |
| 221 |
| 222 /** |
| 223 * Commandline argument parser. |
| 224 * |
| 225 * TODO(pquitslund): when the args package supports ignoring unrecognized |
| 226 * options/flags, this class can be replaced with a simple [ArgParser] instance. |
| 227 */ |
| 228 class _CommandLineParser { |
| 229 |
| 230 final List<String> _knownFlags; |
| 231 final ArgParser _parser; |
| 232 |
| 233 /** Creates a new command line parser */ |
| 234 _CommandLineParser() |
| 235 : _knownFlags = <String>[], |
| 236 _parser = new ArgParser(); |
| 237 |
| 238 |
| 239 /** |
| 240 * Defines a flag. |
| 241 * |
| 242 * See [ArgParser.addFlag()]. |
| 243 */ |
| 244 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, |
| 245 bool negatable: true, void callback(bool value), bool hide: false}) { |
| 246 _knownFlags.add(name); |
| 247 _parser.addFlag(name, abbr: abbr, help: help, defaultsTo: defaultsTo, |
| 248 negatable: negatable, callback: callback, hide: hide); |
| 249 } |
| 250 |
| 251 /** |
| 252 * Defines a value-taking option. |
| 253 * |
| 254 * See [ArgParser.addOption()]. |
| 255 */ |
| 256 void addOption(String name, {String abbr, String help, List<String> allowed, |
| 257 Map<String, String> allowedHelp, String defaultsTo, |
| 258 void callback(value), bool allowMultiple: false}) { |
| 259 _knownFlags.add(name); |
| 260 _parser.addOption(name, abbr: abbr, help: help, allowed: allowed, |
| 261 allowedHelp: allowedHelp, defaultsTo: defaultsTo, callback: callback, |
| 262 allowMultiple: allowMultiple); |
| 263 } |
| 264 |
| 265 |
| 266 /** |
| 267 * Generates a string displaying usage information for the defined options. |
| 268 * |
| 269 * See [ArgParser.getUsage()]. |
| 270 */ |
| 271 String getUsage() => _parser.getUsage(); |
| 272 |
| 273 /** |
| 274 * Parses [args], a list of command-line arguments, matches them against the |
| 275 * flags and options defined by this parser, and returns the result. The |
| 276 * values of any defined variables are captured in the given map. |
| 277 * |
| 278 * See [ArgParser]. |
| 279 */ |
| 280 ArgResults parse(List<String> args, Map<String, String> definedVariables) => _
parser.parse(_filterUnknowns(parseDefinedVariables(args, definedVariables))); |
| 281 |
| 282 List<String> parseDefinedVariables(List<String> args, Map<String, String> defi
nedVariables) { |
| 283 int count = args.length; |
| 284 List<String> remainingArgs = <String>[]; |
| 285 for (int i = 0; i < count; i++) { |
| 286 String arg = args[i]; |
| 287 if (arg == '--') { |
| 288 while (i < count) { |
| 289 remainingArgs.add(args[i++]); |
| 290 } |
| 291 } else if (arg.startsWith("-D")) { |
| 292 definedVariables[arg.substring(2)] = args[++i]; |
| 293 } else { |
| 294 remainingArgs.add(arg); |
| 295 } |
| 296 } |
| 297 return remainingArgs; |
| 298 } |
| 299 |
| 300 List<String> _filterUnknowns(args) { |
| 301 |
| 302 // Only filter args if the ignore flag is specified. |
| 303 if (!args.contains('--ignore-unrecognized-flags')) { |
| 304 return args; |
| 305 } |
| 306 |
| 307 //TODO(pquitslund): replace w/ the following once library skew issues are so
rted out |
| 308 //return args.where((arg) => !arg.startsWith('--') || |
| 309 // _knownFlags.contains(arg.substring(2))); |
| 310 |
| 311 // Filter all unrecognized flags and options. |
| 312 var filtered = <String>[]; |
| 313 for (var i=0; i < args.length; ++i) { |
| 314 var arg = args[i]; |
| 315 if (arg.startsWith('--') && arg.length > 2) { |
| 316 if (!_knownFlags.contains(arg.substring(2))) { |
| 317 print('remove: $arg'); |
| 318 //"eat" params by advancing to the next flag/option |
| 319 i = _getNextFlagIndex(args, i); |
| 320 } else { |
| 321 filtered.add(arg); |
| 322 } |
| 323 } else { |
| 324 filtered.add(arg); |
| 325 } |
| 326 } |
| 327 |
| 328 return filtered; |
| 329 } |
| 330 |
| 331 _getNextFlagIndex(args, i) { |
| 332 for ( ; i < args.length; ++i) { |
| 333 if (args[i].startsWith('--')) { |
| 334 return i; |
| 335 } |
| 336 } |
| 337 return i; |
| 338 } |
| 339 } |
OLD | NEW |