| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 options; | 5 library options; |
| 6 | 6 |
| 7 import 'package:args/args.dart'; | 7 import 'package:args/args.dart'; |
| 8 | 8 |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 | 11 |
| 12 const _BINARY_NAME = 'analyzer'; | 12 const _BINARY_NAME = 'analyzer'; |
| 13 const _SDK_ENV = 'com.google.dart.sdk'; | |
| 14 final _DEFAULT_SDK_LOCATION = Platform.environment[_SDK_ENV]; | |
| 15 | 13 |
| 16 /** | 14 /** |
| 17 * Analyzer commandline configuration options. | 15 * Analyzer commandline configuration options. |
| 18 */ | 16 */ |
| 19 class CommandLineOptions { | 17 class CommandLineOptions { |
| 20 | 18 |
| 21 /** Batch mode (for unit testing) */ | 19 /** Batch mode (for unit testing) */ |
| 22 final bool shouldBatch; | 20 final bool shouldBatch; |
| 23 | 21 |
| 24 /** Whether to use machine format for error display */ | 22 /** Whether to use machine format for error display */ |
| 25 final bool machineFormat; | 23 final bool machineFormat; |
| 26 | 24 |
| 27 /** Whether to ignore unrecognized flags */ | 25 /** Whether to ignore unrecognized flags */ |
| 28 final bool ignoreUnrecognizedFlags; | 26 final bool ignoreUnrecognizedFlags; |
| 29 | 27 |
| 30 /** Whether to print metrics */ | 28 /** Whether to show package: warnings */ |
| 31 final bool showMetrics; | 29 final bool showPackageWarnings; |
| 30 |
| 31 /** Whether to show SDK warnings */ |
| 32 final bool showSdkWarnings; |
| 32 | 33 |
| 33 /** Whether to treat warnings as fatal */ | 34 /** Whether to treat warnings as fatal */ |
| 34 final bool warningsAreFatal; | 35 final bool warningsAreFatal; |
| 35 | 36 |
| 36 /** The path to the dart SDK */ | 37 /** The path to the dart SDK */ |
| 37 final String dartSdkPath; | 38 final String dartSdkPath; |
| 38 | 39 |
| 39 /** The source files to analyze */ | 40 /** The source files to analyze */ |
| 40 final List<String> sourceFiles; | 41 final List<String> sourceFiles; |
| 41 | 42 |
| 42 /** | 43 /** |
| 43 * Initialize options from the given parsed [args]. | 44 * Initialize options from the given parsed [args]. |
| 44 */ | 45 */ |
| 45 CommandLineOptions._fromArgs(ArgResults args) | 46 CommandLineOptions._fromArgs(ArgResults args) |
| 46 : shouldBatch = args['batch'], | 47 : shouldBatch = args['batch'], |
| 47 machineFormat = args['machine_format'], | 48 machineFormat = args['machine_format'], |
| 48 ignoreUnrecognizedFlags = args['ignore_unrecognized_flags'], | 49 ignoreUnrecognizedFlags = args['ignore_unrecognized_flags'], |
| 49 showMetrics = args['metrics'], | 50 showPackageWarnings = args['show_package_warnings'], |
| 51 showSdkWarnings = args['show_sdk_warnings'], |
| 50 warningsAreFatal = args['fatal_warnings'], | 52 warningsAreFatal = args['fatal_warnings'], |
| 51 dartSdkPath = args['dart_sdk'], | 53 dartSdkPath = args['dart_sdk'], |
| 52 sourceFiles = args.rest; | 54 sourceFiles = args.rest; |
| 53 | 55 |
| 54 /** | 56 /** |
| 55 * Parse [args] into [CommandLineOptions] describing the specified | 57 * Parse [args] into [CommandLineOptions] describing the specified |
| 56 * analyzer options. In case of a format error, [null] is returned. | 58 * analyzer options. In case of a format error, prints error and exists. |
| 57 */ | 59 */ |
| 58 factory CommandLineOptions.parse(List<String> args) { | 60 static CommandLineOptions parse(List<String> args) { |
| 61 CommandLineOptions options = _parse(args); |
| 62 // check SDK |
| 63 { |
| 64 var sdkPath = options.dartSdkPath; |
| 65 // check that SDK is specified |
| 66 if (sdkPath == null) { |
| 67 print('Usage: $_BINARY_NAME: no Dart SDK found.'); |
| 68 exit(15); |
| 69 } |
| 70 // check that SDK is existing directory |
| 71 if (!(new Directory(sdkPath)).existsSync()) { |
| 72 print('Usage: $_BINARY_NAME: invalid Dart SDK path: $sdkPath'); |
| 73 exit(15); |
| 74 } |
| 75 } |
| 76 // OK |
| 77 return options; |
| 78 } |
| 59 | 79 |
| 80 static CommandLineOptions _parse(List<String> args) { |
| 60 var parser = new _CommandLineParser() | 81 var parser = new _CommandLineParser() |
| 61 ..addFlag('batch', abbr: 'b', help: 'Run in batch mode', | 82 ..addFlag('batch', abbr: 'b', help: 'Run in batch mode', |
| 62 defaultsTo: false, negatable: false) | 83 defaultsTo: false, negatable: false) |
| 63 ..addOption('dart_sdk', help: 'Specify path to the Dart sdk', | 84 ..addOption('dart_sdk', help: 'Specify path to the Dart sdk') |
| 64 defaultsTo: _DEFAULT_SDK_LOCATION) | |
| 65 ..addFlag('machine_format', help: 'Specify whether errors ' | 85 ..addFlag('machine_format', help: 'Specify whether errors ' |
| 66 'should be in machine format', | 86 'should be in machine format', |
| 67 defaultsTo: false, negatable: false) | 87 defaultsTo: false, negatable: false) |
| 68 ..addFlag('ignore_unrecognized_flags', | 88 ..addFlag('ignore_unrecognized_flags', |
| 69 help: 'Ignore unrecognized command line flags', | 89 help: 'Ignore unrecognized command line flags', |
| 70 defaultsTo: false, negatable: false) | 90 defaultsTo: false, negatable: false) |
| 71 ..addFlag('fatal_warnings', help: 'Treat non-type warnings as fatal', | 91 ..addFlag('fatal_warnings', help: 'Treat non-type warnings as fatal', |
| 72 defaultsTo: false, negatable: false) | 92 defaultsTo: false, negatable: false) |
| 73 ..addFlag('metrics', help: 'Print metrics', | 93 ..addFlag('show_package_warnings', help: 'Show warnings from package: impo
rts', |
| 74 defaultsTo: false, negatable: false) | 94 defaultsTo: false, negatable: false) |
| 95 ..addFlag('show_sdk_warnings', help: 'Show warnings from SDK imports', |
| 96 defaultsTo: false, negatable: false) |
| 75 ..addFlag('help', abbr: 'h', help: 'Display this help message', | 97 ..addFlag('help', abbr: 'h', help: 'Display this help message', |
| 76 defaultsTo: false, negatable: false); | 98 defaultsTo: false, negatable: false); |
| 77 | 99 |
| 78 try { | 100 try { |
| 79 var results = parser.parse(args); | 101 var results = parser.parse(args); |
| 80 if (results['help'] || results.rest.length == 0) { | 102 // help requests |
| 103 if (results['help']) { |
| 81 _showUsage(parser); | 104 _showUsage(parser); |
| 82 return null; | 105 exit(0); |
| 106 } |
| 107 // batch mode and input files |
| 108 if (results['batch']) { |
| 109 if (results.rest.length != 0) { |
| 110 print('No source files expected in the batch mode.'); |
| 111 _showUsage(parser); |
| 112 exit(15); |
| 113 } |
| 114 } else { |
| 115 if (results.rest.length == 0) { |
| 116 _showUsage(parser); |
| 117 exit(15); |
| 118 } |
| 83 } | 119 } |
| 84 return new CommandLineOptions._fromArgs(results); | 120 return new CommandLineOptions._fromArgs(results); |
| 85 } on FormatException catch (e) { | 121 } on FormatException catch (e) { |
| 86 print(e.message); | 122 print(e.message); |
| 87 _showUsage(parser); | 123 _showUsage(parser); |
| 88 return null; | 124 exit(15); |
| 89 } | 125 } |
| 90 | 126 |
| 91 } | 127 } |
| 92 | 128 |
| 93 static _showUsage(parser) { | 129 static _showUsage(parser) { |
| 94 print('Usage: ${_BINARY_NAME} [options...] ' | 130 print('Usage: $_BINARY_NAME [options...] <libraries to analyze...>'); |
| 95 '<libraries to analyze...>'); | |
| 96 print(parser.getUsage()); | 131 print(parser.getUsage()); |
| 97 } | 132 } |
| 98 | 133 |
| 99 } | 134 } |
| 100 | 135 |
| 101 /** | 136 /** |
| 102 * Commandline argument parser. | 137 * Commandline argument parser. |
| 103 * | 138 * |
| 104 * TODO(pquitslund): when the args package supports ignoring unrecognized | 139 * TODO(pquitslund): when the args package supports ignoring unrecognized |
| 105 * options/flags, this class can be replaced with a simple [ArgParser] instance. | 140 * options/flags, this class can be replaced with a simple [ArgParser] instance. |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 } | 222 } |
| 188 | 223 |
| 189 _getNextFlagIndex(args, i) { | 224 _getNextFlagIndex(args, i) { |
| 190 for ( ; i < args.length; ++i) { | 225 for ( ; i < args.length; ++i) { |
| 191 if (args[i].startsWith('--')) { | 226 if (args[i].startsWith('--')) { |
| 192 return i; | 227 return i; |
| 193 } | 228 } |
| 194 } | 229 } |
| 195 return i; | 230 return i; |
| 196 } | 231 } |
| 197 | |
| 198 } | 232 } |
| 199 | |
| OLD | NEW |