Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ class BetterCommandRunner<O extends OptionDefinition, T>
super.rootFooter,
super.subcommands,
final List<CommandMiddleware<O, T>>? prependMiddlewares,
final int? wrapTextColumn,
final SetLogLevel? setLogLevel,
super.usageGenerator,
super.ignoreUnexpectedPositionalArgs,
Expand All @@ -168,6 +167,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>
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,
Expand All @@ -179,8 +179,11 @@ class BetterCommandRunner<O extends OptionDefinition, T>
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),
Expand Down Expand Up @@ -251,6 +254,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>
static UsageOutputHandler _makeUsageOutputHandler(
final UsageOutputHandler? usageOutput,
final MessageOutput? messageOutput,
final int? wrapTextColumn,
) {
if (usageOutput != null) {
if (messageOutput != null) {
Expand All @@ -259,11 +263,15 @@ class BetterCommandRunner<O extends OptionDefinition, T>
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),
),
Expand Down Expand Up @@ -324,7 +332,7 @@ class BetterCommandRunner<O extends OptionDefinition, T>
) 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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/config/example/command_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class AnalyticsMiddleware extends CommandMiddlewareHooks<GlobalOption, int> {
CommandContext<GlobalOption, int> onRun(
final CommandContext<GlobalOption, int> context,
) {
if (context.isCompletion) return context;
if (context.runMode == CommandRunMode.completion) return context;
makeAnalyticsSender(analyticsEnabled, onAnalyticsEvent)(
context.root().next?.command.name ?? context.command.name,
);
Expand Down Expand Up @@ -234,15 +234,15 @@ class CommandSpanMiddleware<O extends OptionDefinition, T>

@override
CommandContext<O, T> onRun(final CommandContext<O, T> 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;
}

@override
T? onReturn(final CommandContext<O, T> context, final T? response) {
if (context.isCompletion) return response;
if (context.runMode == CommandRunMode.completion) return response;
final stopwatch = context.provider.fetch<CommandSpan>().stopwatch..stop();
print(
'>>> ${context.leaf().command.name} returned $response '
Expand Down
4 changes: 0 additions & 4 deletions packages/config/lib/src/command/abstract_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ abstract class AbstractCommand<O extends OptionDefinition, T>
/// 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<O> persistentOptions;

Expand Down Expand Up @@ -53,7 +50,6 @@ abstract class AbstractCommand<O extends OptionDefinition, T>
/// is defined. The `help` command is always built in, as the last one.
AbstractCommand({
required this.usageGenerator,
this.usageLineLength,
final List<O>? persistentOptions,
final List<O>? options,
final List<Command<OptionDefinition, T>>? subcommands,
Expand Down
3 changes: 0 additions & 3 deletions packages/config/lib/src/command/command_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ class CommandContext<O extends OptionDefinition, T> {
/// 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<O> config;

Expand Down
3 changes: 1 addition & 2 deletions packages/config/lib/src/command/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ class CommandRunner<O extends OptionDefinition, T>
super.subcommands,
super.middlewares,
super.canRunAsLeafCommand,
super.usageLineLength,
final UsageGenerator? usageGenerator,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
final bool? ignoreUnexpectedPositionalArgs,
final ParserSettings? parserSettings,
Expand Down Expand Up @@ -260,7 +259,7 @@ class CommandRunner<O extends OptionDefinition, T>
///
/// 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<T?> run(
final Iterable<String> args, {
final Map<String, String>? env,
Expand Down
9 changes: 2 additions & 7 deletions packages/config/lib/src/command/usage_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 12 additions & 0 deletions packages/config/lib/src/command/usage_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<UsageExceptionHandler>? addExceptionHandlers,
}) : _standardExceptionHandler = standardUsageExceptionHandler,
Expand All @@ -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<UsageExceptionHandler> exceptionHandlers,
}) : _standardExceptionHandler = null,
Expand Down Expand Up @@ -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
////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
4 changes: 3 additions & 1 deletion packages/config/test/command/usage_formatting_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,9 @@ CommandRunner<OptionDefinition, void> _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,
Expand Down
Loading