Skip to content

Commit daf5c35

Browse files
authored
[flutter_tools] Update argParser usageLineLength when --wrap-column is passed (flutter#191264)
Rebuild and repopulate `ArgParser` in `FlutterCommandRunner` when global options `--wrap` / `--wrap-column` are parsed, ensuring `usageLineLength` reflects the user-specified wrap column. Fixes flutter#36484 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 5e5237a commit daf5c35

2 files changed

Lines changed: 50 additions & 8 deletions

File tree

‎packages/flutter_tools/lib/src/runner/flutter_command_runner.dart‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class FlutterCommandRunner extends CommandRunner<void> {
5858
bool verboseHelp = false,
5959
}) : _analytics = analytics,
6060
_toolContext = toolContext,
61+
_verboseHelp = verboseHelp,
62+
_argParser = ArgParser(
63+
allowTrailingOptions: false,
64+
usageLineLength: toolContext?.outputPreferences.wrapText ?? false
65+
? toolContext?.outputPreferences.wrapColumn
66+
: null,
67+
),
6168
super(
6269
'flutter',
6370
'Manage your Flutter app development.\n'
@@ -70,6 +77,16 @@ class FlutterCommandRunner extends CommandRunner<void> {
7077
' flutter run [options]\n'
7178
' Run your Flutter application on an attached device or in an emulator.',
7279
) {
80+
_populateOptions(verboseHelp: verboseHelp);
81+
}
82+
83+
final bool _verboseHelp;
84+
85+
@override
86+
ArgParser get argParser => _argParser;
87+
ArgParser _argParser;
88+
89+
void _populateOptions({required bool verboseHelp}) {
7390
argParser.addFlag(
7491
FlutterGlobalOptions.kVerboseFlag,
7592
abbr: 'v',
@@ -237,14 +254,17 @@ class FlutterCommandRunner extends CommandRunner<void> {
237254
);
238255
}
239256

240-
@override
241-
ArgParser get argParser => _argParser;
242-
late final _argParser = ArgParser(
243-
allowTrailingOptions: false,
244-
usageLineLength: _toolContext?.outputPreferences.wrapText ?? false
245-
? _toolContext?.outputPreferences.wrapColumn
246-
: null,
247-
);
257+
void _updateArgParser({required int? wrapColumn}) {
258+
_argParser = ArgParser(allowTrailingOptions: false, usageLineLength: wrapColumn);
259+
260+
argParser.addFlag('help', abbr: 'h', negatable: false, help: 'Print this usage information.');
261+
262+
_populateOptions(verboseHelp: _verboseHelp);
263+
264+
for (final MapEntry<String, Command<void>> entry in commands.entries) {
265+
argParser.addCommand(entry.key, entry.value.argParser);
266+
}
267+
}
248268

249269
@override
250270
String get usageFooter {
@@ -424,6 +444,10 @@ class FlutterCommandRunner extends CommandRunner<void> {
424444
wrapColumn: wrapColumn,
425445
);
426446

447+
_updateArgParser(
448+
wrapColumn: useWrapping ? (wrapColumn ?? toolContext.stdio.terminalColumns) : null,
449+
);
450+
427451
if (((topLevelResults[FlutterGlobalOptions.kShowTestDeviceFlag] as bool?) ?? false) ||
428452
topLevelResults[FlutterGlobalOptions.kDeviceIdOption] ==
429453
FlutterTesterDevices.kTesterDeviceId) {

‎packages/flutter_tools/test/general.shard/args_test.dart‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ void main() {
209209
await runner.run(<String>['dummy']);
210210
expect(command.stringsArg('key'), <String>[]);
211211
});
212+
213+
testUsingContext('wrap-column option updates argParser usageLineLength', () async {
214+
final command = DummyFlutterCommand(
215+
commandFunction: () async {
216+
return const FlutterCommandResult(ExitStatus.success);
217+
},
218+
);
219+
final runner = FlutterCommandRunner(
220+
analytics: FakeAnalytics(),
221+
toolContext: FakeToolContext(),
222+
verboseHelp: true,
223+
);
224+
runner.addCommand(command);
225+
226+
await runner.run(<String>['--wrap', '--wrap-column=50', 'dummy']);
227+
228+
expect(runner.argParser.usageLineLength, 50);
229+
});
212230
}
213231

214232
void verifyCommandRunner(CommandRunner<Object?> runner) {

0 commit comments

Comments
 (0)