-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Permitted regexp/range support #158
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
require 'stringio' | ||
require 'test_helper' | ||
|
||
module Optimist | ||
|
||
class ParserPermittedTest < ::Minitest::Test | ||
def setup | ||
@p = Parser.new | ||
end | ||
|
||
def test_permitted_flags_filter_inputs | ||
@p.opt "arg", "desc", :type => :strings, :permitted => %w(foo bar) | ||
|
||
result = @p.parse(%w(--arg foo)) | ||
assert_equal ["foo"], result["arg"] | ||
assert_raises(CommandlineError) { @p.parse(%w(--arg baz)) } | ||
end | ||
|
||
def test_permitted_invalid_scalar_value | ||
err_regexp = /permitted values for option "(bad|mad|sad)" must be either nil, Range, Regexp or an Array/ | ||
assert_raises(ArgumentError, err_regexp) { | ||
@p.opt 'bad', 'desc', :permitted => 1 | ||
} | ||
assert_raises(ArgumentError, err_regexp) { | ||
@p.opt 'mad', 'desc', :permitted => "A" | ||
} | ||
assert_raises_errmatch(ArgumentError, err_regexp) { | ||
@p.opt 'sad', 'desc', :permitted => :abcd | ||
} | ||
end | ||
|
||
def test_permitted_with_string_array | ||
@p.opt 'fiz', 'desc', :type => 'string', :permitted => ['foo', 'bar'] | ||
@p.parse(%w(--fiz foo)) | ||
assert_raises_errmatch(CommandlineError, /option '--fiz' only accepts one of: foo, bar/) { | ||
@p.parse(%w(--fiz buz)) | ||
} | ||
end | ||
def test_permitted_with_symbol_array | ||
@p.opt 'fiz', 'desc', :type => 'string', :permitted => %i[dog cat] | ||
@p.parse(%w(--fiz dog)) | ||
@p.parse(%w(--fiz cat)) | ||
assert_raises_errmatch(CommandlineError, /option '--fiz' only accepts one of: dog, cat/) { | ||
@p.parse(%w(--fiz rat)) | ||
} | ||
end | ||
|
||
def test_permitted_with_numeric_array | ||
@p.opt 'mynum', 'desc', :type => Integer, :permitted => [1,2,4] | ||
@p.parse(%w(--mynum 1)) | ||
@p.parse(%w(--mynum 4)) | ||
assert_raises_errmatch(CommandlineError, /option '--mynum' only accepts one of: 1, 2, 4/) { | ||
@p.parse(%w(--mynum 3)) | ||
} | ||
end | ||
|
||
def test_permitted_with_numeric_range | ||
@p.opt 'fiz', 'desc', :type => Integer, :permitted => 1..3 | ||
opts = @p.parse(%w(--fiz 1)) | ||
assert_equal opts['fiz'], 1 | ||
opts = @p.parse(%w(--fiz 3)) | ||
assert_equal opts['fiz'], 3 | ||
assert_raises_errmatch(CommandlineError, /option '--fiz' only accepts value in range of: 1\.\.3/) { | ||
@p.parse(%w(--fiz 4)) | ||
} | ||
end | ||
|
||
def test_permitted_with_regexp | ||
@p.opt 'zipcode', 'desc', :type => String, :permitted => /^[0-9]{5}$/ | ||
@p.parse(%w(--zipcode 39762)) | ||
err_regexp = %r|option '--zipcode' only accepts value matching: /\^\[0-9\]\{5\}\$/| | ||
assert_raises_errmatch(CommandlineError, err_regexp) { | ||
@p.parse(%w(--zipcode A9A9AA)) | ||
} | ||
end | ||
def test_permitted_with_reason | ||
# test all keys passed into the formatter for the permitted_response | ||
@p.opt 'zipcode', 'desc', type: String, permitted: /^[0-9]{5}$/, | ||
permitted_response: "opt %{arg} should be a zipcode but you have %{value}" | ||
@p.opt :wig, 'wig', type: Integer, permitted: 1..4, | ||
permitted_response: "opt %{arg} exceeded four wigs (%{valid_string}), %{permitted}, but you gave '%{given}'" | ||
err_regexp = %r|opt --zipcode should be a zipcode but you have A9A9AA| | ||
assert_raises_errmatch(CommandlineError, err_regexp) { | ||
@p.parse(%w(--zipcode A9A9AA)) | ||
} | ||
err_regexp = %r|opt --wig exceeded four wigs \(value in range of: 1\.\.4\), 1\.\.4, but you gave '5'| | ||
assert_raises_errmatch(CommandlineError, err_regexp) { | ||
@p.parse(%w(--wig 5)) | ||
} | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a shame we can't use .include? directly on the Range. Something we might want to consider in the future somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iirc I did this b/c of the typing mismatch between the given arg from the cmdline (always a string) and the range type, though maybe it could be compared the other way around?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Offhand, the way you did it is the only way I can think of, so overall I'm fine to merge. We get strings from the command line so there's really not much we can do about that.