From cf2fb57cb7ccd3f02025e89f889fba5ee49fc317 Mon Sep 17 00:00:00 2001 From: Christer Date: Fri, 20 Nov 2026 13:54:21 +0100 Subject: [PATCH 1/3] refactor(command): Replaced usageLineLength with usageSettings Replace AbstractCommand.usageLineLength with CommandRunner.usageSettings. --- .../config/lib/src/command/abstract_command.dart | 4 ---- packages/config/lib/src/command/command_runner.dart | 3 +-- packages/config/lib/src/command/usage_generator.dart | 9 ++------- packages/config/lib/src/command/usage_output.dart | 12 ++++++++++++ .../config/test/command/usage_formatting_test.dart | 4 +++- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/config/lib/src/command/abstract_command.dart b/packages/config/lib/src/command/abstract_command.dart index bcb8d8c3..a5d717ed 100644 --- a/packages/config/lib/src/command/abstract_command.dart +++ b/packages/config/lib/src/command/abstract_command.dart @@ -23,9 +23,6 @@ abstract class AbstractCommand /// The usage string generator for this command. final UsageGenerator usageGenerator; - /// If non-null, specifies the line length for usage output wrapping. - int? usageLineLength; - @override final List persistentOptions; @@ -53,7 +50,6 @@ abstract class AbstractCommand /// is defined. The `help` command is always built in, as the last one. AbstractCommand({ required this.usageGenerator, - this.usageLineLength, final List? persistentOptions, final List? options, final List>? subcommands, diff --git a/packages/config/lib/src/command/command_runner.dart b/packages/config/lib/src/command/command_runner.dart index e65c5d0d..496cfa44 100644 --- a/packages/config/lib/src/command/command_runner.dart +++ b/packages/config/lib/src/command/command_runner.dart @@ -188,7 +188,6 @@ class CommandRunner super.subcommands, super.middlewares, super.canRunAsLeafCommand, - super.usageLineLength, final UsageGenerator? usageGenerator, final bool? ignoreUnexpectedPositionalArgs, final ParserSettings? parserSettings, @@ -260,7 +259,7 @@ class CommandRunner /// /// Throws [UsageException] if any configuration input including arguments, /// environment variables, and configuration files is invalid, - /// unless [CommandUsageSettings.noUsageException] is true. + /// unless [UsageExceptionSettings.noUsageException] is true. Future run( final Iterable args, { final Map? env, diff --git a/packages/config/lib/src/command/usage_generator.dart b/packages/config/lib/src/command/usage_generator.dart index 774339b7..e9bb9bbe 100644 --- a/packages/config/lib/src/command/usage_generator.dart +++ b/packages/config/lib/src/command/usage_generator.dart @@ -27,11 +27,6 @@ class StandardCommandUsageGenerator implements UsageGenerator { } } -int? _getUsageLineLength(AbstractCommand command) { - int? lineLength = command.usageLineLength; - while (lineLength == null && command.hasParent) { - command = command.parent; - lineLength = command.usageLineLength; - } - return lineLength; +int? _getUsageLineLength(final AbstractCommand command) { + return command.runner.usageOutput.usageSettings.usageLineLength; } diff --git a/packages/config/lib/src/command/usage_output.dart b/packages/config/lib/src/command/usage_output.dart index b5b4d7a3..47591073 100644 --- a/packages/config/lib/src/command/usage_output.dart +++ b/packages/config/lib/src/command/usage_output.dart @@ -7,6 +7,7 @@ import 'output_sinks.dart'; /// controls the help output and the usage exception output. class UsageOutputHandler { final OutputSinks sinks; + final UsageSettings usageSettings; final UsageExceptionSettings exceptionSettings; final UsageExceptionHandler? _standardExceptionHandler; @@ -24,6 +25,7 @@ class UsageOutputHandler { /// constructed with no handlers. UsageOutputHandler({ this.sinks = const OutputSinks(), + this.usageSettings = const UsageSettings(), this.exceptionSettings = const UsageExceptionSettings(), final Iterable? addExceptionHandlers, }) : _standardExceptionHandler = standardUsageExceptionHandler, @@ -34,6 +36,7 @@ class UsageOutputHandler { /// Replaces the standard exception handlers with the given exception handlers. UsageOutputHandler.replace({ this.sinks = const OutputSinks(), + this.usageSettings = const UsageSettings(), this.exceptionSettings = const UsageExceptionSettings(), required final Iterable exceptionHandlers, }) : _standardExceptionHandler = null, @@ -69,6 +72,15 @@ class UsageOutputHandler { } } +class UsageSettings { + /// If non-null, specifies the line length for usage output wrapping. + final int? usageLineLength; + + const UsageSettings({ + this.usageLineLength, + }); +} + //////////////////////// // UsageException handling and printing //////////////////////// diff --git a/packages/config/test/command/usage_formatting_test.dart b/packages/config/test/command/usage_formatting_test.dart index 6785d3c3..38b36002 100644 --- a/packages/config/test/command/usage_formatting_test.dart +++ b/packages/config/test/command/usage_formatting_test.dart @@ -1483,7 +1483,9 @@ CommandRunner _runner({ description, globalOptions: globalOptions, subcommands: subcommands, - usageLineLength: usageLineLength, + usageOutput: UsageOutputHandler( + usageSettings: UsageSettings(usageLineLength: usageLineLength), + ), rootFooter: rootFooter, // The built-in completion command is covered by the completion tests. completion: CompletionSettings.disabled, From 49fa7f0bc500ee134fa688f475aad7f3bffe5418 Mon Sep 17 00:00:00 2001 From: Christer Date: Sat, 21 Nov 2026 09:30:57 +0100 Subject: [PATCH 2/3] refactor(command): Removed isCompletion convenience getter --- packages/config/example/command_example.dart | 6 +++--- packages/config/lib/src/command/command_context.dart | 3 --- .../test/command/completion/completion_engine_test.dart | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/config/example/command_example.dart b/packages/config/example/command_example.dart index 410a2328..eb598853 100644 --- a/packages/config/example/command_example.dart +++ b/packages/config/example/command_example.dart @@ -205,7 +205,7 @@ class AnalyticsMiddleware extends CommandMiddlewareHooks { CommandContext onRun( final CommandContext context, ) { - if (context.isCompletion) return context; + if (context.runMode == CommandRunMode.completion) return context; makeAnalyticsSender(analyticsEnabled, onAnalyticsEvent)( context.root().next?.command.name ?? context.command.name, ); @@ -234,7 +234,7 @@ class CommandSpanMiddleware @override CommandContext onRun(final CommandContext context) { - if (context.isCompletion) return context; + if (context.runMode == CommandRunMode.completion) return context; context.provider.store(CommandSpan()); print('>>> ${context.leaf().command.name} started'); return context; @@ -242,7 +242,7 @@ class CommandSpanMiddleware @override T? onReturn(final CommandContext context, final T? response) { - if (context.isCompletion) return response; + if (context.runMode == CommandRunMode.completion) return response; final stopwatch = context.provider.fetch().stopwatch..stop(); print( '>>> ${context.leaf().command.name} returned $response ' diff --git a/packages/config/lib/src/command/command_context.dart b/packages/config/lib/src/command/command_context.dart index ff680231..194f80ef 100644 --- a/packages/config/lib/src/command/command_context.dart +++ b/packages/config/lib/src/command/command_context.dart @@ -24,9 +24,6 @@ class CommandContext { /// Whether this context is part of executing or completing the command line. final CommandRunMode runMode; - /// Whether middleware is running for completion rather than execution. - bool get isCompletion => runMode == CommandRunMode.completion; - /// The configuration of the command's own (application-defined) options. final Configuration config; diff --git a/packages/config/test/command/completion/completion_engine_test.dart b/packages/config/test/command/completion/completion_engine_test.dart index 82f27cac..2ec55189 100644 --- a/packages/config/test/command/completion/completion_engine_test.dart +++ b/packages/config/test/command/completion/completion_engine_test.dart @@ -768,7 +768,7 @@ void main() { Middlewares.withHooks( onRun: (final ctx) { trace.add('${ctx.command.name}:onRun:${ctx.runMode.name}'); - if (ctx.isCompletion && + if (ctx.runMode == CommandRunMode.completion && ctx.leaf().config.options.contains(projectOption)) { final project = ctx.leaf().config.optionalValue(projectOption); ctx.provider.store(_ProjectApi(project ?? 'none')); From 883261c1fd2589c7835eee92b227c2932ed7938a Mon Sep 17 00:00:00 2001 From: Christer Date: Sat, 21 Nov 2026 11:15:14 +0100 Subject: [PATCH 3/3] fix(cli_tools): Updated BetterCommandRunner according to changes --- .../better_command_runner.dart | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/cli_tools/lib/src/better_command_runner/better_command_runner.dart b/packages/cli_tools/lib/src/better_command_runner/better_command_runner.dart index cb38875e..0cc584ae 100644 --- a/packages/cli_tools/lib/src/better_command_runner/better_command_runner.dart +++ b/packages/cli_tools/lib/src/better_command_runner/better_command_runner.dart @@ -159,7 +159,6 @@ class BetterCommandRunner super.rootFooter, super.subcommands, final List>? prependMiddlewares, - final int? wrapTextColumn, final SetLogLevel? setLogLevel, super.usageGenerator, super.ignoreUnexpectedPositionalArgs, @@ -168,6 +167,7 @@ class BetterCommandRunner final CompletionSettings? completion, final OnAnalyticsEvent? onAnalyticsEvent, final BuildCommandLinePropertiesForAnalytics? buildAnalyticsProperties, + @Deprecated('Use usageOutput instead') final int? wrapTextColumn, @Deprecated('Use a middleware instead') final OnBeforeRunCommand? onBeforeRunCommand, @Deprecated('Use usageOutput instead') final MessageOutput? messageOutput, @@ -179,8 +179,11 @@ class BetterCommandRunner super( globalOptions: _makeGlobalOptions(globalOptions, onAnalyticsEvent), canRunAsLeafCommand: true, // prints usage when run as leaf command - usageLineLength: wrapTextColumn, - usageOutput: _makeUsageOutputHandler(usageOutput, messageOutput), + usageOutput: _makeUsageOutputHandler( + usageOutput, + messageOutput, + wrapTextColumn, + ), completion: completion ?? CompletionSettings(enabled: enableCompletionCommand ?? true), @@ -251,6 +254,7 @@ class BetterCommandRunner static UsageOutputHandler _makeUsageOutputHandler( final UsageOutputHandler? usageOutput, final MessageOutput? messageOutput, + final int? wrapTextColumn, ) { if (usageOutput != null) { if (messageOutput != null) { @@ -259,11 +263,15 @@ class BetterCommandRunner return usageOutput; } - if (messageOutput == null) return UsageOutputHandler(); - + if (messageOutput == null) { + return UsageOutputHandler( + usageSettings: UsageSettings(usageLineLength: wrapTextColumn), + ); + } // create a UsageOutputHandler with wrappers around the legacy messageOutput return UsageOutputHandler( sinks: OutputSinks(out: BridgeUsagePrinter(messageOutput)), + usageSettings: UsageSettings(usageLineLength: wrapTextColumn), exceptionSettings: UsageExceptionSettings( exceptionPrinter: BridgeUsageExceptionPrinter(messageOutput), ), @@ -324,7 +332,7 @@ class BetterCommandRunner ) async { if (onAnalyticsEvent == null) return false; - if (context.isCompletion) return false; + if (context.runMode == CommandRunMode.completion) return false; const name = BetterCommandRunnerFlags.analytics; if (context.config.findValueOf(argName: name) == false) {