Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9501337e3975
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 7aad33e0b290
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Sep 14, 2016

  1. OptionParser: handle long flags in to_s. Fixes #3305

    Ary Borenszweig committed Sep 14, 2016
    Copy the full SHA
    18d9c97 View commit details
  2. Use heredoc in OptionParser specs

    Ary Borenszweig committed Sep 14, 2016
    Copy the full SHA
    7aad33e View commit details
Showing with 38 additions and 15 deletions.
  1. +33 −14 spec/std/option_parser_spec.cr
  2. +5 −1 src/option_parser.cr
47 changes: 33 additions & 14 deletions spec/std/option_parser_spec.cr
Original file line number Diff line number Diff line change
@@ -165,11 +165,11 @@ describe "OptionParser" do
opts.on("-g[FLAG]", "some other flag") do
end
end
parser.to_s.should eq([
"Usage: foo",
" -f, --flag some flag",
" -g[FLAG] some other flag",
].join "\n")
parser.to_s.should eq <<-USAGE
Usage: foo
-f, --flag some flag
-g[FLAG] some other flag
USAGE
end

it "does to_s with separators" do
@@ -184,15 +184,34 @@ describe "OptionParser" do
opts.on("-g[FLAG]", "some other flag") do
end
end
parser.to_s.should eq([
"Usage: foo",
"",
"Type F flags:",
" -f, --flag some flag",
"",
"Type G flags:",
" -g[FLAG] some other flag",
].join "\n")
parser.to_s.should eq <<-USAGE
Usage: foo
Type F flags:
-f, --flag some flag
Type G flags:
-g[FLAG] some other flag
USAGE
end

it "does to_s with very long flag (#3305)" do
parser = OptionParser.parse([] of String) do |opts|
opts.banner = "Usage: foo"
opts.on("--very_long_option_kills=formatter", "long") do
end
opts.on("-f", "--flag", "some flag") do
end
opts.on("-g[FLAG]", "some other flag") do
end
end
parser.to_s.should eq <<-USAGE
Usage: foo
--very_long_option_kills=formatter
long
-f, --flag some flag
-g[FLAG] some other flag
USAGE
end

it "raises on invalid option" do
6 changes: 5 additions & 1 deletion src/option_parser.cr
Original file line number Diff line number Diff line change
@@ -153,7 +153,11 @@ class OptionParser
end

private def append_flag(flag, description)
@flags << " #{flag}#{" " * (33 - flag.size)}#{description}"
if flag.size >= 33
@flags << " #{flag}\n#{" " * 37}#{description}"
else
@flags << " #{flag}#{" " * (33 - flag.size)}#{description}"
end
end

# Parses the passed *args*, running the handlers associated to each option.