Unverified Commit b02f68a6 authored by Aman Verma's avatar Aman Verma Committed by GitHub

[flutter_tools] refactor stringsArg (#105032)

parent fe7b6de4
...@@ -1556,7 +1556,9 @@ abstract class FlutterCommand extends Command<void> { ...@@ -1556,7 +1556,9 @@ abstract class FlutterCommand extends Command<void> {
int? intArg(String name) => argResults?[name] as int?; int? intArg(String name) => argResults?[name] as int?;
/// Gets the parsed command-line option named [name] as `List<String>`. /// Gets the parsed command-line option named [name] as `List<String>`.
List<String> stringsArg(String name) => argResults?[name] as List<String>? ?? <String>[]; List<String> stringsArg(String name) {
return argResults![name]! as List<String>? ?? <String>[];
}
} }
/// A mixin which applies an implementation of [requiredArtifacts] that only /// A mixin which applies an implementation of [requiredArtifacts] that only
......
...@@ -72,6 +72,36 @@ void main() { ...@@ -72,6 +72,36 @@ void main() {
expect(command.stringArgDeprecated('key'), 'value'); expect(command.stringArgDeprecated('key'), 'value');
expect(() => command.stringArgDeprecated('empty'), throwsA(const TypeMatcher<ArgumentError>())); expect(() => command.stringArgDeprecated('empty'), throwsA(const TypeMatcher<ArgumentError>()));
}); });
testUsingContext('List<String> safe argResults', () async {
final DummyFlutterCommand command = DummyFlutterCommand(
commandFunction: () async {
return const FlutterCommandResult(ExitStatus.success);
}
);
final FlutterCommandRunner runner = FlutterCommandRunner(verboseHelp: true);
command.argParser.addMultiOption(
'key',
allowed: <String>['a', 'b', 'c'],
);
// argResults will be null at this point, if attempt to read them is made,
// exception `Null check operator used on a null value` would be thrown.
expect(() => command.stringsArg('key'), throwsA(const TypeMatcher<TypeError>()));
runner.addCommand(command);
await runner.run(<String>['dummy', '--key', 'a']);
// throws error when trying to parse non-existent key.
expect(() => command.stringsArg('empty'),throwsA(const TypeMatcher<ArgumentError>()));
expect(command.stringsArg('key'), <String>['a']);
await runner.run(<String>['dummy', '--key', 'a', '--key', 'b']);
expect(command.stringsArg('key'), <String>['a', 'b']);
await runner.run(<String>['dummy']);
expect(command.stringsArg('key'), <String>[]);
});
} }
void verifyCommandRunner(CommandRunner<Object> runner) { void verifyCommandRunner(CommandRunner<Object> runner) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment