| 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 = 'dartanalyzer'; |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Analyzer commandline configuration options. | 15 * Analyzer commandline configuration options. |
| 16 */ | 16 */ |
| 17 class CommandLineOptions { | 17 class CommandLineOptions { |
| 18 | 18 |
| 19 /** Batch mode (for unit testing) */ | 19 /** Batch mode (for unit testing) */ |
| 20 final bool shouldBatch; | 20 final bool shouldBatch; |
| 21 | 21 |
| 22 /** Whether to use machine format for error display */ | 22 /** Whether to use machine format for error display */ |
| 23 final bool machineFormat; | 23 final bool machineFormat; |
| 24 | 24 |
| 25 /** Whether to display version information */ |
| 26 final bool displayVersion; |
| 27 |
| 25 /** Whether to ignore unrecognized flags */ | 28 /** Whether to ignore unrecognized flags */ |
| 26 final bool ignoreUnrecognizedFlags; | 29 final bool ignoreUnrecognizedFlags; |
| 27 | 30 |
| 28 /** Whether to show package: warnings */ | 31 /** Whether to show package: warnings */ |
| 29 final bool showPackageWarnings; | 32 final bool showPackageWarnings; |
| 30 | 33 |
| 31 /** Whether to show SDK warnings */ | 34 /** Whether to show SDK warnings */ |
| 32 final bool showSdkWarnings; | 35 final bool showSdkWarnings; |
| 33 | 36 |
| 34 /** Whether to treat warnings as fatal */ | 37 /** Whether to treat warnings as fatal */ |
| 35 final bool warningsAreFatal; | 38 final bool warningsAreFatal; |
| 36 | 39 |
| 37 /** The path to the dart SDK */ | 40 /** The path to the dart SDK */ |
| 38 final String dartSdkPath; | 41 final String dartSdkPath; |
| 39 | 42 |
| 40 /** The path to the package root */ | 43 /** The path to the package root */ |
| 41 final String packageRootPath; | 44 final String packageRootPath; |
| 42 | 45 |
| 43 /** The source files to analyze */ | 46 /** The source files to analyze */ |
| 44 final List<String> sourceFiles; | 47 final List<String> sourceFiles; |
| 45 | 48 |
| 46 /** | 49 /** |
| 47 * Initialize options from the given parsed [args]. | 50 * Initialize options from the given parsed [args]. |
| 48 */ | 51 */ |
| 49 CommandLineOptions._fromArgs(ArgResults args) | 52 CommandLineOptions._fromArgs(ArgResults args) |
| 50 : shouldBatch = args['batch'], | 53 : shouldBatch = args['batch'], |
| 51 machineFormat = args['machine-format'], | 54 machineFormat = args['machine'], |
| 55 displayVersion = args['version'], |
| 52 ignoreUnrecognizedFlags = args['ignore-unrecognized-flags'], | 56 ignoreUnrecognizedFlags = args['ignore-unrecognized-flags'], |
| 53 showPackageWarnings = args['show-package-warnings'], | 57 showPackageWarnings = args['show-package-warnings'], |
| 54 showSdkWarnings = args['show-sdk-warnings'], | 58 showSdkWarnings = args['show-sdk-warnings'], |
| 55 warningsAreFatal = args['fatal-warnings'], | 59 warningsAreFatal = args['fatal-warnings'], |
| 56 dartSdkPath = args['dart-sdk'], | 60 dartSdkPath = args['dart-sdk'], |
| 57 packageRootPath = args['package-root'], | 61 packageRootPath = args['package-root'], |
| 58 sourceFiles = args.rest; | 62 sourceFiles = args.rest; |
| 59 | 63 |
| 60 /** | 64 /** |
| 61 * Parse [args] into [CommandLineOptions] describing the specified | 65 * Parse [args] into [CommandLineOptions] describing the specified |
| (...skipping 18 matching lines...) Expand all Loading... |
| 80 // OK | 84 // OK |
| 81 return options; | 85 return options; |
| 82 } | 86 } |
| 83 | 87 |
| 84 static CommandLineOptions _parse(List<String> args) { | 88 static CommandLineOptions _parse(List<String> args) { |
| 85 var parser = new _CommandLineParser() | 89 var parser = new _CommandLineParser() |
| 86 ..addFlag('batch', abbr: 'b', help: 'Run in batch mode', | 90 ..addFlag('batch', abbr: 'b', help: 'Run in batch mode', |
| 87 defaultsTo: false, negatable: false) | 91 defaultsTo: false, negatable: false) |
| 88 ..addOption('dart-sdk', help: 'The path to the Dart SDK') | 92 ..addOption('dart-sdk', help: 'The path to the Dart SDK') |
| 89 ..addOption('package-root', help: 'The path to the package root') | 93 ..addOption('package-root', help: 'The path to the package root') |
| 90 ..addFlag('machine-format', help: 'Specify whether errors ' | 94 ..addFlag('machine', |
| 91 'should be in machine format', | 95 help: 'Print errors in a format suitable for parsing', |
| 96 defaultsTo: false, negatable: false) |
| 97 ..addFlag('version', help: 'Print the analyzer version', |
| 92 defaultsTo: false, negatable: false) | 98 defaultsTo: false, negatable: false) |
| 93 ..addFlag('ignore-unrecognized-flags', | 99 ..addFlag('ignore-unrecognized-flags', |
| 94 help: 'Ignore unrecognized command line flags', | 100 help: 'Ignore unrecognized command line flags', |
| 95 defaultsTo: false, negatable: false) | 101 defaultsTo: false, negatable: false) |
| 96 ..addFlag('fatal-warnings', help: 'Treat non-type warnings as fatal', | 102 ..addFlag('fatal-warnings', help: 'Treat non-type warnings as fatal', |
| 97 defaultsTo: false, negatable: false) | 103 defaultsTo: false, negatable: false) |
| 98 ..addFlag('show-package-warnings', help: 'Show warnings from package: impo
rts', | 104 ..addFlag('show-package-warnings', |
| 99 defaultsTo: false, negatable: false) | 105 help: 'Show warnings from package: imports', |
| 106 defaultsTo: false, negatable: false) |
| 100 ..addFlag('show-sdk-warnings', help: 'Show warnings from SDK imports', | 107 ..addFlag('show-sdk-warnings', help: 'Show warnings from SDK imports', |
| 101 defaultsTo: false, negatable: false) | 108 defaultsTo: false, negatable: false) |
| 102 ..addFlag('help', abbr: 'h', help: 'Display this help message', | 109 ..addFlag('help', abbr: 'h', help: 'Display this help message', |
| 103 defaultsTo: false, negatable: false); | 110 defaultsTo: false, negatable: false); |
| 104 | 111 |
| 105 try { | 112 try { |
| 106 var results = parser.parse(args); | 113 var results = parser.parse(args); |
| 107 // help requests | 114 // help requests |
| 108 if (results['help']) { | 115 if (results['help']) { |
| 109 _showUsage(parser); | 116 _showUsage(parser); |
| 110 exit(0); | 117 exit(0); |
| 111 } | 118 } |
| 112 // batch mode and input files | 119 // batch mode and input files |
| 113 if (results['batch']) { | 120 if (results['batch']) { |
| 114 if (results.rest.length != 0) { | 121 if (results.rest.length != 0) { |
| 115 print('No source files expected in the batch mode.'); | 122 print('No source files expected in the batch mode.'); |
| 116 _showUsage(parser); | 123 _showUsage(parser); |
| 117 exit(15); | 124 exit(15); |
| 118 } | 125 } |
| 126 } if (results['version']) { |
| 127 print('$_BINARY_NAME version ${_getVersion()}'); |
| 128 exit(0); |
| 119 } else { | 129 } else { |
| 120 if (results.rest.length == 0) { | 130 if (results.rest.length == 0) { |
| 121 _showUsage(parser); | 131 _showUsage(parser); |
| 122 exit(15); | 132 exit(15); |
| 123 } | 133 } |
| 124 } | 134 } |
| 125 return new CommandLineOptions._fromArgs(results); | 135 return new CommandLineOptions._fromArgs(results); |
| 126 } on FormatException catch (e) { | 136 } on FormatException catch (e) { |
| 127 print(e.message); | 137 print(e.message); |
| 128 _showUsage(parser); | 138 _showUsage(parser); |
| 129 exit(15); | 139 exit(15); |
| 130 } | 140 } |
| 131 | 141 |
| 132 } | 142 } |
| 133 | 143 |
| 134 static _showUsage(parser) { | 144 static _showUsage(parser) { |
| 135 print('Usage: $_BINARY_NAME [options...] <libraries to analyze...>'); | 145 print('Usage: $_BINARY_NAME [options...] <libraries to analyze...>'); |
| 136 print(parser.getUsage()); | 146 print(parser.getUsage()); |
| 147 print(''); |
| 148 print('For more information, see http://www.dartlang.org/tools/analyzer.'); |
| 137 } | 149 } |
| 138 | 150 |
| 151 static String _getVersion() { |
| 152 try { |
| 153 Path path = new Path(new Options().script); |
| 154 Path versionPath = path.directoryPath.append('..').append('version'); |
| 155 File versionFile = new File.fromPath(versionPath); |
| 156 |
| 157 return versionFile.readAsStringSync().trim(); |
| 158 } catch (_) { |
| 159 // This happens when the script is not running in the context of an SDK. |
| 160 return "<unknown>"; |
| 161 } |
| 162 } |
| 139 } | 163 } |
| 140 | 164 |
| 141 /** | 165 /** |
| 142 * Commandline argument parser. | 166 * Commandline argument parser. |
| 143 * | 167 * |
| 144 * TODO(pquitslund): when the args package supports ignoring unrecognized | 168 * TODO(pquitslund): when the args package supports ignoring unrecognized |
| 145 * options/flags, this class can be replaced with a simple [ArgParser] instance. | 169 * options/flags, this class can be replaced with a simple [ArgParser] instance. |
| 146 */ | 170 */ |
| 147 class _CommandLineParser { | 171 class _CommandLineParser { |
| 148 | 172 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 | 252 |
| 229 _getNextFlagIndex(args, i) { | 253 _getNextFlagIndex(args, i) { |
| 230 for ( ; i < args.length; ++i) { | 254 for ( ; i < args.length; ++i) { |
| 231 if (args[i].startsWith('--')) { | 255 if (args[i].startsWith('--')) { |
| 232 return i; | 256 return i; |
| 233 } | 257 } |
| 234 } | 258 } |
| 235 return i; | 259 return i; |
| 236 } | 260 } |
| 237 } | 261 } |
| OLD | NEW |