From b70111d21cd1dedb6ff0388cb614b00a5d3a9b42 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Thu, 6 Nov 2025 11:17:55 +0900 Subject: [PATCH] Fix OptionParser subcommand help to respect custom summary_indent --- spec/std/option_parser_spec.cr | 21 +++++++++++++++++++++ src/option_parser.cr | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/spec/std/option_parser_spec.cr b/spec/std/option_parser_spec.cr index 789ca149b0f1..af364d2ced06 100644 --- a/spec/std/option_parser_spec.cr +++ b/spec/std/option_parser_spec.cr @@ -899,4 +899,25 @@ describe "OptionParser with summary_width and summary_indent" do parser.summary_width = -10 end end + + it "formats subcommand help with custom summary_indent" do + help = nil + OptionParser.parse(%w(subcommand --help)) do |opts| + opts.summary_indent = "||" + opts.banner = "Usage: foo" + + opts.on("subcommand", "Subcommand description") do + opts.banner = "Usage: foo subcommand" + opts.on("--local", "Local flag") { } + end + opts.on("other", "Other subcommand") { } + opts.on("--help", "Help") { help = opts.to_s } + end + + help.should eq <<-USAGE + Usage: foo subcommand + ||--help Help + ||--local Local flag + USAGE + end end diff --git a/src/option_parser.cr b/src/option_parser.cr index cf2affe0497c..d20ea3b61bf4 100644 --- a/src/option_parser.cr +++ b/src/option_parser.cr @@ -493,7 +493,7 @@ class OptionParser # subcommands since they are no longer valid. unless flag.starts_with?('-') @handlers.select! { |k, _| k.starts_with?('-') } - @flags.select!(&.starts_with?(" -")) + @flags.select!(&.starts_with?("#{summary_indent}-")) end handler.block.call(value || "")