mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
[flutter_tools] refactor stringsArg (#105032)
This commit is contained in:
parent
fe7b6de4df
commit
b02f68a66c
@ -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() {
|
|||||||
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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user