Skip to content

Commit 4619658

Browse files
authored
refactor(flutter_tools): unify OptionDescriptor identity and addTo (flutter#196076)
Aligns `OptionDescriptor.addTo` (`{bool? hide, String? help}`) with upcoming `package:args/typed.dart` (dart-lang/core#1012) and removes the three non-`const` `OptionDescriptor` factories in `DebuggingOptionDescriptors` so every option lookup queries the exact descriptor instance registered on `ArgParser`: - **`DebuggingOptionDescriptors.devToolsServerAddress`**: Consolidates [`devToolsServerAddress(...)` and `devToolsServerAddressOption`](https://github.com/flutter/flutter/blob/1eecf014c5c47aada3a859fd05fa180a6ccee7dd/packages/flutter_tools/lib/src/runner/options/common_options.dart#L924-L945) into a single `const` descriptor, passing `devToolsServerAddressBaseHelp` via `addTo(..., help: ...)` when `--devtools` is omitted so [`FlutterCommand.devToolsServerAddress`](https://github.com/flutter/flutter/blob/1eecf014c5c47aada3a859fd05fa180a6ccee7dd/packages/flutter_tools/lib/src/runner/flutter_command.dart#L423-L431) matches the registered instance. - **`RunCommandBase._startPaused` & `DriveCommand._publishPort`**: Replaces [`startPaused(...)` / `startPausedOption`](https://github.com/flutter/flutter/blob/1eecf014c5c47aada3a859fd05fa180a6ccee7dd/packages/flutter_tools/lib/src/commands/run.dart#L84) ([queried at line 211](https://github.com/flutter/flutter/blob/1eecf014c5c47aada3a859fd05fa180a6ccee7dd/packages/flutter_tools/lib/src/commands/run.dart#L211)) with an instance-scoped `_startPaused` field on `RunCommandBase`, and inlines [`publishPortOption(enabledByDefault: false)`](https://github.com/flutter/flutter/blob/1eecf014c5c47aada3a859fd05fa180a6ccee7dd/packages/flutter_tools/lib/src/runner/options/common_options.dart#L893-L904) as `static const _publishPort` on `DriveCommand`. Part of flutter#194338 ## 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 in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [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/CONTRIBUTING.ai.md [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 [flutter/website]: https://github.com/flutter/website [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 e6b33e6 commit 4619658

10 files changed

Lines changed: 140 additions & 138 deletions

File tree

‎packages/flutter_tools/lib/src/commands/drive.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class DriveCommand extends RunCommandBase {
6161
}) {
6262
requiresPubspecYaml();
6363

64-
argParser.addDescriptors(<OptionDescriptor<Object?>>[
64+
argParser.addDescriptors(const <OptionDescriptor<Object?>>[
6565
CommonOptions.enableExperiment,
6666
// By default, the drive app should not publish the VM service port over mDNS
6767
// to prevent a local network permission dialog on iOS 14+,
@@ -84,8 +84,10 @@ class DriveCommand extends RunCommandBase {
8484
], verboseHelp: verboseHelp);
8585
}
8686

87-
static final FlagOptionDescriptor _publishPort = DebuggingOptionDescriptors.publishPortOption(
88-
enabledByDefault: false,
87+
static const _publishPort = FlagOptionDescriptor(
88+
name: 'publish-port',
89+
verboseOnly: true,
90+
help: DebuggingOptionDescriptors.publishPortHelp,
8991
);
9092

9193
static const _kKeepAppRunning = 'keep-app-running';

‎packages/flutter_tools/lib/src/commands/logs.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LogsCommand extends FlutterCommand {
3030
help: 'Clear log history before reading from logs.',
3131
);
3232
registerOptionBundle(const DeviceDiscoveryOptionsBundle());
33-
DebuggingOptionDescriptors.adbLogFiltering.addTo(argParser, hideOverride: false);
33+
DebuggingOptionDescriptors.adbLogFiltering.addTo(argParser, hide: false);
3434
}
3535

3636
final ProcessSignal _sigint;

‎packages/flutter_tools/lib/src/commands/run.dart‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
5959
EngineRenderingFlagsBundle(includeTestFlag: true),
6060
WebServerOptionsBundle(),
6161
]);
62-
CommonOptions.nativeNullAssertions.addTo(argParser, hideOverride: !verboseHelp);
62+
CommonOptions.nativeNullAssertions.addTo(argParser, hide: !verboseHelp);
6363
argParser.addDescriptors(<OptionDescriptor<Object?>>[
6464
CommonOptions.dartDefines,
6565
CommonOptions.dartDefineFromFile,
@@ -81,7 +81,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
8181
DebuggingOptionDescriptors.vmserviceOutFile,
8282
DebuggingOptionDescriptors.disableServiceAuthCodes,
8383
DebuggingOptionDescriptors.disableServiceOriginCheck,
84-
DebuggingOptionDescriptors.startPaused(defaultsTo: startPausedDefault),
84+
_startPaused,
8585
DebuggingOptionDescriptors.dartFlags,
8686
DebuggingOptionDescriptors.endlessTraceBuffer,
8787
DebuggingOptionDescriptors.traceSystrace,
@@ -101,6 +101,12 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
101101
], verboseHelp: verboseHelp);
102102
}
103103

104+
late final _startPaused = FlagOptionDescriptor(
105+
name: 'start-paused',
106+
defaultsTo: startPausedDefault,
107+
help: 'Start in a paused mode and wait for a debugger to connect.',
108+
);
109+
104110
final BuildSystem buildSystem;
105111
final BuildTargets buildTargets;
106112

@@ -208,7 +214,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
208214
} else {
209215
return DebuggingOptions.enabled(
210216
buildInfo,
211-
startPaused: getValue(DebuggingOptionDescriptors.startPausedOption),
217+
startPaused: getValue(_startPaused),
212218
disableServiceAuthCodes: disableServiceAuthCodes,
213219
disableServiceOriginCheck: disableServiceOriginCheck,
214220
cacheStartupProfile: cacheStartupProfile,

‎packages/flutter_tools/lib/src/commands/widget_preview.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ final class WidgetPreviewStartCommand extends WidgetPreviewSubCommandBase with C
154154
}
155155
_analysisServerFactoryOverride = analysisServerFactoryOverride;
156156
addPubOptions();
157-
argParser.addDescriptors(<OptionDescriptor<Object?>>[
157+
argParser.addDescriptors(const <OptionDescriptor<Object?>>[
158158
CommonOptions.machine,
159159
DebuggingOptionDescriptors.enableDevTools,
160-
DebuggingOptionDescriptors.devToolsServerAddress(),
160+
DebuggingOptionDescriptors.devToolsServerAddress,
161161
], verboseHelp: verbose);
162162
argParser
163163
..addFlag(

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,10 @@ abstract class FlutterCommand extends Command<void> {
341341
void addDdsOptions({required bool verboseHelp}) {
342342
DebuggingOptionDescriptors.dds.addTo(argParser);
343343
DebuggingOptionDescriptors.ddsPort.addTo(argParser, verboseHelp: verboseHelp);
344-
DebuggingOptionDescriptors.devToolsServerAddressOption.addTo(
344+
DebuggingOptionDescriptors.devToolsServerAddress.addTo(
345345
argParser,
346346
verboseHelp: verboseHelp,
347+
help: DebuggingOptionDescriptors.devToolsServerAddressBaseHelp,
347348
);
348349
}
349350

@@ -421,10 +422,8 @@ abstract class FlutterCommand extends Command<void> {
421422
}
422423

423424
Uri? get devToolsServerAddress {
424-
if (wasParsed(DebuggingOptionDescriptors.devToolsServerAddressOption)) {
425-
final Uri? uri = Uri.tryParse(
426-
getValue(DebuggingOptionDescriptors.devToolsServerAddressOption)!,
427-
);
425+
if (wasParsed(DebuggingOptionDescriptors.devToolsServerAddress)) {
426+
final Uri? uri = Uri.tryParse(getValue(DebuggingOptionDescriptors.devToolsServerAddress)!);
428427
if (uri != null && uri.host.isNotEmpty && uri.port != 0) {
429428
return uri;
430429
}

‎packages/flutter_tools/lib/src/runner/options/common_options.dart‎

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -561,24 +561,18 @@ class VmServicePortOptionsBundle extends OptionBundle {
561561

562562
/// A bundle encapsulating DDS and DevTools options (`--dds`, `--dds-port`, `--disable-dds`, `--devtools`, `--devtools-server-address`).
563563
class DdsAndDevToolsOptionsBundle extends OptionBundle {
564-
const DdsAndDevToolsOptionsBundle({
565-
this.includeDevTools = true,
566-
this.includeEnableDevTools = true,
567-
});
564+
const DdsAndDevToolsOptionsBundle({this.includeDevTools = true});
568565

569566
final bool includeDevTools;
570-
final bool includeEnableDevTools;
571567

572568
@override
573569
List<OptionDescriptor<Object?>> get descriptors => <OptionDescriptor<Object?>>[
574570
DebuggingOptionDescriptors.ddsPort,
575571
DebuggingOptionDescriptors.dds,
576572
DebuggingOptionDescriptors.disableDds,
577-
if (includeDevTools) ...<OptionDescriptor<Object?>>[
578-
if (includeEnableDevTools) DebuggingOptionDescriptors.enableDevTools,
579-
DebuggingOptionDescriptors.devToolsServerAddress(
580-
includeEnableDevTools: includeEnableDevTools,
581-
),
573+
if (includeDevTools) ...const <OptionDescriptor<Object?>>[
574+
DebuggingOptionDescriptors.enableDevTools,
575+
DebuggingOptionDescriptors.devToolsServerAddress,
582576
],
583577
];
584578
}
@@ -881,28 +875,17 @@ abstract final class DebuggingOptionDescriptors {
881875
'Specifying port 0 (the default) will find a random free port.',
882876
);
883877

878+
static const publishPortHelp =
879+
'Publish the VM service port over mDNS. Disable to prevent the '
880+
'local network permission app dialog in debug and profile build modes (iOS devices only).';
881+
884882
static const publishPort = FlagOptionDescriptor(
885883
name: 'publish-port',
886884
defaultsTo: true,
887885
verboseOnly: true,
888-
help:
889-
'Publish the VM service port over mDNS. Disable to prevent the '
890-
'local network permission app dialog in debug and profile build modes (iOS devices only).',
886+
help: publishPortHelp,
891887
);
892888

893-
static FlagOptionDescriptor publishPortOption({bool enabledByDefault = true}) {
894-
if (enabledByDefault) {
895-
return publishPort;
896-
}
897-
return const FlagOptionDescriptor(
898-
name: 'publish-port',
899-
verboseOnly: true,
900-
help:
901-
'Publish the VM service port over mDNS. Disable to prevent the '
902-
'local network permission app dialog in debug and profile build modes (iOS devices only).',
903-
);
904-
}
905-
906889
static const disableDds = FlagOptionDescriptor(
907890
name: 'disable-dds',
908891
verboseOnly: true,
@@ -921,27 +904,17 @@ abstract final class DebuggingOptionDescriptors {
921904
'If "--no-${FlutterCommand.kEnableDevTools}" is specified, "--${FlutterCommand.kDevToolsServerAddress}" is ignored.',
922905
);
923906

924-
static StringOptionDescriptor devToolsServerAddress({bool includeEnableDevTools = true}) {
925-
final ignoredMessage = includeEnableDevTools
926-
? ' Ignored if "--no-${FlutterCommand.kEnableDevTools}" is specified.'
927-
: '';
928-
return StringOptionDescriptor(
929-
name: FlutterCommand.kDevToolsServerAddress,
930-
verboseOnly: true,
931-
help:
932-
'When this value is provided, the Flutter tool will not spin up a '
933-
'new DevTools server instance, and will instead use the one provided '
934-
'at the given address.$ignoredMessage',
935-
);
936-
}
907+
static const devToolsServerAddressBaseHelp =
908+
'When this value is provided, the Flutter tool will not spin up a '
909+
'new DevTools server instance, and will instead use the one provided '
910+
'at the given address.';
937911

938-
static const devToolsServerAddressOption = StringOptionDescriptor(
912+
static const devToolsServerAddress = StringOptionDescriptor(
939913
name: FlutterCommand.kDevToolsServerAddress,
940914
verboseOnly: true,
941915
help:
942-
'When this value is provided, the Flutter tool will not spin up a '
943-
'new DevTools server instance, and will instead use the one provided '
944-
'at the given address.',
916+
'$devToolsServerAddressBaseHelp '
917+
'Ignored if "--no-${FlutterCommand.kEnableDevTools}" is specified.',
945918
);
946919

947920
static const ipv6 = FlagOptionDescriptor(
@@ -1018,17 +991,6 @@ abstract final class DebuggingOptionDescriptors {
1018991
'(Not recommended. This can open your device to remote code execution attacks.)',
1019992
);
1020993

1021-
static FlagOptionDescriptor startPaused({bool defaultsTo = false}) => FlagOptionDescriptor(
1022-
name: 'start-paused',
1023-
defaultsTo: defaultsTo,
1024-
help: 'Start in a paused mode and wait for a debugger to connect.',
1025-
);
1026-
1027-
static const startPausedOption = FlagOptionDescriptor(
1028-
name: 'start-paused',
1029-
help: 'Start in a paused mode and wait for a debugger to connect.',
1030-
);
1031-
1032994
static const dartFlags = StringOptionDescriptor(
1033995
name: 'dart-flags',
1034996
verboseOnly: true,

‎packages/flutter_tools/lib/src/runner/options/option_bundle.dart‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@ abstract class OptionBundle {
2626

2727
/// Registers all option descriptors and child bundles into [parser] and binds
2828
/// to [command].
29-
void register(FlutterCommand command, ArgParser parser, {bool? verboseHelp, bool? hideOverride}) {
29+
void register(FlutterCommand command, ArgParser parser, {bool? verboseHelp, bool? hide}) {
3030
if (title != null) {
3131
parser.addSeparator(title!);
3232
}
3333
onRegister(command);
34-
parser.addDescriptors(
35-
descriptors,
36-
verboseHelp: verboseHelp ?? command.verboseHelp,
37-
hideOverride: hideOverride,
38-
);
34+
parser.addDescriptors(descriptors, verboseHelp: verboseHelp ?? command.verboseHelp, hide: hide);
3935
for (final OptionBundle subBundle in subBundles) {
40-
subBundle.register(command, parser, verboseHelp: verboseHelp, hideOverride: hideOverride);
36+
subBundle.register(command, parser, verboseHelp: verboseHelp, hide: hide);
4137
}
4238
}
4339
}

0 commit comments

Comments
 (0)