@@ -178,33 +178,20 @@ final class FormatCommand extends Command<int> {
178178 Future <int > run () async {
179179 var argResults = this .argResults! ;
180180
181- if (argResults[ 'version' ] as bool ) {
181+ if (argResults. flag ( 'version' ) ) {
182182 print (dartStyleVersion);
183183 return 0 ;
184184 }
185185
186- var show = const {
187- 'all' : Show .all,
188- 'changed' : Show .changed,
189- 'none' : Show .none,
190- }[argResults['show' ]]! ;
191-
192- var output = const {
193- 'write' : Output .write,
194- 'show' : Output .show,
195- 'none' : Output .none,
196- 'json' : Output .json,
197- }[argResults['output' ]]! ;
198-
199- var summary = Summary .none;
200- switch (argResults['summary' ] as String ) {
201- case 'line' :
202- summary = Summary .line ();
203- break ;
204- case 'profile' :
205- summary = Summary .profile ();
206- break ;
207- }
186+ var show = Show .values.byName (argResults.option ('show' )! );
187+
188+ var output = Output .values.byName (argResults.option ('output' )! );
189+
190+ var summary = switch (argResults.option ('summary' )) {
191+ 'line' => Summary .line (),
192+ 'profile' => Summary .profile (),
193+ _ => Summary .none,
194+ };
208195
209196 // If the user is sending code through stdin, default the output to stdout.
210197 if (! argResults.wasParsed ('output' ) && argResults.rest.isEmpty) {
@@ -224,7 +211,7 @@ final class FormatCommand extends Command<int> {
224211 }
225212
226213 // Can't use --verbose with anything but --help.
227- if (argResults[ 'verbose' ] as bool && ! ( argResults[ 'help' ] as bool )) {
214+ if (argResults. flag ( 'verbose' ) && ! argResults. flag ( 'help' )) {
228215 usageException ('Can only use --verbose with --help.' );
229216 }
230217
@@ -234,7 +221,7 @@ final class FormatCommand extends Command<int> {
234221 }
235222
236223 Version ? languageVersion;
237- if (argResults[ 'language-version' ] case String version) {
224+ if (argResults. option ( 'language-version' ) case String version) {
238225 var versionPattern = RegExp (r'^([0-9]+)\.([0-9]+)$' );
239226 if (version == 'latest' ) {
240227 languageVersion = DartFormatter .latestLanguageVersion;
@@ -255,9 +242,9 @@ final class FormatCommand extends Command<int> {
255242 // Allow the old option name if the new one wasn't passed.
256243 String ? pageWidthString;
257244 if (argResults.wasParsed ('page-width' )) {
258- pageWidthString = argResults[ 'page-width' ] as String ;
245+ pageWidthString = argResults. option ( 'page-width' ) ! ;
259246 } else if (argResults.wasParsed ('line-length' )) {
260- pageWidthString = argResults[ 'line-length' ] as String ;
247+ pageWidthString = argResults. option ( 'line-length' ) ! ;
261248 }
262249
263250 int ? pageWidth;
@@ -276,7 +263,7 @@ final class FormatCommand extends Command<int> {
276263 if (argResults.wasParsed ('trailing-commas' )) {
277264 // We check the values explicitly here instead of using `allowedValues`
278265 // from [ArgParser] because this provides a better error message.
279- trailingCommas = switch (argResults[ 'trailing-commas' ] ) {
266+ trailingCommas = switch (argResults. option ( 'trailing-commas' ) ) {
280267 'automate' => TrailingCommas .automate,
281268 'preserve' => TrailingCommas .preserve,
282269 var mode => usageException (
@@ -286,10 +273,10 @@ final class FormatCommand extends Command<int> {
286273 }
287274
288275 var indent =
289- int .tryParse (argResults[ 'indent' ] as String ) ??
276+ int .tryParse (argResults. option ( 'indent' ) ! ) ??
290277 usageException (
291278 '--indent must be an integer, was '
292- '"${argResults [ 'indent' ] }".' ,
279+ '"${argResults . option ( 'indent' ) }".' ,
293280 );
294281
295282 if (indent < 0 ) {
@@ -306,10 +293,10 @@ final class FormatCommand extends Command<int> {
306293 usageException (exception.message);
307294 }
308295
309- var followLinks = argResults[ 'follow-links' ] as bool ;
310- var setExitIfChanged = argResults[ 'set-exit-if-changed' ] as bool ;
296+ var followLinks = argResults. flag ( 'follow-links' ) ;
297+ var setExitIfChanged = argResults. flag ( 'set-exit-if-changed' ) ;
311298
312- var experimentFlags = argResults[ 'enable-experiment' ] as List < String > ;
299+ var experimentFlags = argResults. multiOption ( 'enable-experiment' ) ;
313300
314301 // If stdin isn't connected to a pipe, then the user is not passing
315302 // anything to stdin, so let them know they made a mistake.
@@ -324,7 +311,7 @@ final class FormatCommand extends Command<int> {
324311 if (argResults.wasParsed ('stdin-name' ) && argResults.rest.isNotEmpty) {
325312 usageException ('Cannot pass --stdin-name when not reading from stdin.' );
326313 }
327- var stdinName = argResults[ 'stdin-name' ] as String ? ;
314+ var stdinName = argResults. option ( 'stdin-name' ) ;
328315
329316 var options = FormatterOptions (
330317 languageVersion: languageVersion,
@@ -352,7 +339,7 @@ final class FormatCommand extends Command<int> {
352339 }
353340
354341 List <int >? _parseSelection (ArgResults argResults, String optionName) {
355- var option = argResults[ optionName] as String ? ;
342+ var option = argResults. option ( optionName) ;
356343 if (option == null ) return null ;
357344
358345 // Can only preserve a selection when parsing from stdin.
@@ -375,7 +362,7 @@ final class FormatCommand extends Command<int> {
375362 } on FormatException catch (_) {
376363 throw FormatException (
377364 '--$optionName must be a colon-separated pair of integers, was '
378- '"${ argResults [ optionName ]} ".' ,
365+ '"$option ".' ,
379366 );
380367 }
381368 }
0 commit comments