slop 2.4.2 → 2.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES.md CHANGED
@@ -1,3 +1,10 @@
1
+ HEAD
2
+ ----
3
+
4
+ * Allow the `:as` option to accept an object responding to :call for
5
+ custom type conversions (#45)
6
+ * Ensure negative integers are not parsed as possible options (#46)
7
+
1
8
  2.4.2 (2011-12-18)
2
9
  ------------------
3
10
 
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Slop
2
2
  ====
3
3
 
4
- Slop is a simple option parser with an easy to remember syntax and friendly API.
4
+ Slop is a simple, lightweight option parser with an easy to remember syntax and friendly API.
5
5
 
6
6
  Installation
7
7
  ------------
@@ -290,4 +290,4 @@ opts = Slop.parse do
290
290
  end
291
291
 
292
292
  opts.to_hash #=> { :name => 'lee', :age => 105 }
293
- ```
293
+ ```
data/lib/slop.rb CHANGED
@@ -2,7 +2,7 @@ class Slop
2
2
  include Enumerable
3
3
 
4
4
  # @return [String] The current version string
5
- VERSION = '2.4.2'
5
+ VERSION = '2.4.3'
6
6
 
7
7
  # Slops standard Error class. All exception classes should
8
8
  # inherit from this class
@@ -92,7 +92,8 @@ class Slop
92
92
 
93
93
  @delimiter = @options.fetch(:delimiter, ',')
94
94
  @limit = @options.fetch(:limit, 0)
95
- @argument_type = @options[:as].to_s.downcase
95
+
96
+ @argument_type = @options[:as]
96
97
  @argument_value = nil
97
98
 
98
99
  self.forced = false
@@ -129,7 +130,7 @@ class Slop
129
130
  #
130
131
  # @param [Object] value The value to set this options argument to
131
132
  def argument_value=(value)
132
- if @argument_type == 'array'
133
+ if @argument_type.to_s.downcase == 'array'
133
134
  @argument_value ||= []
134
135
 
135
136
  if value.respond_to?(:to_str)
@@ -144,27 +145,32 @@ class Slop
144
145
  # according to the `:as` option
145
146
  def argument_value
146
147
  return @argument_value if forced
148
+ type = @argument_type.to_s.downcase
147
149
  # Check for count first to prefer 0 over nil
148
- return count if @argument_type == 'count'
150
+ return count if type == 'count'
149
151
 
150
152
  value = @argument_value || @options[:default]
151
153
  return if value.nil?
152
154
 
153
- case @argument_type
154
- when 'array'
155
- arg_value(@argument_value)
156
- when 'range'
157
- arg_value(value_to_range(value))
158
- when 'float'
159
- arg_value(value.to_s.to_f)
160
- when 'string', 'str'
161
- arg_value(value.to_s)
162
- when 'symbol', 'sym'
163
- arg_value(value.to_s.to_sym)
164
- when 'integer', 'int'
165
- arg_value(value.to_s.to_i)
155
+ if @argument_type.respond_to?(:call)
156
+ @argument_type.call(value)
166
157
  else
167
- value
158
+ case type
159
+ when 'array'
160
+ arg_value(@argument_value)
161
+ when 'range'
162
+ arg_value(value_to_range(value))
163
+ when 'float'
164
+ arg_value(value.to_s.to_f)
165
+ when 'string', 'str'
166
+ arg_value(value.to_s)
167
+ when 'symbol', 'sym'
168
+ arg_value(value.to_s.to_sym)
169
+ when 'integer', 'int'
170
+ arg_value(value.to_s.to_i)
171
+ else
172
+ value
173
+ end
168
174
  end
169
175
  end
170
176
 
@@ -802,7 +808,6 @@ class Slop
802
808
  end
803
809
 
804
810
  trash = []
805
- ignore_all = false
806
811
 
807
812
  items.each_with_index do |item, index|
808
813
  item = item.to_s
@@ -810,14 +815,13 @@ class Slop
810
815
 
811
816
  if item == '--'
812
817
  trash << index
813
- ignore_all = true
818
+ break
814
819
  end
815
820
 
816
- next if ignore_all
817
821
  autocreate(flag, index, items) if @autocreate
818
822
  option, argument = extract_option(item, flag)
819
823
 
820
- if @multiple_switches and item[/\A-[^-]/] and not option
824
+ if @multiple_switches and item[/\A-[^-][A-Za-z]/] and not option
821
825
  trash << index
822
826
  next
823
827
  end
data/slop.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'slop'
3
- s.version = '2.4.2'
3
+ s.version = '2.4.3'
4
4
  s.summary = 'Option gathering made easy'
5
5
  s.description = 'A simple DSL for gathering options and parsing the command line'
6
6
  s.author = 'Lee Jarvis'
data/test/option_test.rb CHANGED
@@ -86,6 +86,9 @@ class OptionTest < TestCase
86
86
  assert_equal 1, option_value(%w/--verbose/, :v, :verbose, :as => :count)
87
87
  assert_equal 2, option_value(%w/--verbose -v/, :v, :verbose, :as => :count)
88
88
  assert_equal 3, option_value(%w/-vvv/, :v, :verbose, :as => :count)
89
+
90
+ assert_equal 1, option_value(%w/-i 1/, :i, true, :as => proc { |x| x.to_i })
91
+ assert_equal "oof", option_value(%w/-i foo/, :i, true, :as => proc { |x| x.reverse })
89
92
  end
90
93
 
91
94
  test 'casting should return nil for optionless arguments' do
data/test/slop_test.rb CHANGED
@@ -587,4 +587,10 @@ class SlopTest < TestCase
587
587
  assert opts.verbose?
588
588
  assert_equal 'enable quiet mode', opts.options[:quiet].description
589
589
  end
590
+
591
+ test "negative integers should not be processed as options and removed" do
592
+ items = %w(-1)
593
+ Slop.parse!(items)
594
+ assert_equal %w(-1), items
595
+ end
590
596
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slop
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.2
4
+ version: 2.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-18 00:00:00.000000000 Z
12
+ date: 2012-01-16 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple DSL for gathering options and parsing the command line
15
15
  email: lee@jarvis.co