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 0cc584a..071412f 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 @@ -132,26 +132,6 @@ class BetterCommandRunner /// final ConfigOptionBase option; /// } /// ``` - /// - /// ## Arguments after `--` - /// - /// To keep arguments written after `--`, pass [parserSettings] with - /// [DoubleDashHandling.passThroughRest] and set - /// [ignoreUnexpectedPositionalArgs] to true. `runWithConfig` reads them - /// from [Configuration.remainingPosArgs]: - /// - /// ```dart - /// BetterCommandRunner( - /// 'tool', - /// 'Tools', - /// parserSettings: const ParserSettings( - /// doubleDashHandling: DoubleDashHandling.passThroughRest, - /// ), - /// ignoreUnexpectedPositionalArgs: true, - /// ); - /// // tool build -q -- --some --remaining - /// // commandConfig.remainingPosArgs == ['--some', '--remaining'] - /// ``` BetterCommandRunner( super.executableName, super.description, { @@ -161,7 +141,6 @@ class BetterCommandRunner final List>? prependMiddlewares, final SetLogLevel? setLogLevel, super.usageGenerator, - super.ignoreUnexpectedPositionalArgs, super.parserSettings, final UsageOutputHandler? usageOutput, final CompletionSettings? completion, @@ -218,12 +197,13 @@ class BetterCommandRunner ); addMiddleware( - Middlewares.onBeforeRun( - (final ctx) => setLogLevel?.call( + Middlewares.onBeforeRun((final ctx) { + if (ctx.runMode == CommandRunMode.completion) return; + setLogLevel?.call( parsedLogLevel: _determineLogLevel(ctx.config), commandName: ctx.next?.command.name, - ), - ), + ); + }), ); if (onBeforeRunCommand case final OnBeforeRunCommand onBeforeRun) { diff --git a/packages/cli_tools/test/analytics/command_line_properties_test.dart b/packages/cli_tools/test/analytics/command_line_properties_test.dart index 3ed67c0..6dde0f1 100644 --- a/packages/cli_tools/test/analytics/command_line_properties_test.dart +++ b/packages/cli_tools/test/analytics/command_line_properties_test.dart @@ -24,6 +24,7 @@ Command _createLeafCommand() { ..._createOptions, StringOption(argPos: 0), ], + ignoreUnexpectedPositionalArgs: true, run: (final _) {}, ); } @@ -244,7 +245,6 @@ void main() { runner = CommandRunner( 'tool', 'test cli', - ignoreUnexpectedPositionalArgs: true, subcommands: [ _createBranchCommand(subcommands: [_templateCommand()]), ], diff --git a/packages/config/lib/src/command/abstract_command.dart b/packages/config/lib/src/command/abstract_command.dart index a5d717e..2775b4f 100644 --- a/packages/config/lib/src/command/abstract_command.dart +++ b/packages/config/lib/src/command/abstract_command.dart @@ -83,6 +83,17 @@ abstract class AbstractCommand final CommandContext? prevContext, ) => null; + /// Whether additional positional arguments (that don't match the defined + /// positional options) are allowed after this command. Defaults to false. + /// + /// When true, such arguments are included in [Configuration.remainingPosArgs] + /// of this command and do not fail the run. + /// Otherwise they are reported as usage errors. + /// + /// Setting this to true only has effect for leaf commands. + @override + bool get ignoreUnexpectedPositionalArgs => false; + /// Whether this command accepts a partial command line, i.e. runs as the /// last command on the command line even if the command line is incomplete. /// @@ -192,7 +203,7 @@ abstract class AbstractCommand parsedArgs: parsedArgs, env: env, configBroker: configBroker(prevContext), - ignoreUnexpectedPositionalArgs: runner.ignoreUnexpectedPositionalArgs, + ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, ); return CommandContext( diff --git a/packages/config/lib/src/command/command.dart b/packages/config/lib/src/command/command.dart index d73e261..cd9622d 100644 --- a/packages/config/lib/src/command/command.dart +++ b/packages/config/lib/src/command/command.dart @@ -179,25 +179,34 @@ abstract class CommandImpl @override final String name; + @override + final bool ignoreUnexpectedPositionalArgs; + CommandImpl({ required this.name, required final String description, final String? usageFooter, final String? category, final bool? hidden, + final String? invocation, + final String? summary, final List? aliases, final List? suggestionAliases, + final bool? ignoreUnexpectedPositionalArgs, super.persistentOptions, super.options, super.subcommands, super.middlewares, super.canRunAsLeafCommand, - }) : super( + }) : ignoreUnexpectedPositionalArgs = ignoreUnexpectedPositionalArgs ?? false, + super( usageDefinition: CommandUsageDefinition( description: description, usageFooter: usageFooter, category: category, hidden: hidden, + invocation: invocation, + summary: summary, ), aliasesDefinition: CommandAliasesDefinition( aliases: aliases, @@ -216,6 +225,8 @@ abstract class CommandImpl final String? usageFooter, final String? category, final bool? hidden, + final String? invocation, + final String? summary, final List? aliases, final List? suggestionAliases, final List>? middlewares, @@ -229,6 +240,8 @@ abstract class CommandImpl usageFooter: usageFooter, category: category, hidden: hidden, + invocation: invocation, + summary: summary, aliases: aliases, suggestionAliases: suggestionAliases, middlewares: middlewares, @@ -256,8 +269,11 @@ abstract class CommandImpl final String? usageFooter, final String? category, final bool? hidden, + final String? invocation, + final String? summary, final List? aliases, final List? suggestionAliases, + final bool? ignoreUnexpectedPositionalArgs, final List? options, final List>? middlewares, required final CommandRunFunction run, @@ -268,8 +284,11 @@ abstract class CommandImpl usageFooter: usageFooter, category: category, hidden: hidden, + invocation: invocation, + summary: summary, aliases: aliases, suggestionAliases: suggestionAliases, + ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, middlewares: middlewares, options: options, run: run, @@ -284,12 +303,15 @@ class _CommandWithRunFunction _CommandWithRunFunction({ required super.name, required super.description, - required super.usageFooter, - required super.category, - required super.hidden, - required super.aliases, - required super.suggestionAliases, - required super.middlewares, + super.usageFooter, + super.category, + super.hidden, + super.invocation, + super.summary, + super.aliases, + super.suggestionAliases, + super.ignoreUnexpectedPositionalArgs, + super.middlewares, super.persistentOptions, super.options, super.subcommands, diff --git a/packages/config/lib/src/command/command_runner.dart b/packages/config/lib/src/command/command_runner.dart index 496cfa4..60c7ef2 100644 --- a/packages/config/lib/src/command/command_runner.dart +++ b/packages/config/lib/src/command/command_runner.dart @@ -44,6 +44,47 @@ import 'usage_output.dart'; /// Using int as T can be sensible in order to return the exit code that the /// command executable should return. /// +/// ## Usage +/// +/// ### Built-in options and commands +/// +/// The built-in global option `--help` / `-h` is predefined, +/// as is `--completion ` if the runner has no subcommands, +/// see [builtinPersistentOptions]. +/// +/// If the runner has subcommands, the built-in commands are added: `help`, +/// and if completion is enabled, `completion` and`__complete`. +/// +/// ### Action options +/// +/// Action options are arguments that have the syntax of an option flag but run +/// like a command. They are defined by an [OptionDefinition] with the +/// [ActionOptionMixin] mixin. If present, they run instead of a command. +/// A use case example is the `--version` option. +/// +/// ### Pass-through arguments after `--` +/// +/// To accept arguments written after `--`, pass +/// [DoubleDashHandling.passThroughRest]. +/// In addition, the command that receives them must set +/// [ignoreUnexpectedPositionalArgs] to true. +/// It can then read the list from [Configuration.remainingPosArgs]: +/// +/// ```dart +/// CommandRunner( +/// 'tool', +/// 'Tools', +/// parserSettings: const ParserSettings( +/// doubleDashHandling: DoubleDashHandling.passThroughRest, +/// ), +/// ); +/// @override +/// bool get ignoreUnexpectedPositionalArgs => true; +/// ... +/// // tool build -q -- --some --remaining +/// // context.config.remainingPosArgs == ['--some', '--remaining'] +/// ``` +/// /// ## Design /// /// Uses the concept of middlewares to customize the execution logic @@ -114,13 +155,6 @@ class CommandRunner final CommandAliasesDefinition aliasesDefinition = const CommandAliasesDefinition(); - /// Whether a positional argument that matches no positional option is allowed. - /// - /// When true, such arguments are included in [Configuration.remainingPosArgs] - /// of the command that received them and do not fail the run. - /// When false (default) they are reported as a usage error. - final bool ignoreUnexpectedPositionalArgs; - /// Settings for scanning and parsing the command line. final ParserSettings parserSettings; @@ -138,71 +172,60 @@ class CommandRunner /// Creates a new command runner with the given executable name and description. /// - /// [globalOptions] is the list of global options. - /// Global options are "persistent" which means they are valid anywhere on the - /// command line. A subcommand shadows an identical spelling of a global - /// option, and leaves the global option's other spellings in place. + /// [executableName] is the name of the executable being run, as presented in + /// usage and error messages. + /// + /// [description] is the usage description of this root command + /// (the executable). + /// + /// [invocation] overrides the default invocation synopsis of this root + /// command (the executable). + /// + /// [rootFooter] is a string that will be added to this root command's usage + /// footer. /// - /// The built-in global option `--help` / `-h` is available in addition, - /// as is `--completion ` for a runner without subcommands, - /// see [builtinPersistentOptions]. + /// [globalOptions] is the list of global options. Global options are + /// "persistent" which means they are valid anywhere on the command line. /// - /// [subcommands] is a list of subcommands that will be added to - /// the runner. Can also be added later using [addSubcommand]. + /// [subcommands] is a list of subcommands that will be added to the runner. + /// Can also be added later using [addSubcommand]. /// /// [middlewares] is a list of middlewares that will be added to the runner. /// - /// [ignoreUnexpectedPositionalArgs] when true keeps positional arguments - /// that match no positional option on [Configuration.remainingPosArgs]. - /// Defaults to false, which reports them as a usage error. - /// If not provided, it defaults to true if [parserSettings.doubleDashHandling] - /// is [DoubleDashHandling.passThroughRest]. + /// [canRunAsLeafCommand] when true allows this command to run as a leaf + /// command even if it has subcommands. /// - /// [parserSettings] controls parsing and defaults to [ParserSettings]. - /// To accept arguments written after `--`, pass - /// [DoubleDashHandling.passThroughRest]. The command that received - /// them reads the list from [Configuration.remainingPosArgs]: + /// [usageGenerator] overrides the [UsageGenerator] for this root command + /// (not its subcommands, each command has its own). /// - /// ```dart - /// CommandRunner( - /// 'tool', - /// 'Tools', - /// parserSettings: const ParserSettings( - /// doubleDashHandling: DoubleDashHandling.passThroughRest, - /// ), - /// ignoreUnexpectedPositionalArgs: true, - /// ); - /// // tool build -q -- --some --remaining - /// // context.config.remainingPosArgs == ['--some', '--remaining'] - /// ``` + /// [parserSettings] overrides the default [ParserSettings] for this runner + /// and its subcommands. /// - /// [rootFooter] is a string that will be added to the runner's usage footer. + /// [usageOutput] overrides the default [UsageOutputHandler] for this runner + /// and its subcommands. /// - /// [completion] configures the built-in shell completion support, - /// which is enabled by default. See [CompletionSettings]. + /// [completion] configures the built-in shell completion support for this + /// runner and its subcommands. See [CompletionSettings]. CommandRunner( this.executableName, final String description, { - final List? globalOptions, + final String? invocation, final String? rootFooter, + final List? globalOptions, super.subcommands, super.middlewares, super.canRunAsLeafCommand, final UsageGenerator? usageGenerator, - final bool? ignoreUnexpectedPositionalArgs, final ParserSettings? parserSettings, final UsageOutputHandler? usageOutput, final CompletionSettings? completion, }) : usageDefinition = CommandUsageDefinition( description: description, usageFooter: rootFooter, + invocation: invocation, ), usageOutput = usageOutput ?? UsageOutputHandler(), completion = completion ?? const CompletionSettings(), - ignoreUnexpectedPositionalArgs = - ignoreUnexpectedPositionalArgs ?? - parserSettings?.doubleDashHandling == - DoubleDashHandling.passThroughRest, parserSettings = parserSettings ?? const ParserSettings(), super( usageGenerator: usageGenerator ?? const StandardRunnerUsageGenerator(), @@ -242,7 +265,9 @@ class CommandRunner /// and returns `null` without running any command. Middleware still runs /// through the runner, which defines `--help`. /// - /// To make a subcommand which is named like an option, simply define it, for example: + /// To make a subcommand which is named like an option, simply define it + /// like the following example. But typically it is better to define an + /// action option instead. /// ```dart /// CommandImpl.leafWithRunFunction( /// name: 'version', @@ -325,7 +350,6 @@ class CommandRunner final engine = CompletionEngine( this, resolver: resolver, - ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, parserSettings: parserSettings, onError: (final error, final stackTrace) { completion.sinks.err.writeln( diff --git a/packages/config/lib/src/command/completion/completion_engine.dart b/packages/config/lib/src/command/completion/completion_engine.dart index 1adfca9..cbbea0c 100644 --- a/packages/config/lib/src/command/completion/completion_engine.dart +++ b/packages/config/lib/src/command/completion/completion_engine.dart @@ -35,9 +35,6 @@ import 'option_value_completion.dart'; class CompletionEngine { final CommandDefinition rootCommand; - /// See `CommandRunner.ignoreUnexpectedPositionalArgs`. - final bool ignoreUnexpectedPositionalArgs; - /// See `CommandRunner.parserSettings`. final ParserSettings parserSettings; @@ -54,7 +51,6 @@ class CompletionEngine { const CompletionEngine( this.rootCommand, { this.resolver, - this.ignoreUnexpectedPositionalArgs = false, this.parserSettings = const ParserSettings(), this.onError, }); @@ -337,7 +333,7 @@ class CompletionEngine { return _completeOptionValue(line, option.option, current); } } - return ignoreUnexpectedPositionalArgs + return line.leafCommand.ignoreUnexpectedPositionalArgs ? const CompletionResponse.files() : const CompletionResponse.nothing(); } diff --git a/packages/config/lib/src/config/arg_parser/command_definition.dart b/packages/config/lib/src/config/arg_parser/command_definition.dart index cbb9ef7..3e5adca 100644 --- a/packages/config/lib/src/config/arg_parser/command_definition.dart +++ b/packages/config/lib/src/config/arg_parser/command_definition.dart @@ -32,6 +32,10 @@ abstract interface class CommandDefinition { /// If false and this command is the last command found on the command line, /// the command line parser will yield an error. bool get canRunAsLeafCommand => false; + + /// Whether positional arguments that match no positional option are allowed + /// after this command. Defaults to false. Only leaf commands use this. + bool get ignoreUnexpectedPositionalArgs => false; } class CommandUsageDefinition { diff --git a/packages/config/test/command/command_fixtures.dart b/packages/config/test/command/command_fixtures.dart index 68339c0..4c8d5a8 100644 --- a/packages/config/test/command/command_fixtures.dart +++ b/packages/config/test/command/command_fixtures.dart @@ -42,10 +42,13 @@ class RecordingCommand super.aliases, super.suggestionAliases, super.hidden, + super.invocation, + super.summary, super.options, super.persistentOptions, super.subcommands, super.middlewares, + super.ignoreUnexpectedPositionalArgs, this.body, }); @@ -76,8 +79,6 @@ class TracingRunner super.middlewares, super.canRunAsLeafCommand, super.globalOptions, - // super.configBroker, - super.ignoreUnexpectedPositionalArgs, super.rootFooter, super.usageOutput, }) : super('tool', 'Tools'); diff --git a/packages/config/test/command/command_line_run_test.dart b/packages/config/test/command/command_line_run_test.dart index bdcd152..9b2c09f 100644 --- a/packages/config/test/command/command_line_run_test.dart +++ b/packages/config/test/command/command_line_run_test.dart @@ -290,35 +290,42 @@ void main() { }); }); - group('Given a runner that ignores unexpected positional arguments', () { - late List trace; - late TracingRunner runner; + group( + 'Given a leaf command that ignores unexpected positional arguments', + () { + late List trace; + late RecordingCommand command; + late TracingRunner runner; + + setUp(() { + trace = []; + command = RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + ignoreUnexpectedPositionalArgs: true, + ); + runner = TracingRunner( + trace, + subcommands: [command], + ); + }); - setUp(() { - trace = []; - runner = TracingRunner( - trace, - ignoreUnexpectedPositionalArgs: true, - subcommands: [ - RecordingCommand( - name: 'build', - description: 'Build the app', - trace: trace, - ), - ], - ); - }); + test('when reading the command then the flag is set', () { + expect(command.ignoreUnexpectedPositionalArgs, isTrue); + }); - test( - 'when the leaf command receives an extra positional argument ' - 'then the command runs', - () async { - await runner.run(['build', 'extra']); + test( + 'when the leaf command receives an extra positional argument ' + 'then the command runs', + () async { + await runner.run(['build', 'extra']); - expect(trace, ['build']); - }, - ); - }); + expect(trace, ['build']); + }, + ); + }, + ); group('Given a command created with a run function', () { test( diff --git a/packages/config/test/command/command_tree_test.dart b/packages/config/test/command/command_tree_test.dart index b98a1d6..64400de 100644 --- a/packages/config/test/command/command_tree_test.dart +++ b/packages/config/test/command/command_tree_test.dart @@ -324,4 +324,99 @@ void main() { expect(identical(runner.subcommands.single, help), isTrue); }); }); + + group('Given a leaf command created from parameters', () { + late Command command; + + setUp(() { + command = CommandImpl.leafWithRunFunction( + name: 'get', + description: 'Download packages.\nMore detail here.', + invocation: 'get ', + summary: 'Download packages', + ignoreUnexpectedPositionalArgs: true, + run: (final _) {}, + ); + }); + + test('when reading the usage definition then the summary is kept', () { + expect(command.usageDefinition.summary, 'Download packages'); + }); + + test('when reading the usage definition then the invocation is kept', () { + expect(command.usageDefinition.invocation, 'get '); + }); + + test( + 'when reading the command ' + 'then unexpected positional arguments are ignored', + () { + expect(command.ignoreUnexpectedPositionalArgs, isTrue); + }, + ); + }); + + group('Given a leaf command created without those parameters', () { + late Command command; + + setUp(() { + command = CommandImpl.leafWithRunFunction( + name: 'get', + description: 'Download packages', + run: (final _) {}, + ); + }); + + test( + 'when reading the usage definition ' + 'then the summary is left to the usage generator', + () { + expect(command.usageDefinition.summary, isNull); + }, + ); + + test( + 'when reading the usage definition ' + 'then the invocation is left to the usage generator', + () { + expect(command.usageDefinition.invocation, isNull); + }, + ); + + test( + 'when reading the command ' + 'then unexpected positional arguments are reported', + () { + expect(command.ignoreUnexpectedPositionalArgs, isFalse); + }, + ); + }); + + group('Given a branch command created from parameters', () { + late Command command; + + setUp(() { + command = CommandImpl.branch( + name: 'remote', + description: 'Manage remotes.\nMore detail here.', + invocation: 'remote ', + summary: 'Manage remotes', + subcommands: [ + CommandImpl.leafWithRunFunction( + name: 'add', + description: 'Add a remote', + run: (final _) {}, + ), + ], + ); + }); + + test('when reading the usage definition then the summary is kept', () { + expect(command.usageDefinition.summary, 'Manage remotes'); + }); + + test('when reading the usage definition then the invocation is kept', () { + expect(command.usageDefinition.invocation, 'remote '); + }); + }); } diff --git a/packages/config/test/command/completion/completion_engine_test.dart b/packages/config/test/command/completion/completion_engine_test.dart index 2ec5518..d6b58fd 100644 --- a/packages/config/test/command/completion/completion_engine_test.dart +++ b/packages/config/test/command/completion/completion_engine_test.dart @@ -468,7 +468,7 @@ void main() { }); test('when all positional arguments are given ' - 'and unexpected positional arguments are ignored ' + 'and the leaf command ignores unexpected positional arguments ' 'then file completion is requested', () async { runner = buildCompletionCli( usageOutput: UsageOutputHandler(sinks: OutputSinks(out: out)), diff --git a/packages/config/test/command/completion/fixtures/completion_cli.dart b/packages/config/test/command/completion/fixtures/completion_cli.dart index 42e459e..f0252fb 100644 --- a/packages/config/test/command/completion/fixtures/completion_cli.dart +++ b/packages/config/test/command/completion/fixtures/completion_cli.dart @@ -210,7 +210,6 @@ CommandRunner buildCompletionCli({ output: completionOutput, errorOutput: completionError, ), - ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, parserSettings: parserSettings, middlewares: [ Middlewares.onBeforeRun((final ctx) { @@ -221,6 +220,7 @@ CommandRunner buildCompletionCli({ CommandImpl.leafWithRunFunction( name: 'build', description: 'Build the project.\nIn one of the build modes.', + ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, options: [ modeOption, outOption, @@ -236,12 +236,14 @@ CommandRunner buildCompletionCli({ CommandImpl.leafWithRunFunction( name: 'deploy', description: 'Deploy a bundle to a target.', + ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, options: [targetOption, bundleOption], run: run, ), CommandImpl.leafWithRunFunction( name: 'export', description: 'Export data in one of the formats.', + ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, options: [jsonOption, yamlOption, stdinOption, inputOption], run: run, ), @@ -249,12 +251,14 @@ CommandRunner buildCompletionCli({ name: 'internal', description: 'A hidden command.', hidden: true, + ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, run: run, ), CommandImpl.leafWithRunFunction( name: 'version', aliases: ['--version'], description: 'Display the version.', + ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, run: run, ), CommandImpl.branch( @@ -264,12 +268,14 @@ CommandRunner buildCompletionCli({ CommandImpl.leafWithRunFunction( name: 'migrate', description: 'Run the migrations.', + ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, options: [stepsOption], run: run, ), CommandImpl.leafWithRunFunction( name: 'seed', description: 'Seed the database.', + ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, run: run, ), ], @@ -285,7 +291,6 @@ CommandRunner buildLeafCompletionCli({ final CompletionSettings completion = inProcessCompletion, final StringSink? completionOutput, final StringSink? completionError, - final bool ignoreUnexpectedPositionalArgs = false, }) { return CommandRunner( 'leaftool', @@ -298,7 +303,6 @@ CommandRunner buildLeafCompletionCli({ errorOutput: completionError, ), canRunAsLeafCommand: true, - ignoreUnexpectedPositionalArgs: ignoreUnexpectedPositionalArgs, ) // Positional options can't be persistent, add them as regular options. ..addOption(targetOption) diff --git a/packages/config/test/command/usage_formatting_test.dart b/packages/config/test/command/usage_formatting_test.dart index 38b3600..7fa646c 100644 --- a/packages/config/test/command/usage_formatting_test.dart +++ b/packages/config/test/command/usage_formatting_test.dart @@ -1290,17 +1290,23 @@ void main() { late CommandRunner runner; setUp(() { - command = _OverrideUsageCommand( + command = _command( name: 'get', - usageDefinition: const CommandUsageDefinition( - description: 'Download packages.\nMore detail here.', - summary: 'Download packages', - invocation: 'get ', - ), + description: 'Download packages.\nMore detail here.', + summary: 'Download packages', + invocation: 'get ', ); runner = _runner(subcommands: [command]); }); + test('when reading the usage definition then the summary is kept', () { + expect(command.usageDefinition.summary, 'Download packages'); + }); + + test('when reading the usage definition then the invocation is kept', () { + expect(command.usageDefinition.invocation, 'get '); + }); + test('when reading the parent usage then the custom summary is shown', () { expect( runner.usage, @@ -1342,12 +1348,10 @@ void main() { late Command command; setUp(() { - command = _OverrideUsageCommand( + command = _command( name: 'add', - usageDefinition: const CommandUsageDefinition( - description: 'Add a remote', - invocation: 'add ', - ), + description: 'Add a remote', + invocation: 'add ', ); final branch = _command( name: 'remote', @@ -1357,6 +1361,10 @@ void main() { _runner(subcommands: [branch]); }); + test('when reading the usage definition then the invocation is kept', () { + expect(command.usageDefinition.invocation, 'add '); + }); + test( 'when reading usage then the invocation replaces only this command', () { @@ -1421,6 +1429,8 @@ void main() { Command _command({ required final String name, required final String description, + final String? invocation, + final String? summary, final List? aliases, final bool? hidden, final bool canRunAsLeafCommand = false, @@ -1431,6 +1441,8 @@ Command _command({ return _UsageCommand( name: name, description: description, + invocation: invocation, + summary: summary, aliases: aliases, hidden: hidden, canRunAsLeafCommand: canRunAsLeafCommand, @@ -1442,23 +1454,12 @@ Command _command({ enum _Pace { fast, slow } -class _OverrideUsageCommand extends Command { - @override - final String name; - - _OverrideUsageCommand({ - required this.name, - required final CommandUsageDefinition usageDefinition, - }) : super(usageDefinition: usageDefinition); - - @override - void run(final CommandContext context) {} -} - class _UsageCommand extends CommandImpl { _UsageCommand({ required super.name, required super.description, + super.invocation, + super.summary, super.aliases, super.hidden, super.canRunAsLeafCommand, diff --git a/packages/config/test/config/arg_parser/arg_parser_test_support.dart b/packages/config/test/config/arg_parser/arg_parser_test_support.dart index c108675..1d313cd 100644 --- a/packages/config/test/config/arg_parser/arg_parser_test_support.dart +++ b/packages/config/test/config/arg_parser/arg_parser_test_support.dart @@ -15,6 +15,7 @@ class TestCommand implements CommandDefinition { this.options = const [], this.subcommands = const [], this.canRunAsLeafCommand = false, + this.ignoreUnexpectedPositionalArgs = false, }); @override @@ -46,4 +47,7 @@ class TestCommand implements CommandDefinition { @override final bool canRunAsLeafCommand; + + @override + final bool ignoreUnexpectedPositionalArgs; } diff --git a/packages/config/test/config/arg_parser/scan_and_parse_commands_test.dart b/packages/config/test/config/arg_parser/scan_and_parse_commands_test.dart index 6cddea3..2e0cf1c 100644 --- a/packages/config/test/config/arg_parser/scan_and_parse_commands_test.dart +++ b/packages/config/test/config/arg_parser/scan_and_parse_commands_test.dart @@ -2983,4 +2983,7 @@ class _EnumCommand implements CommandDefinition { @override final bool canRunAsLeafCommand = false; + + @override + final bool ignoreUnexpectedPositionalArgs = false; }