Skip to content

Commit af61d97

Browse files
authored
Fix OptionParser subcommand help to respect custom summary_indent (#16334)
1 parent 0540803 commit af61d97

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

spec/std/option_parser_spec.cr

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,4 +899,25 @@ describe "OptionParser with summary_width and summary_indent" do
899899
parser.summary_width = -10
900900
end
901901
end
902+
903+
it "formats subcommand help with custom summary_indent" do
904+
help = nil
905+
OptionParser.parse(%w(subcommand --help)) do |opts|
906+
opts.summary_indent = "||"
907+
opts.banner = "Usage: foo"
908+
909+
opts.on("subcommand", "Subcommand description") do
910+
opts.banner = "Usage: foo subcommand"
911+
opts.on("--local", "Local flag") { }
912+
end
913+
opts.on("other", "Other subcommand") { }
914+
opts.on("--help", "Help") { help = opts.to_s }
915+
end
916+
917+
help.should eq <<-USAGE
918+
Usage: foo subcommand
919+
||--help Help
920+
||--local Local flag
921+
USAGE
922+
end
902923
end

src/option_parser.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ class OptionParser
493493
# subcommands since they are no longer valid.
494494
unless flag.starts_with?('-')
495495
@handlers.select! { |k, _| k.starts_with?('-') }
496-
@flags.select!(&.starts_with?(" -"))
496+
@flags.select!(&.starts_with?("#{summary_indent}-"))
497497
end
498498

499499
handler.block.call(value || "")

0 commit comments

Comments
 (0)