From d474124497b2b924de22a0130d10159ee461ff65 Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Sat, 1 Aug 2026 16:56:56 +0200 Subject: [PATCH] Register UI options with custom CLI objects --- src/cli.toit | 7 +++---- tests/command_group_options_test.toit | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/cli.toit b/src/cli.toit index a0fd585..e459f2a 100644 --- a/src/cli.toit +++ b/src/cli.toit @@ -350,9 +350,9 @@ class Command: If no $cli is given, the arguments are parsed for `--verbose`, `--verbosity-level` and `--output-format` to create the appropriate UI object. If a $cli object is given, - then these arguments are ignored. + then these arguments do not change its UI. - The $add-ui-help flag is used to determine whether to include help for `--verbose`, ... + The $add-ui-help flag determines whether these UI options are accepted and included in the help output. By default it is active if no $cli is provided. The $completion-as-flag parameter controls whether shell completion is exposed as a @@ -439,9 +439,8 @@ class Command: if not cli: ui := create-ui-from-args_ arguments log.set-default (ui.logger --name=name) - if add-ui-help: - add-ui-options_ cli = Cli_ name --ui=ui --cache=null --config=null + if add-ui-help: add-ui-options_ parser := Parser_ --invoked-command=invoked-command parser.parse this arguments: | path/Path parameters/Parameters | invocation := Invocation.private_ cli path.commands parameters diff --git a/tests/command_group_options_test.toit b/tests/command_group_options_test.toit index 72ab212..36bf415 100644 --- a/tests/command_group_options_test.toit +++ b/tests/command_group_options_test.toit @@ -18,6 +18,7 @@ When a subcommand is found on a CommandGroup, the parser goes directly main: test-commands-option-accessible-from-subcommand test-commands-option-accessible-with-value + test-ui-option-with-custom-cli test-commands-option-accessible-from-subcommand: sub-invoked := false @@ -67,3 +68,22 @@ test-commands-option-accessible-with-value: root.run ["--sdk-dir", "/my/sdk", "run"] expect-equals "/my/sdk" captured-value + +test-ui-option-with-custom-cli: + captured-format := null + commands-cmd := cli.Command "commands" + commands-cmd.add + cli.Command "run" + --run=:: | invocation/cli.Invocation | + captured-format = invocation["output-format"] + + root := cli.CommandGroup "app" + --default=(cli.Command "default" + --rest=[cli.Option "source" --required] + --run=:: unreachable) + --commands=commands-cmd + + root.run ["--output-format", "json", "run"] + --cli=(cli.Cli "app" --ui=(cli.Ui.human --level=cli.Ui.SILENT-LEVEL)) + --add-ui-help + expect-equals "json" captured-format