Clean up use of pkg:args, bump args dep (#1784)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8bad983..c51cdf6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,7 +13,8 @@
dot shorthand.
* Require `analyzer: '>=8.2.0 <10.0.0'`.
-* Require `sdk: ^3.9.0`
+* Require `args: ^2.5.0`.
+* Require `sdk: ^3.9.0`.
### Bug fixes
diff --git a/lib/src/cli/format_command.dart b/lib/src/cli/format_command.dart
index 9e92629..5d51151 100644
--- a/lib/src/cli/format_command.dart
+++ b/lib/src/cli/format_command.dart
@@ -178,33 +178,20 @@
Future<int> run() async {
var argResults = this.argResults!;
- if (argResults['version'] as bool) {
+ if (argResults.flag('version')) {
print(dartStyleVersion);
return 0;
}
- var show = const {
- 'all': Show.all,
- 'changed': Show.changed,
- 'none': Show.none,
- }[argResults['show']]!;
+ var show = Show.values.byName(argResults.option('show')!);
- var output = const {
- 'write': Output.write,
- 'show': Output.show,
- 'none': Output.none,
- 'json': Output.json,
- }[argResults['output']]!;
+ var output = Output.values.byName(argResults.option('output')!);
- var summary = Summary.none;
- switch (argResults['summary'] as String) {
- case 'line':
- summary = Summary.line();
- break;
- case 'profile':
- summary = Summary.profile();
- break;
- }
+ var summary = switch (argResults.option('summary')) {
+ 'line' => Summary.line(),
+ 'profile' => Summary.profile(),
+ _ => Summary.none,
+ };
// If the user is sending code through stdin, default the output to stdout.
if (!argResults.wasParsed('output') && argResults.rest.isEmpty) {
@@ -224,7 +211,7 @@
}
// Can't use --verbose with anything but --help.
- if (argResults['verbose'] as bool && !(argResults['help'] as bool)) {
+ if (argResults.flag('verbose') && !argResults.flag('help')) {
usageException('Can only use --verbose with --help.');
}
@@ -234,7 +221,7 @@
}
Version? languageVersion;
- if (argResults['language-version'] case String version) {
+ if (argResults.option('language-version') case String version) {
var versionPattern = RegExp(r'^([0-9]+)\.([0-9]+)$');
if (version == 'latest') {
languageVersion = DartFormatter.latestLanguageVersion;
@@ -255,9 +242,9 @@
// Allow the old option name if the new one wasn't passed.
String? pageWidthString;
if (argResults.wasParsed('page-width')) {
- pageWidthString = argResults['page-width'] as String;
+ pageWidthString = argResults.option('page-width')!;
} else if (argResults.wasParsed('line-length')) {
- pageWidthString = argResults['line-length'] as String;
+ pageWidthString = argResults.option('line-length')!;
}
int? pageWidth;
@@ -276,7 +263,7 @@
if (argResults.wasParsed('trailing-commas')) {
// We check the values explicitly here instead of using `allowedValues`
// from [ArgParser] because this provides a better error message.
- trailingCommas = switch (argResults['trailing-commas']) {
+ trailingCommas = switch (argResults.option('trailing-commas')) {
'automate' => TrailingCommas.automate,
'preserve' => TrailingCommas.preserve,
var mode => usageException(
@@ -286,10 +273,10 @@
}
var indent =
- int.tryParse(argResults['indent'] as String) ??
+ int.tryParse(argResults.option('indent')!) ??
usageException(
'--indent must be an integer, was '
- '"${argResults['indent']}".',
+ '"${argResults.option('indent')}".',
);
if (indent < 0) {
@@ -306,10 +293,10 @@
usageException(exception.message);
}
- var followLinks = argResults['follow-links'] as bool;
- var setExitIfChanged = argResults['set-exit-if-changed'] as bool;
+ var followLinks = argResults.flag('follow-links');
+ var setExitIfChanged = argResults.flag('set-exit-if-changed');
- var experimentFlags = argResults['enable-experiment'] as List<String>;
+ var experimentFlags = argResults.multiOption('enable-experiment');
// If stdin isn't connected to a pipe, then the user is not passing
// anything to stdin, so let them know they made a mistake.
@@ -324,7 +311,7 @@
if (argResults.wasParsed('stdin-name') && argResults.rest.isNotEmpty) {
usageException('Cannot pass --stdin-name when not reading from stdin.');
}
- var stdinName = argResults['stdin-name'] as String?;
+ var stdinName = argResults.option('stdin-name');
var options = FormatterOptions(
languageVersion: languageVersion,
@@ -352,7 +339,7 @@
}
List<int>? _parseSelection(ArgResults argResults, String optionName) {
- var option = argResults[optionName] as String?;
+ var option = argResults.option(optionName);
if (option == null) return null;
// Can only preserve a selection when parsing from stdin.
@@ -375,7 +362,7 @@
} on FormatException catch (_) {
throw FormatException(
'--$optionName must be a colon-separated pair of integers, was '
- '"${argResults[optionName]}".',
+ '"$option".',
);
}
}
diff --git a/pubspec.yaml b/pubspec.yaml
index 180c73d..1180ab6 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -10,7 +10,7 @@
dependencies:
analyzer: '>=8.2.0 <10.0.0'
- args: ^2.0.0
+ args: ^2.5.0
collection: ^1.19.0
package_config: ^2.1.0
path: ^1.9.0