Skip to content
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

enhancements to permitted option #159

Merged
merged 1 commit into from
May 24, 2024
Merged
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
11 changes: 7 additions & 4 deletions lib/optimist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ def permitted_value?(val)
case permitted
when nil then true
when Regexp then val.match? permitted
when Range then permitted.to_a.map(&:to_s).include? val
when Range then permitted.include? as_type(val)
when Array then permitted.map(&:to_s).include? val
else false
end
Expand Down Expand Up @@ -1009,11 +1009,12 @@ def parse(_paramlist, neg_given)
class FloatOption < Option
register_alias :float, :double
def type_format ; "=<f>" ; end
def as_type(param) ; param.to_f ; end
def parse(paramlist, _neg_given)
paramlist.map do |pg|
pg.map do |param|
raise CommandlineError, "option '#{self.name}' needs a floating-point number" unless param.is_a?(Numeric) || param =~ FLOAT_RE
param.to_f
as_type(param)
end
end
end
Expand All @@ -1023,11 +1024,12 @@ def parse(paramlist, _neg_given)
class IntegerOption < Option
register_alias :int, :integer, :fixnum
def type_format ; "=<i>" ; end
def as_type(param) ; param.to_i ; end
def parse(paramlist, _neg_given)
paramlist.map do |pg|
pg.map do |param|
raise CommandlineError, "option '#{self.name}' needs an integer" unless param.is_a?(Numeric) || param =~ /^-?[\d_]+$/
param.to_i
as_type(param)
end
end
end
Expand Down Expand Up @@ -1060,9 +1062,10 @@ def parse(paramlist, _neg_given)
# Option class for handling Strings.
class StringOption < Option
register_alias :string
def as_type(val) ; val.to_s ; end
def type_format ; "=<s>" ; end
def parse(paramlist, _neg_given)
paramlist.map { |pg| pg.map(&:to_s) }
paramlist.map { |pg| pg.map { |param| as_type(param) } }
end
end

Expand Down
30 changes: 29 additions & 1 deletion test/optimist/parser_permitted_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ def test_permitted_with_numeric_array
}
end

def test_permitted_with_numeric_range
def test_permitted_with_string_range
@p.opt 'fiz', 'desc', :type => String, :permitted => 'A'..'z'
opts = @p.parse(%w(--fiz B))
assert_equal opts['fiz'], "B"
opts = @p.parse(%w(--fiz z))
assert_equal opts['fiz'], "z"
assert_raises_errmatch(CommandlineError, /option '--fiz' only accepts value in range of: A\.\.z/) {
@p.parse(%w(--fiz @))
}
end

def test_permitted_with_integer_range
@p.opt 'fiz', 'desc', :type => Integer, :permitted => 1..3
opts = @p.parse(%w(--fiz 1))
assert_equal opts['fiz'], 1
Expand All @@ -65,6 +76,23 @@ def test_permitted_with_numeric_range
}
end

def test_permitted_with_float_range
@p.opt 'fiz', 'desc', :type => Float, :permitted => 1.2 .. 3.5
opts = @p.parse(%w(--fiz 1.2))
assert_in_epsilon opts['fiz'], 1.2
opts = @p.parse(%w(--fiz 2.7))
assert_in_epsilon opts['fiz'], 2.7
opts = @p.parse(%w(--fiz 3.5))
assert_in_epsilon opts['fiz'], 3.5
err_regexp = /option '--fiz' only accepts value in range of: 1\.2\.\.3\.5/
assert_raises_errmatch(CommandlineError, err_regexp) {
@p.parse(%w(--fiz 3.51))
}
assert_raises_errmatch(CommandlineError, err_regexp) {
@p.parse(%w(--fiz 1.19))
}
end

def test_permitted_with_regexp
@p.opt 'zipcode', 'desc', :type => String, :permitted => /^[0-9]{5}$/
@p.parse(%w(--zipcode 39762))
Expand Down
Loading