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: 9365b251ad45
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: fa22a8d98945
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Sep 4, 2016

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    7ea54e2 View commit details

Commits on Sep 5, 2016

  1. Merge pull request #3246 from grosser/grosser/short-arg

    OptionParser: parse arguments from shorflag if it is only referenced …
    Ary Borenszweig authored Sep 5, 2016
    Copy the full SHA
    fa22a8d View commit details
Showing with 28 additions and 1 deletion.
  1. +21 −1 spec/std/option_parser_spec.cr
  2. +7 −0 src/option_parser.cr
22 changes: 21 additions & 1 deletion spec/std/option_parser_spec.cr
Original file line number Diff line number Diff line change
@@ -133,10 +133,30 @@ describe "OptionParser" do
expect_doesnt_capture_option [] of String, "-f [FLAG]"
end

it "doesn't raise if required option is not specified with separated short flag 2" do
it "doesn't raise if required option is not specified with separated short flag" do
expect_doesnt_capture_option [] of String, "-f FLAG"
end

it "parses argument when only referenced in long flag" do
captured = ""
parser = OptionParser.parse([] of String) do |opts|
opts.on("-f", "--flag X", "some flag") { |x| captured = x }
end
parser.parse(["-f", "12"])
captured.should eq "12"
parser.to_s.should contain " -f, --flag X"
end

it "parses argument when referenced in long and short flag" do
captured = ""
parser = OptionParser.parse([] of String) do |opts|
opts.on("-f X", "--flag X", "some flag") { |x| captured = x }
end
parser.parse(["-f", "12"])
captured.should eq "12"
parser.to_s.should contain " -f X, --flag X"
end

it "does to_s with banner" do
parser = OptionParser.parse([] of String) do |opts|
opts.banner = "Usage: foo"
7 changes: 7 additions & 0 deletions src/option_parser.cr
Original file line number Diff line number Diff line change
@@ -107,6 +107,13 @@ class OptionParser
# See the other definition of `on` for examples.
def on(short_flag, long_flag, description, &block : String ->)
append_flag "#{short_flag}, #{long_flag}", description

has_argument = /([ =].+)/
if long_flag =~ has_argument
argument = $1
short_flag += argument unless short_flag =~ has_argument
end

@handlers << Handler.new(short_flag, block)
@handlers << Handler.new(long_flag, block)
end