| 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 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 if (!(new Directory(sdkPath)).existsSync()) { | 79 if (!(new Directory(sdkPath)).existsSync()) { |
| 80 print('Usage: $_BINARY_NAME: invalid Dart SDK path: $sdkPath'); | 80 print('Usage: $_BINARY_NAME: invalid Dart SDK path: $sdkPath'); |
| 81 exit(15); | 81 exit(15); |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 // OK | 84 // OK |
| 85 return options; | 85 return options; |
| 86 } | 86 } |
| 87 | 87 |
| 88 static CommandLineOptions _parse(List<String> args) { | 88 static CommandLineOptions _parse(List<String> args) { |
| 89 args = args.expand((String arg) => arg.split('=')).toList(); |
| 89 var parser = new _CommandLineParser() | 90 var parser = new _CommandLineParser() |
| 90 ..addFlag('batch', abbr: 'b', help: 'Run in batch mode', | 91 ..addFlag('batch', abbr: 'b', help: 'Run in batch mode', |
| 91 defaultsTo: false, negatable: false) | 92 defaultsTo: false, negatable: false) |
| 92 ..addOption('dart-sdk', help: 'The path to the Dart SDK') | 93 ..addOption('dart-sdk', help: 'The path to the Dart SDK') |
| 93 ..addOption('package-root', help: 'The path to the package root') | 94 ..addOption('package-root', help: 'The path to the package root') |
| 94 ..addFlag('machine', | 95 ..addFlag('machine', |
| 95 help: 'Print errors in a format suitable for parsing', | 96 help: 'Print errors in a format suitable for parsing', |
| 96 defaultsTo: false, negatable: false) | 97 defaultsTo: false, negatable: false) |
| 97 ..addFlag('version', help: 'Print the analyzer version', | 98 ..addFlag('version', help: 'Print the analyzer version', |
| 98 defaultsTo: false, negatable: false) | 99 defaultsTo: false, negatable: false) |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 } | 194 } |
| 194 | 195 |
| 195 /** | 196 /** |
| 196 * Defines a value-taking option. | 197 * Defines a value-taking option. |
| 197 * | 198 * |
| 198 * See [ArgParser.addOption()]. | 199 * See [ArgParser.addOption()]. |
| 199 */ | 200 */ |
| 200 void addOption(String name, {String abbr, String help, List<String> allowed, | 201 void addOption(String name, {String abbr, String help, List<String> allowed, |
| 201 Map<String, String> allowedHelp, String defaultsTo, | 202 Map<String, String> allowedHelp, String defaultsTo, |
| 202 void callback(value), bool allowMultiple: false}) { | 203 void callback(value), bool allowMultiple: false}) { |
| 204 _knownFlags.add(name); |
| 203 _parser.addOption(name, abbr: abbr, help: help, allowed: allowed, | 205 _parser.addOption(name, abbr: abbr, help: help, allowed: allowed, |
| 204 allowedHelp: allowedHelp, defaultsTo: defaultsTo, callback: callback, | 206 allowedHelp: allowedHelp, defaultsTo: defaultsTo, callback: callback, |
| 205 allowMultiple: allowMultiple); | 207 allowMultiple: allowMultiple); |
| 206 } | 208 } |
| 207 | 209 |
| 208 | 210 |
| 209 /** | 211 /** |
| 210 * Generates a string displaying usage information for the defined options. | 212 * Generates a string displaying usage information for the defined options. |
| 211 * | 213 * |
| 212 * See [ArgParser.getUsage()]. | 214 * See [ArgParser.getUsage()]. |
| 213 */ | 215 */ |
| 214 String getUsage() => _parser.getUsage(); | 216 String getUsage() => _parser.getUsage(); |
| 215 | 217 |
| 216 /** | 218 /** |
| 217 * Parses [args], a list of command-line arguments, matches them against the | 219 * Parses [args], a list of command-line arguments, matches them against the |
| 218 * flags and options defined by this parser, and returns the result. | 220 * flags and options defined by this parser, and returns the result. |
| 219 * | 221 * |
| 220 * See [ArgParser]. | 222 * See [ArgParser]. |
| 221 */ | 223 */ |
| 222 ArgResults parse(List<String> args) => _parser.parse(_filterUnknowns(args)); | 224 ArgResults parse(List<String> args) => _parser.parse(_filterUnknowns(args)); |
| 223 | 225 |
| 224 List<String> _filterUnknowns(args) { | 226 List<String> _filterUnknowns(args) { |
| 225 | 227 |
| 226 // Only filter args if the ignore flag is specified. | 228 // Only filter args if the ignore flag is specified. |
| 227 if (!args.contains('--ignore_unrecognized_flags')) { | 229 if (!args.contains('--ignore-unrecognized-flags')) { |
| 228 return args; | 230 return args; |
| 229 } | 231 } |
| 230 | 232 |
| 231 //TODO(pquitslund): replace w/ the following once library skew issues are so
rted out | 233 //TODO(pquitslund): replace w/ the following once library skew issues are so
rted out |
| 232 //return args.where((arg) => !arg.startsWith('--') || | 234 //return args.where((arg) => !arg.startsWith('--') || |
| 233 // _knownFlags.contains(arg.substring(2))); | 235 // _knownFlags.contains(arg.substring(2))); |
| 234 | 236 |
| 235 // Filter all unrecognized flags and options. | 237 // Filter all unrecognized flags and options. |
| 236 var filtered = <String>[]; | 238 var filtered = <String>[]; |
| 237 for (var i=0; i < args.length; ++i) { | 239 for (var i=0; i < args.length; ++i) { |
| 238 var arg = args[i]; | 240 var arg = args[i]; |
| 239 if (arg.startsWith('--') && arg.length > 2) { | 241 if (arg.startsWith('--') && arg.length > 2) { |
| 240 if (!_knownFlags.contains(arg.substring(2))) { | 242 if (!_knownFlags.contains(arg.substring(2))) { |
| 243 print('remove: $arg'); |
| 241 //"eat" params by advancing to the next flag/option | 244 //"eat" params by advancing to the next flag/option |
| 242 i = _getNextFlagIndex(args, i); | 245 i = _getNextFlagIndex(args, i); |
| 243 } else { | 246 } else { |
| 244 filtered.add(arg); | 247 filtered.add(arg); |
| 245 } | 248 } |
| 246 } else { | 249 } else { |
| 247 filtered.add(arg); | 250 filtered.add(arg); |
| 248 } | 251 } |
| 249 } | 252 } |
| 250 | 253 |
| 254 print(filtered); |
| 251 return filtered; | 255 return filtered; |
| 252 } | 256 } |
| 253 | 257 |
| 254 _getNextFlagIndex(args, i) { | 258 _getNextFlagIndex(args, i) { |
| 255 for ( ; i < args.length; ++i) { | 259 for ( ; i < args.length; ++i) { |
| 256 if (args[i].startsWith('--')) { | 260 if (args[i].startsWith('--')) { |
| 257 return i; | 261 return i; |
| 258 } | 262 } |
| 259 } | 263 } |
| 260 return i; | 264 return i; |
| 261 } | 265 } |
| 262 } | 266 } |
| OLD | NEW |