Skip to content
Open
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
21 changes: 21 additions & 0 deletions spec/std/option_parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note on Flag Ordering
In the help output, common flags like --help appear before subcommand-specific flags like --local. This happens because OptionParser shows flags in the order they are registered.

This may not be the best user experience, but fixing the flag order is not part of this PR.

USAGE
end
end
2 changes: 1 addition & 1 deletion src/option_parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 || "")
Expand Down