Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: pkg/analyzer/lib/options.dart

Issue 290733010: Parse defined variables in command-line analyzer (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analyzer/test/options_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 'dart:io'; 7 import 'dart:io';
8 8
9 import 'package:args/args.dart'; 9 import 'package:args/args.dart';
10 10
11 const _BINARY_NAME = 'dartanalyzer'; 11 const _BINARY_NAME = 'dartanalyzer';
12 12
13 /** 13 /**
14 * Analyzer commandline configuration options. 14 * Analyzer commandline configuration options.
15 */ 15 */
16 class CommandLineOptions { 16 class CommandLineOptions {
17 17
18 /** Batch mode (for unit testing) */ 18 /** Batch mode (for unit testing) */
19 final bool shouldBatch; 19 final bool shouldBatch;
20 20
21 /** Whether to use machine format for error display */ 21 /** Whether to use machine format for error display */
22 final bool machineFormat; 22 final bool machineFormat;
23 23
24 /** Whether to display version information */ 24 /** Whether to display version information */
25 final bool displayVersion; 25 final bool displayVersion;
26 26
27 /** A table mapping the names of defined variables to their values. */
28 final Map<String, String> definedVariables;
29
27 /** Whether to report hints */ 30 /** Whether to report hints */
28 final bool disableHints; 31 final bool disableHints;
29 32
30 /** Whether to ignore unrecognized flags */ 33 /** Whether to ignore unrecognized flags */
31 final bool ignoreUnrecognizedFlags; 34 final bool ignoreUnrecognizedFlags;
32 35
33 /** Whether to show performance statistics */ 36 /** Whether to show performance statistics */
34 final bool perf; 37 final bool perf;
35 38
36 /** Whether to show package: warnings */ 39 /** Whether to show package: warnings */
(...skipping 16 matching lines...) Expand all
53 56
54 /** The source files to analyze */ 57 /** The source files to analyze */
55 final List<String> sourceFiles; 58 final List<String> sourceFiles;
56 59
57 /** Whether to log additional analysis messages and exceptions */ 60 /** Whether to log additional analysis messages and exceptions */
58 final bool log; 61 final bool log;
59 62
60 /** 63 /**
61 * Initialize options from the given parsed [args]. 64 * Initialize options from the given parsed [args].
62 */ 65 */
63 CommandLineOptions._fromArgs(ArgResults args) 66 CommandLineOptions._fromArgs(ArgResults args, Map<String, String> definedVaria bles)
64 : shouldBatch = args['batch'], 67 : shouldBatch = args['batch'],
65 machineFormat = args['machine'] || args['format'] == 'machine', 68 machineFormat = args['machine'] || args['format'] == 'machine',
66 displayVersion = args['version'], 69 displayVersion = args['version'],
67 disableHints = args['no-hints'], 70 disableHints = args['no-hints'],
68 ignoreUnrecognizedFlags = args['ignore-unrecognized-flags'], 71 ignoreUnrecognizedFlags = args['ignore-unrecognized-flags'],
69 perf = args['perf'], 72 perf = args['perf'],
70 showPackageWarnings = args['show-package-warnings'] || args['package-warni ngs'], 73 showPackageWarnings = args['show-package-warnings'] || args['package-warni ngs'],
71 showSdkWarnings = args['show-sdk-warnings'] || args['warnings'], 74 showSdkWarnings = args['show-sdk-warnings'] || args['warnings'],
72 warmPerf = args['warm-perf'], 75 warmPerf = args['warm-perf'],
73 warningsAreFatal = args['fatal-warnings'], 76 warningsAreFatal = args['fatal-warnings'],
74 dartSdkPath = args['dart-sdk'], 77 dartSdkPath = args['dart-sdk'],
75 packageRootPath = args['package-root'], 78 packageRootPath = args['package-root'],
76 log = args['log'], 79 log = args['log'],
77 sourceFiles = args.rest; 80 sourceFiles = args.rest,
81 this.definedVariables = definedVariables;
78 82
79 /** 83 /**
80 * Parse [args] into [CommandLineOptions] describing the specified 84 * Parse [args] into [CommandLineOptions] describing the specified
81 * analyzer options. In case of a format error, prints error and exists. 85 * analyzer options. In case of a format error, prints error and exists.
82 */ 86 */
83 static CommandLineOptions parse(List<String> args) { 87 static CommandLineOptions parse(List<String> args) {
84 CommandLineOptions options = _parse(args); 88 CommandLineOptions options = _parse(args);
85 // check SDK 89 // check SDK
86 { 90 {
87 var sdkPath = options.dartSdkPath; 91 var sdkPath = options.dartSdkPath;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 ..addFlag('show-sdk-warnings', help: 'Show warnings from SDK imports (depr ecated)', 143 ..addFlag('show-sdk-warnings', help: 'Show warnings from SDK imports (depr ecated)',
140 defaultsTo: false, negatable: false) 144 defaultsTo: false, negatable: false)
141 ..addFlag('help', abbr: 'h', help: 'Display this help message', 145 ..addFlag('help', abbr: 'h', help: 'Display this help message',
142 defaultsTo: false, negatable: false) 146 defaultsTo: false, negatable: false)
143 ..addFlag('log', help: 'Log additional messages and exceptions', 147 ..addFlag('log', help: 'Log additional messages and exceptions',
144 defaultsTo: false, negatable: false, hide: true); 148 defaultsTo: false, negatable: false, hide: true);
145 149
146 try { 150 try {
147 // TODO(scheglov) https://code.google.com/p/dart/issues/detail?id=11061 151 // TODO(scheglov) https://code.google.com/p/dart/issues/detail?id=11061
148 args = args.map((String arg) => arg == '-batch' ? '--batch' : arg).toList( ); 152 args = args.map((String arg) => arg == '-batch' ? '--batch' : arg).toList( );
149 var results = parser.parse(args); 153 Map<String, String> definedVariables = <String, String>{};
pquitslund 2014/05/22 16:22:40 I think we want to lose the type annotation for co
154 var results = parser.parse(args, definedVariables);
150 // help requests 155 // help requests
151 if (results['help']) { 156 if (results['help']) {
152 _showUsage(parser); 157 _showUsage(parser);
153 exit(0); 158 exit(0);
154 } 159 }
155 // batch mode and input files 160 // batch mode and input files
156 if (results['batch']) { 161 if (results['batch']) {
157 if (results.rest.isNotEmpty) { 162 if (results.rest.isNotEmpty) {
158 print('No source files expected in the batch mode.'); 163 print('No source files expected in the batch mode.');
159 _showUsage(parser); 164 _showUsage(parser);
160 exit(15); 165 exit(15);
161 } 166 }
162 } else if (results['version']) { 167 } else if (results['version']) {
163 print('$_BINARY_NAME version ${_getVersion()}'); 168 print('$_BINARY_NAME version ${_getVersion()}');
164 exit(0); 169 exit(0);
165 } else { 170 } else {
166 if (results.rest.isEmpty) { 171 if (results.rest.isEmpty) {
167 _showUsage(parser); 172 _showUsage(parser);
168 exit(15); 173 exit(15);
169 } 174 }
170 } 175 }
171 return new CommandLineOptions._fromArgs(results); 176 return new CommandLineOptions._fromArgs(results, definedVariables);
172 } on FormatException catch (e) { 177 } on FormatException catch (e) {
173 print(e.message); 178 print(e.message);
174 _showUsage(parser); 179 _showUsage(parser);
175 exit(15); 180 exit(15);
176 } 181 }
177 182
178 } 183 }
179 184
180 static _showUsage(parser) { 185 static _showUsage(parser) {
181 print('Usage: $_BINARY_NAME [options...] <libraries to analyze...>'); 186 print('Usage: $_BINARY_NAME [options...] <libraries to analyze...>');
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 247
243 /** 248 /**
244 * Generates a string displaying usage information for the defined options. 249 * Generates a string displaying usage information for the defined options.
245 * 250 *
246 * See [ArgParser.getUsage()]. 251 * See [ArgParser.getUsage()].
247 */ 252 */
248 String getUsage() => _parser.getUsage(); 253 String getUsage() => _parser.getUsage();
249 254
250 /** 255 /**
251 * Parses [args], a list of command-line arguments, matches them against the 256 * Parses [args], a list of command-line arguments, matches them against the
252 * flags and options defined by this parser, and returns the result. 257 * flags and options defined by this parser, and returns the result. The
258 * values of any defined variables are captured in the given map.
253 * 259 *
254 * See [ArgParser]. 260 * See [ArgParser].
255 */ 261 */
256 ArgResults parse(List<String> args) => _parser.parse(_filterUnknowns(args)); 262 ArgResults parse(List<String> args, Map<String, String> definedVariables) => _ parser.parse(_filterUnknowns(parseDefinedVariables(args, definedVariables)));
263
264 List<String> parseDefinedVariables(List<String> args, Map<String, String> defi nedVariables) {
pquitslund 2014/05/22 16:22:40 This whole method should probably be cleansed of t
265 int count = args.length;
266 List<String> remainingArgs = <String>[];
267 for (int i = 0; i < count; i++) {
268 String arg = args[i];
269 if (arg == '--') {
270 while (i < count) {
271 remainingArgs.add(args[i++]);
272 }
273 } else if (arg.startsWith("-D")) {
274 definedVariables[arg.substring(2)] = args[++i];
275 } else {
276 remainingArgs.add(arg);
277 }
278 }
279 return remainingArgs;
280 }
257 281
258 List<String> _filterUnknowns(args) { 282 List<String> _filterUnknowns(args) {
259 283
260 // Only filter args if the ignore flag is specified. 284 // Only filter args if the ignore flag is specified.
261 if (!args.contains('--ignore-unrecognized-flags')) { 285 if (!args.contains('--ignore-unrecognized-flags')) {
262 return args; 286 return args;
263 } 287 }
264 288
265 //TODO(pquitslund): replace w/ the following once library skew issues are so rted out 289 //TODO(pquitslund): replace w/ the following once library skew issues are so rted out
266 //return args.where((arg) => !arg.startsWith('--') || 290 //return args.where((arg) => !arg.startsWith('--') ||
(...skipping 21 matching lines...) Expand all
288 312
289 _getNextFlagIndex(args, i) { 313 _getNextFlagIndex(args, i) {
290 for ( ; i < args.length; ++i) { 314 for ( ; i < args.length; ++i) {
291 if (args[i].startsWith('--')) { 315 if (args[i].startsWith('--')) {
292 return i; 316 return i;
293 } 317 }
294 } 318 }
295 return i; 319 return i;
296 } 320 }
297 } 321 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/options_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698