OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 analyzer_cli.src.options; | 5 library analyzer_cli.src.options; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'package:analyzer/file_system/physical_file_system.dart'; | 9 import 'package:analyzer/file_system/physical_file_system.dart'; |
10 import 'package:analyzer/src/command_line/arguments.dart'; | 10 import 'package:analyzer/src/command_line/arguments.dart'; |
| 11 import 'package:analyzer/src/command_line/command_line_parser.dart'; |
11 import 'package:analyzer_cli/src/driver.dart'; | 12 import 'package:analyzer_cli/src/driver.dart'; |
12 import 'package:args/args.dart'; | 13 import 'package:args/args.dart'; |
13 import 'package:cli_util/cli_util.dart' show getSdkDir; | 14 import 'package:cli_util/cli_util.dart' show getSdkDir; |
14 | 15 |
15 const _binaryName = 'dartanalyzer'; | 16 const _binaryName = 'dartanalyzer'; |
16 | 17 |
17 /// Shared exit handler. | 18 /// Shared exit handler. |
18 /// | 19 /// |
19 /// *Visible for testing.* | 20 /// *Visible for testing.* |
20 ExitHandler exitHandler = exit; | 21 ExitHandler exitHandler = exit; |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 help: 'Print the analyzer version.', | 311 help: 'Print the analyzer version.', |
311 defaultsTo: false, | 312 defaultsTo: false, |
312 negatable: false) | 313 negatable: false) |
313 ..addFlag('lints', | 314 ..addFlag('lints', |
314 help: 'Show lint results.', defaultsTo: false, negatable: false) | 315 help: 'Show lint results.', defaultsTo: false, negatable: false) |
315 ..addFlag('no-hints', | 316 ..addFlag('no-hints', |
316 help: 'Do not show hint results.', | 317 help: 'Do not show hint results.', |
317 defaultsTo: false, | 318 defaultsTo: false, |
318 negatable: false) | 319 negatable: false) |
319 ..addFlag('disable-cache-flushing', defaultsTo: false, hide: true) | 320 ..addFlag('disable-cache-flushing', defaultsTo: false, hide: true) |
320 ..addFlag('ignore-unrecognized-flags', | |
321 help: 'Ignore unrecognized command line flags.', | |
322 defaultsTo: false, | |
323 negatable: false) | |
324 ..addFlag('fatal-hints', | 321 ..addFlag('fatal-hints', |
325 help: 'Treat hints as fatal.', defaultsTo: false, negatable: false) | 322 help: 'Treat hints as fatal.', defaultsTo: false, negatable: false) |
326 ..addFlag('fatal-warnings', | 323 ..addFlag('fatal-warnings', |
327 help: 'Treat non-type warnings as fatal.', | 324 help: 'Treat non-type warnings as fatal.', |
328 defaultsTo: false, | 325 defaultsTo: false, |
329 negatable: false) | 326 negatable: false) |
330 ..addFlag('fatal-lints', | 327 ..addFlag('fatal-lints', |
331 help: 'Treat lints as fatal.', defaultsTo: false, negatable: false) | 328 help: 'Treat lints as fatal.', defaultsTo: false, negatable: false) |
332 ..addFlag('package-warnings', | 329 ..addFlag('package-warnings', |
333 help: 'Show warnings from package: imports.', | 330 help: 'Show warnings from package: imports.', |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 | 556 |
560 static _showUsage(parser) { | 557 static _showUsage(parser) { |
561 errorSink | 558 errorSink |
562 .writeln('Usage: $_binaryName [options...] <libraries to analyze...>'); | 559 .writeln('Usage: $_binaryName [options...] <libraries to analyze...>'); |
563 errorSink.writeln(parser.getUsage()); | 560 errorSink.writeln(parser.getUsage()); |
564 errorSink.writeln(''); | 561 errorSink.writeln(''); |
565 errorSink.writeln( | 562 errorSink.writeln( |
566 'For more information, see http://www.dartlang.org/tools/analyzer.'); | 563 'For more information, see http://www.dartlang.org/tools/analyzer.'); |
567 } | 564 } |
568 } | 565 } |
569 | |
570 /// Commandline argument parser. | |
571 /// | |
572 /// TODO(pq): when the args package supports ignoring unrecognized | |
573 /// options/flags, this class can be replaced with a simple [ArgParser] | |
574 /// instance. | |
575 class CommandLineParser { | |
576 final List<String> _knownFlags; | |
577 final bool _alwaysIgnoreUnrecognized; | |
578 final ArgParser _parser; | |
579 | |
580 /// Creates a new command line parser. | |
581 CommandLineParser({bool alwaysIgnoreUnrecognized: false}) | |
582 : _knownFlags = <String>[], | |
583 _alwaysIgnoreUnrecognized = alwaysIgnoreUnrecognized, | |
584 _parser = new ArgParser(allowTrailingOptions: true); | |
585 | |
586 ArgParser get parser => _parser; | |
587 | |
588 /// Defines a flag. | |
589 /// See [ArgParser.addFlag()]. | |
590 void addFlag(String name, | |
591 {String abbr, | |
592 String help, | |
593 bool defaultsTo: false, | |
594 bool negatable: true, | |
595 void callback(bool value), | |
596 bool hide: false}) { | |
597 _knownFlags.add(name); | |
598 _parser.addFlag(name, | |
599 abbr: abbr, | |
600 help: help, | |
601 defaultsTo: defaultsTo, | |
602 negatable: negatable, | |
603 callback: callback, | |
604 hide: hide); | |
605 } | |
606 | |
607 /// Defines a value-taking option. | |
608 /// See [ArgParser.addOption()]. | |
609 void addOption(String name, | |
610 {String abbr, | |
611 String help, | |
612 List<String> allowed, | |
613 Map<String, String> allowedHelp, | |
614 String defaultsTo, | |
615 void callback(value), | |
616 bool allowMultiple: false, | |
617 bool splitCommas, | |
618 bool hide: false}) { | |
619 _knownFlags.add(name); | |
620 _parser.addOption(name, | |
621 abbr: abbr, | |
622 help: help, | |
623 allowed: allowed, | |
624 allowedHelp: allowedHelp, | |
625 defaultsTo: defaultsTo, | |
626 callback: callback, | |
627 allowMultiple: allowMultiple, | |
628 splitCommas: splitCommas, | |
629 hide: hide); | |
630 } | |
631 | |
632 /// Generates a string displaying usage information for the defined options. | |
633 /// See [ArgParser.usage]. | |
634 String getUsage() => _parser.usage; | |
635 | |
636 /// Parses [args], a list of command-line arguments, matches them against the | |
637 /// flags and options defined by this parser, and returns the result. | |
638 /// See [ArgParser]. | |
639 ArgResults parse(List<String> args) => _parser.parse(_filterUnknowns(args)); | |
640 | |
641 List<String> _filterUnknowns(List<String> args) { | |
642 // Only filter args if the ignore flag is specified, or if | |
643 // _alwaysIgnoreUnrecognized was set to true. | |
644 if (_alwaysIgnoreUnrecognized || | |
645 args.contains('--ignore-unrecognized-flags')) { | |
646 //TODO(pquitslund): replace w/ the following once library skew issues are | |
647 // sorted out | |
648 //return args.where((arg) => !arg.startsWith('--') || | |
649 // _knownFlags.contains(arg.substring(2))); | |
650 | |
651 // Filter all unrecognized flags and options. | |
652 List<String> filtered = <String>[]; | |
653 for (int i = 0; i < args.length; ++i) { | |
654 String arg = args[i]; | |
655 if (arg.startsWith('--') && arg.length > 2) { | |
656 String option = arg.substring(2); | |
657 // strip the last '=value' | |
658 int equalsOffset = option.lastIndexOf('='); | |
659 if (equalsOffset != -1) { | |
660 option = option.substring(0, equalsOffset); | |
661 } | |
662 // Check the option | |
663 if (!_knownFlags.contains(option)) { | |
664 //"eat" params by advancing to the next flag/option | |
665 i = _getNextFlagIndex(args, i); | |
666 } else { | |
667 filtered.add(arg); | |
668 } | |
669 } else { | |
670 filtered.add(arg); | |
671 } | |
672 } | |
673 | |
674 return filtered; | |
675 } else { | |
676 return args; | |
677 } | |
678 } | |
679 | |
680 int _getNextFlagIndex(args, i) { | |
681 for (; i < args.length; ++i) { | |
682 if (args[i].startsWith('--')) { | |
683 return i; | |
684 } | |
685 } | |
686 return i; | |
687 } | |
688 } | |
OLD | NEW |