@@ -13,11 +13,9 @@ import 'package:analyzer/src/util/sdk.dart';
1313import 'package:analyzer_cli/src/ansi.dart' as ansi;
1414import 'package:analyzer_cli/src/driver.dart' ;
1515import 'package:args/args.dart' ;
16- import 'package:pub_semver/pub_semver.dart' ;
1716
1817const _analysisOptionsFileOption = 'options' ;
1918const _binaryName = 'dartanalyzer' ;
20- const _defaultLanguageVersionOption = 'default-language-version' ;
2119const _defineVariableOption = 'D' ;
2220const _enableExperimentOption = 'enable-experiment' ;
2321const _enableInitializingFormalAccessFlag = 'initializing-formal-access' ;
@@ -30,10 +28,6 @@ const _sdkPathOption = 'dart-sdk';
3028/// *Visible for testing.*
3129ExitHandler exitHandler = io.exit;
3230
33- T cast <T >(dynamic value) => value as T ;
34-
35- T ? castNullable <T >(dynamic value) => value as T ? ;
36-
3731/// Print the given [message] to stderr and exit with the given [exitCode] .
3832void printAndFail (String message, {int exitCode = 15 }) {
3933 errorSink.writeln (message);
@@ -110,30 +104,28 @@ class CommandLineOptions {
110104 ResourceProvider resourceProvider,
111105 ArgResults args,
112106 ) : _argResults = args,
113- dartSdkPath = castNullable ( args[ _sdkPathOption] ),
114- disableCacheFlushing = cast ( args[ 'disable-cache-flushing' ] ),
115- displayVersion = cast ( args[ 'version' ] ),
116- ignoreUnrecognizedFlags = cast ( args[ _ignoreUnrecognizedFlagsFlag] ),
117- log = cast ( args[ 'log' ] ),
107+ dartSdkPath = args. option ( _sdkPathOption),
108+ disableCacheFlushing = args. flag ( 'disable-cache-flushing' ),
109+ displayVersion = args. flag ( 'version' ),
110+ ignoreUnrecognizedFlags = args. flag ( _ignoreUnrecognizedFlagsFlag),
111+ log = args. flag ( 'log' ),
118112 jsonFormat = args['format' ] == 'json' ,
119113 machineFormat = args['format' ] == 'machine' ,
120- perfReport = castNullable ( args[ 'x-perf-report' ] ),
121- batchMode = cast ( args[ 'batch' ] ),
114+ perfReport = args. option ( 'x-perf-report' ),
115+ batchMode = args. flag ( 'batch' ),
122116 sourceFiles = args.rest,
123- trainSnapshot = cast ( args[ 'train-snapshot' ] ),
124- verbose = cast ( args[ 'verbose' ] ),
125- color = cast ( args[ 'color' ] ) {
117+ trainSnapshot = args. flag ( 'train-snapshot' ),
118+ verbose = args. flag ( 'verbose' ),
119+ color = args. flag ( 'color' ) {
126120 //
127121 // File locations.
128122 //
129123 defaultAnalysisOptionsPath = _absoluteNormalizedPath (
130124 resourceProvider,
131- castNullable (args[_analysisOptionsFileOption]),
132- );
133- defaultPackagesPath = _absoluteNormalizedPath (
134- resourceProvider,
135- castNullable (args[_packagesOption]),
125+ args.option (_analysisOptionsFileOption),
136126 );
127+ defaultPackagesPath =
128+ _absoluteNormalizedPath (resourceProvider, args.option (_packagesOption));
137129
138130 //
139131 // Declared variables.
@@ -158,15 +150,9 @@ class CommandLineOptions {
158150 }
159151 }
160152
161- /// The default language version for files that are not in a package.
162- /// (Or null if no default language version to force.)
163- String ? get defaultLanguageVersion {
164- return castNullable (_argResults[_defaultLanguageVersionOption]);
165- }
166-
167153 /// A list of the names of the experiments that are to be enabled.
168- List <String >? get enabledExperiments {
169- return castNullable ( _argResults[ _enableExperimentOption] );
154+ List <String > get enabledExperiments {
155+ return _argResults. multiOption ( _enableExperimentOption);
170156 }
171157
172158 /// Update the [analysisOptions] with flags that the user specified
@@ -175,16 +161,6 @@ class CommandLineOptions {
175161 /// flags that the user specified as command line options more important,
176162 /// so override the corresponding options.
177163 void updateAnalysisOptions (AnalysisOptionsImpl analysisOptions) {
178- var defaultLanguageVersion = this .defaultLanguageVersion;
179- if (defaultLanguageVersion != null ) {
180- var nonPackageLanguageVersion =
181- Version .parse ('$defaultLanguageVersion .0' );
182- analysisOptions.nonPackageLanguageVersion = nonPackageLanguageVersion;
183- analysisOptions.nonPackageFeatureSet = FeatureSet .latestLanguageVersion ()
184- .restrictToVersion (nonPackageLanguageVersion);
185- }
186-
187- var enabledExperiments = this .enabledExperiments! ;
188164 if (enabledExperiments.isNotEmpty) {
189165 analysisOptions.contextFeatures = FeatureSet .fromEnableFlags2 (
190166 sdkLanguageVersion: ExperimentStatus .currentVersion,
@@ -406,10 +382,6 @@ class CommandLineOptions {
406382 defaultsTo: false ,
407383 negatable: false ,
408384 hide: hide)
409- ..addOption (_defaultLanguageVersionOption,
410- help: 'The default language version when it is not specified via '
411- 'other ways (internal, tests only).' ,
412- hide: false )
413385 ..addFlag ('disable-cache-flushing' , defaultsTo: false , hide: hide)
414386 ..addOption ('x-perf-report' ,
415387 help: 'Writes a performance report to the given file (experimental).' ,
@@ -449,21 +421,21 @@ class CommandLineOptions {
449421 var results = parser.parse (args);
450422
451423 // Help requests.
452- if (cast ( results[ 'help' ] )) {
424+ if (results. flag ( 'help' )) {
453425 _showUsage (parser, fromHelp: true );
454426 exitHandler (0 );
455427 return null ; // Only reachable in testing.
456428 }
457429
458430 // Batch mode and input files.
459- if (cast ( results[ 'batch' ] )) {
431+ if (results. flag ( 'batch' )) {
460432 if (results.rest.isNotEmpty) {
461433 errorSink.writeln ('No source files expected in the batch mode.' );
462434 _showUsage (parser);
463435 exitHandler (15 );
464436 return null ; // Only reachable in testing.
465437 }
466- } else if (cast ( results[ 'version' ] )) {
438+ } else if (results. flag ( 'version' )) {
467439 outSink.writeln ('$_binaryName version ${_getVersion ()}' );
468440 exitHandler (0 );
469441 return null ; // Only reachable in testing.
@@ -476,8 +448,7 @@ class CommandLineOptions {
476448 }
477449
478450 if (results.wasParsed (_enableExperimentOption)) {
479- var names =
480- (results[_enableExperimentOption] as List ).cast <String >().toList ();
451+ var names = results.multiOption (_enableExperimentOption);
481452 var errorFound = false ;
482453 for (var validationResult in validateFlags (names)) {
483454 if (validationResult.isError) {
0 commit comments