-
Notifications
You must be signed in to change notification settings - Fork 36
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
Add exact_match to settings, defaulting to inexact matching #154
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5a92e4f
Add exact_match to settings, defaulting to inexact matching
nanobowers a3e34f5
implementation of inexact match feature
nanobowers 7514de5
fix argument match regex
nanobowers 245afb0
fix exact match default to true
nanobowers 657bf8b
fix exact_match default in documentation
nanobowers 6c15fd3
fixed tests for new default value of exact_match (true)
nanobowers d21d718
replacing regexp with String#start_with?
nanobowers 1bc055f
fixed subtle logic bug
nanobowers 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,10 +45,12 @@ def test_synopsis | |
|
||
|
||
def test_unknown_arguments | ||
assert_raises(CommandlineError) { @p.parse(%w(--arg)) } | ||
err = assert_raises(CommandlineError) { @p.parse(%w(--arg)) } | ||
assert_match(/unknown argument '--arg'/, err.message) | ||
@p.opt "arg" | ||
@p.parse(%w(--arg)) | ||
assert_raises(CommandlineError) { @p.parse(%w(--arg2)) } | ||
err = assert_raises(CommandlineError) { @p.parse(%w(--arg2)) } | ||
assert_match(/unknown argument '--arg2'/, err.message) | ||
end | ||
|
||
def test_unknown_arguments_with_suggestions | ||
|
@@ -779,6 +781,20 @@ def test_arguments_passed_through_block | |
assert_equal @goat, boat | ||
end | ||
|
||
## test-only access reader method so that we dont have to | ||
## expose settings in the public API. | ||
class Optimist::Parser | ||
def get_settings_for_testing ; return @settings ;end | ||
end | ||
|
||
def test_two_arguments_passed_through_block | ||
newp = Parser.new(:abcd => 123, :efgh => "other" ) do |i| | ||
end | ||
Comment on lines
+790
to
+792
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is interesting - I actually thought this should raise an exception. (We can discuss separately, I won't hold up the PR for this) |
||
assert_equal newp.get_settings_for_testing[:abcd], 123 | ||
assert_equal newp.get_settings_for_testing[:efgh], "other" | ||
end | ||
|
||
|
||
def test_version_and_help_override_errors | ||
@p.opt :asdf, "desc", :type => String | ||
@p.version "version" | ||
|
@@ -1161,6 +1177,53 @@ def test_default_shorts_assigned_only_after_user_shorts | |
assert opts[:ccd] | ||
end | ||
|
||
def test_inexact_match | ||
newp = Parser.new(exact_match: false) | ||
newp.opt :liberation, "liberate something", :type => :int | ||
newp.opt :evaluate, "evaluate something", :type => :string | ||
opts = newp.parse %w(--lib 5 --ev bar) | ||
assert_equal 5, opts[:liberation] | ||
assert_equal 'bar', opts[:evaluate] | ||
assert_nil opts[:eval] | ||
end | ||
|
||
def test_exact_match | ||
newp = Parser.new() | ||
newp.opt :liberation, "liberate something", :type => :int | ||
newp.opt :evaluate, "evaluate something", :type => :string | ||
assert_raises(CommandlineError, /unknown argument '--lib'/) do | ||
newp.parse %w(--lib 5) | ||
end | ||
assert_raises_errmatch(CommandlineError, /unknown argument '--ev'/) do | ||
newp.parse %w(--ev bar) | ||
end | ||
end | ||
|
||
def test_inexact_collision | ||
newp = Parser.new(exact_match: false) | ||
newp.opt :bookname, "name of a book", :type => :string | ||
newp.opt :bookcost, "cost of the book", :type => :string | ||
opts = newp.parse %w(--bookn hairy_potsworth --bookc 10) | ||
assert_equal 'hairy_potsworth', opts[:bookname] | ||
assert_equal '10', opts[:bookcost] | ||
assert_raises(CommandlineError) do | ||
newp.parse %w(--book 5) # ambiguous | ||
end | ||
## partial match causes 'specified multiple times' error | ||
assert_raises(CommandlineError, /specified multiple times/) do | ||
newp.parse %w(--bookc 17 --bookcost 22) | ||
end | ||
end | ||
|
||
def test_inexact_collision_with_exact | ||
newp = Parser.new(exact_match: false) | ||
newp.opt :book, "name of a book", :type => :string, :default => "ABC" | ||
newp.opt :bookcost, "cost of the book", :type => :int, :default => 5 | ||
opts = newp.parse %w(--book warthog --bookc 3) | ||
assert_equal 'warthog', opts[:book] | ||
assert_equal 3, opts[:bookcost] | ||
end | ||
|
||
def test_accepts_arguments_with_spaces | ||
@p.opt :arg1, "arg", :type => String | ||
@p.opt :arg2, "arg2", :type => String | ||
|
@@ -1316,6 +1379,37 @@ def test_ignore_invalid_options_stop_on_unknown_partial_mid_short | |
assert opts[:arg1] | ||
assert_equal %w{-bu potato}, @p.leftovers | ||
end | ||
|
||
# Due to strangeness in how the cloaker works, there were | ||
# cases where Optimist.parse would work, but Optimist.options | ||
# did not, depending on arguments given to the function. | ||
# These serve to validate different args given to Optimist.options | ||
def test_options_takes_hashy_settings | ||
passargs_copy = [] | ||
settings_copy = [] | ||
::Optimist.options(%w(--wig --pig), :fizz=>:buzz, :bear=>:cat) do |*passargs| | ||
opt :wig | ||
opt :pig | ||
passargs_copy = passargs.dup | ||
settings_copy = @settings | ||
end | ||
assert_equal [], passargs_copy | ||
assert_equal settings_copy[:fizz], :buzz | ||
assert_equal settings_copy[:bear], :cat | ||
end | ||
|
||
def test_options_takes_some_other_data | ||
passargs_copy = [] | ||
settings_copy = [] | ||
::Optimist.options(%w(--nose --close), 1, 2, 3) do |*passargs| | ||
opt :nose | ||
opt :close | ||
passargs_copy = passargs.dup | ||
settings_copy = @settings | ||
end | ||
assert_equal [1,2,3], passargs_copy | ||
assert_equal(Optimist::Parser::DEFAULT_SETTINGS, settings_copy) | ||
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.
Is it a problem to expose settings in the public API? I'm wondering if we should just allow it?
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.
(Note: it's fine for this PR to keep them private, and I won't hold up merge for that)