Skip to content

Commit 3dee87d

Browse files
committed
Clean up use of pkg:args, bump args dep
1 parent ade6076 commit 3dee87d

File tree

3 files changed

+26
-38
lines changed

3 files changed

+26
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
dot shorthand.
1414

1515
* Require `analyzer: '>=8.2.0 <10.0.0'`.
16-
* Require `sdk: ^3.9.0`
16+
* Require `args: ^2.5.0`.
17+
* Require `sdk: ^3.9.0`.
1718

1819
### Bug fixes
1920

lib/src/cli/format_command.dart

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ environment:
1010

1111
dependencies:
1212
analyzer: '>=8.2.0 <10.0.0'
13-
args: ^2.0.0
13+
args: ^2.5.0
1414
collection: ^1.19.0
1515
package_config: ^2.1.0
1616
path: ^1.9.0

0 commit comments

Comments
 (0)