slop 2.4.4 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  class OptionTest < TestCase
4
4
  def option(*args, &block)
5
- Slop.new.option(*args, &block)
5
+ Slop.new.on(*args, &block)
6
6
  end
7
7
 
8
8
  def option_with_argument(*args, &block)
@@ -10,196 +10,102 @@ class OptionTest < TestCase
10
10
  slop = Slop.new
11
11
  option = slop.opt(*args)
12
12
  slop.parse(options)
13
- slop.find {|opt| opt.key == option.key }
13
+ slop.options.find {|opt| opt.key == option.key }
14
14
  end
15
15
 
16
16
  def option_value(*args, &block)
17
- option_with_argument(*args, &block).argument_value
17
+ option_with_argument(*args, &block).value
18
18
  end
19
19
 
20
- test 'expects an argument if argument is true or optional is false' do
21
- assert option(:f, :foo, 'foo', true).expects_argument?
22
- assert option(:f, :argument => true).expects_argument?
23
- assert option(:f, :optional => false).expects_argument?
24
-
25
- refute option(:f, :foo).expects_argument?
26
- end
27
-
28
- test 'expects an argument if long option is suffixed with =' do
29
- assert option(:f, :foo=).expects_argument?
30
- assert option('f', 'foo=').expects_argument?
20
+ test "expects_argument?" do
31
21
  assert option(:f=).expects_argument?
22
+ assert option(:foo=).expects_argument?
23
+ assert option(:foo, :argument => true).expects_argument?
32
24
  end
33
25
 
34
- test 'accepts an optional argument if optional is true' do
35
- assert option(:f, :optional => true).accepts_optional_argument?
36
- assert option(:f, false, :optional => true).accepts_optional_argument?
37
-
38
- refute option(:f, true).accepts_optional_argument?
26
+ test "accepts_optional_argument?" do
27
+ refute option(:f=).accepts_optional_argument?
28
+ assert option(:f=, :argument => :optional).accepts_optional_argument?
29
+ assert option(:f, :optional_argument => true).accepts_optional_argument?
39
30
  end
40
31
 
41
- test 'has a callback when passed a block or callback option' do
42
- item = nil
43
- option(:f){ item = "foo" }.call
44
- assert_equal "foo", item
45
-
46
- assert option(:callback => proc { item = "bar" }).call
47
- assert_equal "bar", item
48
-
49
- refute option(:f).call
50
- end
51
-
52
- test 'splits argument_value with :as => array' do
53
- assert_equal %w/lee john bill/, option_value(
54
- %w/--people lee,john,bill/, :people, true, :as => Array
55
- )
56
-
57
- assert_equal %w/lee john bill/, option_value(
58
- %w/--people lee:john:bill/,
59
- :people, true, :as => Array, :delimiter => ':'
60
- )
61
-
62
- assert_equal ['lee', 'john,bill'], option_value(
63
- %w/--people lee,john,bill/,
64
- :people, true, :as => Array, :limit => 2
65
- )
66
-
67
- assert_equal ['lee', 'john:bill'], option_value(
68
- %w/--people lee:john:bill/,
69
- :people, true, :as => Array, :limit => 2, :delimiter => ':'
70
- )
32
+ test "key" do
33
+ assert_equal 'foo', option(:foo).key
34
+ assert_equal 'foo', option(:f, :foo).key
35
+ assert_equal 'f', option(:f).key
71
36
  end
72
37
 
73
- test 'casting' do
74
- assert_equal :foo, option_value(%w/--name foo/, :name, true, :as => Symbol)
75
- assert_equal :foo, option_value(%w/--name foo/, :name, true, :as => :symbol)
76
- assert_equal :foo, option_value(%w/--name foo/, :name, true, :as => :sym)
77
- assert_equal 30, option_value(%w/--age 30/, :age, true, :as => Integer)
78
- assert_equal "1.0", option_value(%w/--id 1/, :id, true, :as => Float).to_s
79
-
80
- assert_equal -1, option_value(%w/-i -1/, :i, true, :as => Integer)
81
- assert_equal -1, option_value(%w/-i -1.1/, :i, true, :as => Integer)
82
- assert_equal "-1.1", option_value(%w/-i -1.1/, :i, true, :as => Float).to_s
83
- assert_equal "foo", option_value(%w/--foo1 foo/, :foo1, true)
84
-
85
- assert_equal 0, option_value(%w//, :v, :verbose, :as => :count)
86
- assert_equal 1, option_value(%w/--verbose/, :v, :verbose, :as => :count)
87
- assert_equal 2, option_value(%w/--verbose -v/, :v, :verbose, :as => :count)
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 })
38
+ test "call" do
39
+ foo = nil
40
+ option(:f, :callback => proc { foo = "bar" }).call
41
+ assert_equal "bar", foo
42
+ option(:f) { foo = "baz" }.call
43
+ assert_equal "baz", foo
44
+ option(:f) { |o| assert_equal 1, o }.call(1)
92
45
  end
93
46
 
94
- test 'casting should return nil for optionless arguments' do
95
- assert_equal nil, option_value(%w/-i/, :i, :as => :int)
96
- assert_equal nil, option_value(%w/-i/, :i, :as => :str)
97
- assert_equal nil, option_value(%w/-i/, :i, :as => :float)
98
- assert_equal nil, option_value(%w/-i/, :i, :as => :array)
99
- assert_equal nil, option_value(%w/-i/, :i, :as => :range)
47
+ # type casting
100
48
 
101
- assert_equal nil, option_value(%w/-i/, :i, :optional => true, :as => :range)
49
+ test "proc/custom type cast" do
50
+ assert_equal 1, option_value(%w'-f 1', :f=, :as => proc {|x| x.to_i })
51
+ assert_equal "oof", option_value(%w'-f foo', :f=, :as => proc {|x| x.reverse })
102
52
  end
103
53
 
104
- test 'ranges' do
105
- assert_equal (1..10), option_value(%w/-r 1..10/, :r, true, :as => Range)
106
- assert_equal (1..10), option_value(%w/-r 1-10/, :r, true, :as => Range)
107
- assert_equal (1..10), option_value(%w/-r 1,10/, :r, true, :as => Range)
108
- assert_equal (1...10), option_value(%w/-r 1...10/, :r, true, :as => Range)
109
- assert_equal (-1..10), option_value(%w/-r -1..10/, :r, true, :as => Range)
110
- assert_equal (1..-10), option_value(%w/-r 1..-10/, :r, true, :as => Range)
111
-
112
- # default back to the string unless a regex is successful
113
- # return value.to_i if the value is /\A\d+\z/
114
- # maybe this should raise if Slop#strict?
115
- assert_equal "1abc10", option_value(%w/-r 1abc10/, :r, true, :as => Range)
116
- assert_equal (1..1), option_value(%w/-r 1/, :r, true, :as => Range)
117
- assert_equal (2..2), option_value(%w/-r 2/, :r, true, :as => Range)
54
+ test "integer type cast" do
55
+ assert_equal 1, option_value(%w'-f 1', :f=, :as => Integer)
118
56
  end
119
57
 
120
- test 'printing options' do
121
- slop = Slop.new
122
- slop.opt :n, :name, 'Your name', true
123
- slop.opt :age, 'Your age', true
124
- slop.opt :V, 'Display the version'
125
-
126
- assert_equal " -n, --name Your name", slop.options[:name].to_s
127
- assert_equal " --age Your age", slop.options[:age].to_s
128
- assert_equal " -V, Display the version", slop.options[:V].to_s
58
+ test "symbol type cast" do
59
+ assert_equal :foo, option_value(%w'-f foo', :f=, :as => Symbol)
129
60
  end
130
61
 
131
- test 'falls back to default option' do
132
- slop = Slop.new
133
- slop.opt :foo, :optional => true, :default => 'lee'
134
- slop.parse %w/--foo/
135
- assert_equal 'lee', slop[:foo]
62
+ test "range type cast" do
63
+ assert_equal (1..10), option_value(%w/-r 1..10/, :r=, :as => Range)
64
+ assert_equal (1..10), option_value(%w/-r 1-10/, :r=, :as => Range)
65
+ assert_equal (1..10), option_value(%w/-r 1,10/, :r=, :as => Range)
66
+ assert_equal (1...10), option_value(%w/-r 1...10/, :r=, :as => Range)
67
+ assert_equal (-1..10), option_value(%w/-r -1..10/, :r=, :as => Range)
68
+ assert_equal (1..-10), option_value(%w/-r 1..-10/, :r=, :as => Range)
136
69
  end
137
70
 
138
- test 'key should default to long flag otherwise use short flag' do
139
- assert_equal 'foo', option(:f, :foo).key
140
- assert_equal 'b', option(:b).key
71
+ test "array type cast" do
72
+ assert_equal %w/lee john bill/, option_value(%w/-p lee,john,bill/, :p=, :as => Array)
73
+ assert_equal %w/lee john bill/, option_value(%w/-p lee:john:bill/, :p=, :as => Array, :delimiter => ':')
74
+ assert_equal %w/lee john,bill/, option_value(%w/-p lee,john,bill/, :p=, :as => Array, :limit => 2)
75
+ assert_equal %w/lee john:bill/, option_value(%w/-p lee:john:bill/, :p=, :as => Array, :limit => 2, :delimiter => ':')
141
76
  end
142
77
 
143
- test 'tail to append items to the options list when printing help' do
144
- slop = Slop.new
145
- slop.on :f, :foo, :tail => true
146
- slop.on :b, :bar
147
- assert slop.to_s.strip =~ /foo$/
78
+ test "adding custom types" do
79
+ opts = Slop.new
80
+ opt = opts.on :f=, :as => :reverse
81
+ opt.types[:reverse] = proc { |v| v.reverse }
82
+ opts.parse %w'-f bar'
83
+ assert_equal 'rab', opt.value
148
84
  end
149
85
 
150
- test 'do not print help for options with :help => false' do
151
- slop = Slop.new
152
- slop.on :f, :foo, :help => false
153
- refute slop.help.include?('foo')
154
- end
86
+ # end type casting tests
155
87
 
156
- test 'appends a help string with :help => "string"' do
157
- slop = Slop.new
158
- slop.on :n, :name, 'Your name', true, :help => '<YOUR NAME HERE>'
159
- assert_equal ' -n, --name <YOUR NAME HERE> Your name', slop.options[:name].to_s
88
+ test "using a default value as fallback" do
89
+ opts = Slop.new
90
+ opt = opts.on :f, :argument => :optional, :default => 'foo'
91
+ opts.parse %w'-f'
92
+ assert_equal 'foo', opts[:f]
160
93
  end
161
94
 
162
- test 'argument matching' do
95
+ test "printing options" do
163
96
  slop = Slop.new
164
- slop.on :f, :foo, true, :match => /^h/
165
-
166
- assert_raises(Slop::InvalidArgumentError, /world/) { slop.parse %w/--foo world/ }
167
- assert slop.parse %w/--foo hello/
168
- end
169
-
170
- test 'non-casting of nil options' do
171
- slop = Slop.new { on :f, :foo, true, :as => String }
172
- slop.parse []
173
-
174
- assert_equal nil, slop[:foo]
175
- refute_equal "", slop[:foo]
176
- end
177
-
178
- test 'counting options' do
179
- slop = Slop.new { on :v; on :x }
180
- slop.parse %w/-v -v -v -x/
181
- assert_equal 1, slop.options[:x].count
182
- assert_equal 3, slop.options[:v].count
183
- end
184
-
185
- test 'omit block execution with :unless option' do
186
- item = nil
187
- opts = Slop.new do
188
- on :foo
189
- on :bar, true, :unless => 'foo' do; item = "foo"; end
190
- end
191
- opts.parse %w/--foo --bar 1/
97
+ slop.opt :n, :name=, 'Your name'
98
+ slop.opt :age=, 'Your age'
99
+ slop.opt :V, 'Display the version'
192
100
 
193
- assert_equal "1", opts[:bar]
194
- refute item
101
+ assert_equal " -n, --name Your name", slop.fetch_option(:name).to_s
102
+ assert_equal " --age Your age", slop.fetch_option(:age).to_s
103
+ assert_equal " -V, Display the version", slop.fetch_option(:V).help
195
104
  end
196
105
 
197
- test 'raises MissingOptionError when an option is :required' do
198
- opts = Slop.new do
199
- on :foo, :required => true
200
- end
201
-
202
- assert_raises(Slop::MissingOptionError, /foo is required/) { opts.parse %w[ --bar ] }
203
- assert_raises(Slop::MissingOptionError, /foo is required/) { opts.parse %w[ foo ] }
106
+ test "overwriting the help text" do
107
+ slop = Slop.new
108
+ slop.on :foo, :help => ' -f, --foo SOMETHING FOOEY'
109
+ assert_equal ' -f, --foo SOMETHING FOOEY', slop.fetch_option(:foo).help
204
110
  end
205
- end
111
+ end
@@ -1,8 +1,11 @@
1
1
  require 'helper'
2
2
 
3
3
  class SlopTest < TestCase
4
- def clean_options(*args)
5
- Slop.new.send(:clean_options, args)
4
+
5
+ def build_option(*args)
6
+ opt = Slop.new.send(:build_option, args)
7
+ config = opt.config.reject { |k, v| v == Slop::Option::DEFAULT_OPTIONS[k] }
8
+ [opt.short, opt.long, opt.description, config]
6
9
  end
7
10
 
8
11
  def temp_argv(items)
@@ -13,590 +16,264 @@ class SlopTest < TestCase
13
16
  ARGV.replace old_argv
14
17
  end
15
18
 
16
- test 'includes Enumerable' do
17
- assert Slop.included_modules.include?(Enumerable)
18
- end
19
-
20
- test 'new accepts a hash or array of symbols' do
21
- slop = Slop.new :strict, :multiple_switches => true
22
- [ :@multiple_switches, :@strict ].each do |var|
23
- assert slop.instance_variable_get var
24
- end
25
- end
26
-
27
- test 'parse returns a Slop object' do
28
- slop = Slop.parse([])
29
- assert_kind_of Slop, slop
19
+ def temp_stderr
20
+ $stderr = StringIO.new
21
+ yield $stderr.string
22
+ ensure
23
+ $stderr = STDERR
30
24
  end
31
25
 
32
- test 'parsing calls to_s on all of the items in the array' do
33
- opts = Slop.parse([:'--foo']) { on :foo }
34
- assert opts.foo?
35
- end
26
+ test "build_option" do
27
+ assert_equal ['f', nil, nil, {}], build_option(:f)
28
+ assert_equal [nil, 'foo', nil, {}], build_option(:foo)
29
+ assert_equal ['f', nil, 'Some description', {}], build_option(:f, 'Some description')
30
+ assert_equal ['f', 'foo', nil, {}], build_option(:f, :foo)
36
31
 
37
- test '#opt returns an Slop::Option' do
38
- assert_kind_of Slop::Option, Slop.new.option(:n)
32
+ # with arguments
33
+ assert_equal ['f', nil, nil, {:argument=>true}], build_option('f=')
34
+ assert_equal [nil, 'foo', nil, {:argument=>true}], build_option('foo=')
35
+ assert_equal [nil, 'foo', nil, {:optional_argument=>true}], build_option('foo=?')
39
36
  end
40
37
 
41
- test 'enumerating options' do
38
+ test "fetch_option" do
42
39
  slop = Slop.new
43
- slop.opt(:f, :foo, 'foo')
44
- slop.opt(:b, :bar, 'bar')
40
+ opt1 = slop.on :f, :foo
41
+ opt2 = slop.on :bar
45
42
 
46
- slop.each { |option| assert_kind_of Slop::Option, option }
43
+ assert_equal opt1, slop.fetch_option(:foo)
44
+ assert_equal opt1, slop.fetch_option(:f)
45
+ assert_equal opt2, slop.fetch_option(:bar)
46
+ assert_equal opt2, slop.fetch_option('--bar')
47
+ assert_nil slop.fetch_option(:baz)
47
48
  end
48
49
 
49
- test 'defaulting to ARGV' do
50
- temp_argv(%w/--name lee/) do
51
- assert_equal('lee', Slop.parse { on :name, true }[:name])
52
- end
53
- end
50
+ test "default all options to take arguments" do
51
+ slop = Slop.new(:arguments => true)
52
+ opt1 = slop.on :foo
53
+ opt2 = slop.on :bar, :argument => false
54
54
 
55
- test 'callback when option array is empty' do
56
- item1 = nil
57
- temp_argv([]) do
58
- Slop.new { on_empty { item1 = 'foo' } }.parse
59
- end
60
-
61
- assert_equal 'foo', item1
62
-
63
- temp_argv([]) do
64
- assert_equal [], Slop.new { on_empty {} }.parse
65
- end
66
- end
67
-
68
- test 'callback when arguments contain no options' do
69
- item = nil
70
- Slop.new { on_optionless { item = 'foo' } }.parse %w/a b c/
71
- assert_equal 'foo', item
55
+ assert opt1.expects_argument?
56
+ refute opt2.expects_argument?
72
57
  end
73
58
 
74
- test 'multiple switches with the :multiple_switches flag' do
75
- slop = Slop.new :multiple_switches => true, :strict => true
76
- %w/a b c/.each { |f| slop.on f }
77
- slop.on :z, true
78
- slop.parse %w/-abc/
79
-
80
- %w/a b c/.each do |flag|
81
- assert slop[flag]
82
- assert slop.send(flag + '?')
83
- end
84
-
85
- assert_raises(Slop::InvalidOptionError, /d/) { slop.parse %w/-abcd/ }
86
- assert_raises(Slop::MissingArgumentError, /z/) { slop.parse %w/-abcz/ }
87
-
88
- slop = Slop.new(:multiple_switches)
89
- slop.on :a
90
- slop.on :f, true
91
- args = %w[-abc -f foo bar]
92
- slop.parse! args
93
-
94
- assert_equal %w[ bar ], args
95
- assert_equal 'foo', slop[:f]
96
- assert slop[:a]
97
- end
98
-
99
- test 'passing a block' do
100
- assert Slop.new {}
101
- slop = nil
102
- assert Slop.new {|s| slop = s }
103
- assert_kind_of Slop, slop
104
- end
105
-
106
- test 'automatically adding the help option' do
59
+ test "extract_option" do
107
60
  slop = Slop.new
108
- assert_empty slop.options
109
-
110
- slop = Slop.new :help => true
111
- refute_empty slop.options
112
- assert_equal 'Print this help message', slop.options[:help].description
113
- end
114
-
115
- test ':all_accept_arguments' do
116
- opts = Slop.new(:all_accept_arguments) do
117
- on :foo
118
- on :bar, :optional => true
119
- end
120
- opts.parse %w[ --foo hello --bar ]
121
-
122
- assert_equal 'hello', opts[:foo]
123
- assert_nil opts[:bar]
124
- assert_raises(Slop::MissingArgumentError) { opts.parse %w[ --foo --bar ] }
125
- end
61
+ extract = proc { |flag| slop.send(:extract_option, flag) }
62
+ slop.on :opt=
126
63
 
127
- test 'yielding non-options when a block is passed to "parse"' do
128
- opts = Slop.new do
129
- on :name, true
130
- end
131
- opts.parse(%w/--name lee a/) do |v|
132
- assert_equal 'a', v
133
- end
64
+ assert_kind_of Array, extract['--foo']
65
+ assert_equal 'bar', extract['--foo=bar'][1]
66
+ assert_equal 'bar', extract['-f=bar'][1]
67
+ assert_nil extract['--foo'][0]
68
+ assert_kind_of Slop::Option, extract['--opt'][0]
69
+ assert_equal false, extract['--no-opt'][1]
134
70
  end
135
71
 
136
- test 'preserving order when yielding non-options' do
137
- items = []
138
- slop = Slop.new { on(:name, true) { |name| items << name } }
139
- slop.parse(%w/foo --name bar baz/) { |value| items << value }
140
- assert_equal %w/foo bar baz/, items
141
- end
142
-
143
- test 'only parsing options' do
144
- slop = Slop.new { on :n, true }
145
- assert slop.parse %w/n/
146
- end
147
-
148
- test 'setting the banner' do
149
- slop = Slop.new
150
- slop.banner = "foo bar"
151
-
152
- assert_equal "foo bar", slop.banner
153
- assert slop.to_s =~ /^foo bar/
154
-
155
- slop.banner = nil
156
- assert_equal "", slop.to_s
157
-
158
- slop = Slop.new "foo bar"
159
- assert_equal "foo bar", slop.banner
160
-
161
- slop = Slop.new :banner => "foo bar"
162
- assert_equal "foo bar", slop.banner
163
- end
164
-
165
- test 'setting the summary' do
166
- slop = Slop.new
167
- slop.banner = "foo bar"
168
- slop.summary = "does stuff"
169
-
170
- assert_equal "foo bar\n\ndoes stuff", slop.to_s
171
- end
172
-
173
- test 'setting the description' do
72
+ test "non-options yielded to parse()" do
73
+ foo = nil
174
74
  slop = Slop.new
175
- slop.banner = "foo bar"
176
- slop.summary = "does stuff"
177
- slop.description = "This does stuff."
178
-
179
- assert_equal "foo bar\n\ndoes stuff\n\n This does stuff.", slop.to_s
75
+ slop.parse ['foo'] do |x| foo = x end
76
+ assert_equal 'foo', foo
180
77
  end
181
78
 
182
- test 'setting the description without matching summary' do
183
- slop = Slop.new
184
- slop.banner = "foo bar"
185
- slop.description = "This does stuff."
186
-
187
- assert_equal "foo bar\n\n This does stuff.", slop.to_s
79
+ test "::parse returns a Slop object" do
80
+ assert_kind_of Slop, Slop.parse([])
188
81
  end
189
82
 
190
- test 'storing long option lengths' do
83
+ test "parse" do
191
84
  slop = Slop.new
192
- assert_equal 0, slop.longest_flag
193
- slop.opt(:name)
194
- assert_equal 4, slop.longest_flag
195
- slop.opt(:username)
196
- assert_equal 8, slop.longest_flag
197
- end
198
-
199
- test 'parse returning the list of arguments left after parsing' do
200
- opts = Slop.new do
201
- on :name, true
202
- end
203
- assert_equal %w/a/, opts.parse!(%w/--name lee a/)
204
- assert_equal %w/--name lee a/, opts.parse(%w/--name lee a/)
205
- assert_equal ['foo', :bar, 1], opts.parse(['foo', :bar, 1])
206
- end
207
-
208
- test '#parse does not remove parsed items' do
209
- items = %w/--foo/
210
- Slop.new { |opt| opt.on :foo }.parse(items)
211
- assert_equal %w/--foo/, items
212
- end
213
-
214
- test '#parse! removes parsed items' do
215
- items = %w/--foo/
216
- Slop.new { |opt| opt.on :foo }.parse!(items)
217
- assert_empty items
218
- end
219
-
220
- test '#parse! does not remove unparsed items with same value as a parsed item' do
221
- items = %w/bar --foo bar/
222
- Slop.new { |opt| opt.on :foo, 'foo', true }.parse!(items)
223
- assert_equal %w/bar/, items
85
+ assert_equal ['foo'], slop.parse(%w'foo')
86
+ assert_equal ['foo'], slop.parse!(%w'foo')
224
87
  end
225
88
 
226
- test '#parse! removes parsed items prefixed with --no-' do
227
- items = %w/--no-foo/
228
- Slop.new { |opt| opt.on :foo }.parse!(items)
229
- assert_empty items
89
+ test "parse!" do
90
+ slop = Slop.new { on :foo= }
91
+ assert_equal [], slop.parse!(%w'--foo bar')
92
+ slop = Slop.new { on :baz }
93
+ assert_equal ['etc'], slop.parse!(%w'--baz etc')
230
94
  end
231
95
 
232
- test 'the shit out of clean_options' do
233
- assert_equal(
234
- ['s', 'short', 'short option', false, {}],
235
- clean_options('-s', '--short', 'short option')
236
- )
237
-
238
- assert_equal(
239
- [nil, 'long', 'long option only', true, {}],
240
- clean_options('--long', 'long option only', true)
241
- )
242
-
243
- assert_equal(
244
- ['S', 'symbol', 'symbolize', false, {}],
245
- clean_options(:S, :symbol, 'symbolize')
246
- )
247
-
248
- assert_equal(
249
- ['a', nil, 'alphabetical only', true, {}],
250
- clean_options('a', 'alphabetical only', true)
251
- )
252
-
253
- assert_equal( # for description-less options
254
- [nil, 'optiononly', nil, false, {}],
255
- clean_options('--optiononly')
256
- )
257
-
258
- assert_equal(
259
- ['f', 'foo', 'some description', false, {:optional => false, :help => 'BAR'}],
260
- clean_options(:f, 'foo BAR', 'some description')
261
- )
262
-
263
- assert_equal(
264
- [nil, 'bar', nil, false, {:optional => true, :help => '[STUFF]'}],
265
- clean_options('bar [STUFF]')
266
- )
267
-
268
- assert_equal([nil, 'foo', nil, false, {:as => Array}], clean_options(:foo, Array, false))
269
- assert_equal([nil, 'foo', nil, false, {:as => Array}], clean_options(Array, :foo, false))
270
-
271
- assert_equal(['c', nil, nil, true, {}], clean_options(:c, true))
272
- assert_equal(['c', nil, nil, false, {}], clean_options(:c, false))
96
+ test "new() accepts a hash of configuration options" do
97
+ slop = Slop.new(:foo => :bar)
98
+ assert_equal :bar, slop.config[:foo]
273
99
  end
274
100
 
275
- test '[] returns an options argument value or a command or nil (in that order)' do
276
- slop = Slop.new
277
- slop.opt :n, :name, true
278
- slop.opt :foo
279
- slop.command(:foo) { }
280
- slop.command(:bar) { }
281
- slop.parse %w/--name lee --foo/
282
-
283
- assert_equal 'lee', slop[:name]
284
- assert_equal 'lee', slop[:n]
285
-
286
- assert_equal true, slop[:foo]
287
- assert_kind_of Slop, slop[:bar]
288
-
289
- assert_nil slop[:baz]
290
- end
291
-
292
- test 'arguments ending ? test for option existance' do
293
- slop = Slop.new
294
- slop.opt :v, :verbose
295
- slop.opt :d, :debug
296
- slop.parse %w/--verbose/
297
-
298
- assert slop[:verbose]
299
- assert slop.verbose?
300
-
301
- refute slop[:debug]
302
- refute slop.debug?
303
- end
304
-
305
- test 'options are present' do
306
- opts = Slop.new do
307
- on :f, 'foo-bar'
308
- on :b, 'bar-baz'
309
- on :h, :optional => true
101
+ test "defaulting to ARGV" do
102
+ temp_argv(%w/--name lee/) do
103
+ opts = Slop.parse { on :name= }
104
+ assert_equal 'lee', opts[:name]
310
105
  end
311
- opts.parse %w/--foo-bar -h/
312
-
313
- assert opts.present?('foo-bar')
314
- refute opts.present?('bar-baz')
315
- refute opts.present?('foo-bar', 'bar-baz')
316
- assert opts.present?(:h)
317
106
  end
318
107
 
319
- test 'raises if an option expects an argument and none is given' do
320
- slop = Slop.new
321
- slop.opt :name, true
322
- slop.opt :age, :optional => true
323
-
324
- assert_raises(Slop::MissingArgumentError, /name/) { slop.parse %w/--name/ }
325
- assert slop.parse %w/--name 'foo'/
326
- end
327
-
328
- test 'returning a hash of options' do
329
- slop = Slop.new
330
- slop.opt :name, true
331
- slop.opt :version
332
- slop.opt :V, :verbose, :default => false
333
- slop.parse %w/--name lee --version/
334
-
335
- assert_equal({'name' => 'lee', 'version' => true, 'verbose' => false}, slop.to_hash(false))
336
- assert_equal({:name => 'lee', :version => true, :verbose => false}, slop.to_hash(true))
108
+ test "automatically adding the help option" do
109
+ slop = Slop.new :help => true
110
+ refute_empty slop.options
111
+ assert_equal 'Display this help message.', slop.options.first.description
337
112
  end
338
113
 
339
- test 'iterating options' do
340
- slop = Slop.new
341
- slop.opt :a, :abc
342
- slop.opt :f, :foo
114
+ test ":arguments and :optional_arguments config options" do
115
+ slop = Slop.new(:arguments => true) { on :foo }
116
+ assert slop.fetch_option(:foo).expects_argument?
343
117
 
344
- assert_equal 2, slop.count
345
- slop.each {|opt| assert_kind_of Slop::Option, opt }
118
+ slop = Slop.new(:optional_arguments => true) { on :foo }
119
+ assert slop.fetch_option(:foo).accepts_optional_argument?
346
120
  end
347
121
 
348
- test 'fetching options and option values' do
349
- slop = Slop.new
350
- slop.opt :foo, true
351
- slop.parse %w/--foo bar/
352
-
353
- assert_kind_of Slop::Option, slop.options[:foo]
354
- assert_equal "bar", slop[:foo]
355
- assert_equal "bar", slop['foo']
356
- assert_kind_of Slop::Option, slop.options[0]
357
- assert_nil slop.options['0']
122
+ test "yielding non-options when a block is passed to parse()" do
123
+ items = []
124
+ opts = Slop.new { on :name= }
125
+ opts.parse(%w/--name lee a b c/) { |v| items << v }
126
+ assert_equal ['a', 'b', 'c'], items
358
127
  end
359
128
 
360
- test 'printing help' do
361
- slop = Slop.new
362
- slop.banner = 'Usage: foo [options]'
363
- slop.parse
364
- assert slop.to_s =~ /^Usage: foo/
129
+ test "on empty callback" do
130
+ opts = Slop.new
131
+ foo = nil
132
+ opts.add_callback(:empty) { foo = "bar" }
133
+ opts.parse []
134
+ assert_equal "bar", foo
365
135
  end
366
136
 
367
- test 'passing argument values to blocks' do
368
- name = nil
137
+ test "on no_options callback" do
369
138
  opts = Slop.new
370
- opts.on :name, true, :callback => proc {|n| name = n}
371
- opts.parse %w/--name lee/
372
- assert_equal 'lee', name
139
+ foo = nil
140
+ opts.add_callback(:no_options) { foo = "bar" }
141
+ opts.parse %w( --foo --bar etc hello )
142
+ assert_equal "bar", foo
373
143
  end
374
144
 
375
- test 'strict mode' do
376
- strict = Slop.new :strict => true
377
- totallynotstrict = Slop.new
378
-
379
- assert_raises(Slop::InvalidOptionError, /--foo/) { strict.parse %w/--foo/ }
380
- assert totallynotstrict.parse %w/--foo/
145
+ test "to_hash()" do
146
+ opts = Slop.new { on :foo=; on :bar }
147
+ opts.parse(%w'--foo hello --bar')
148
+ assert_equal({ :foo => 'hello', :bar => nil }, opts.to_hash)
381
149
  end
382
150
 
383
- test 'strict mode parses options before raising Slop::InvalidOptionError' do
384
- strict = Slop.new :strict => true
385
- strict.opt :n, :name, true
386
-
387
- assert_raises(Slop::InvalidOptionError, /--foo/) { strict.parse %w/--foo --name nelson/ }
388
- assert_equal 'nelson', strict[:name]
151
+ test "missing() returning all missing option keys" do
152
+ opts = Slop.new { on :foo; on :bar }
153
+ opts.parse %w'--foo'
154
+ assert_equal ['bar'], opts.missing
389
155
  end
390
156
 
391
- test 'short option flag with no space between flag and argument, with :multiple_switches => false' do
392
- slop = Slop.new :multiple_switches => false
393
- slop.opt :p, :password, true
394
- slop.opt :s, :shortpass, true
395
- slop.parse %w/-pfoo -sbar/
396
-
397
- assert_equal 'foo', slop[:password]
398
- assert_equal 'bar', slop[:shortpass]
157
+ test "autocreating options" do
158
+ opts = Slop.new :autocreate => true
159
+ opts.parse %w[ --foo bar --baz ]
160
+ assert opts.fetch_option(:foo).expects_argument?
161
+ assert opts.fetch_option(:foo).autocreated?
162
+ assert_equal 'bar', opts.fetch_option(:foo).value
163
+ refute opts.fetch_option(:baz).expects_argument?
399
164
  end
400
165
 
401
- test 'prefixing --no- onto options for a negative result' do
402
- slop = Slop.new
403
- slop.opt :d, :debug
404
- slop.opt :v, :verbose, :default => true
405
- slop.parse %w/--no-debug --no-verbose --no-nothing/
406
-
407
- refute slop.verbose?
408
- refute slop.debug?
409
- refute slop[:verbose]
410
- refute slop[:debug]
166
+ test "raising an InvalidArgumentError when the argument doesn't match" do
167
+ opts = Slop.new { on :foo=, :match => /^[a-z]+$/ }
168
+ assert_raises(Slop::InvalidArgumentError) { opts.parse %w' --foo b4r '}
411
169
  end
412
170
 
413
- test 'option=value' do
414
- slop = Slop.new
415
- slop.opt :n, :name, true
416
- slop.parse %w/--name=lee/
417
-
418
- assert_equal 'lee', slop[:name]
419
- assert slop.name?
171
+ test "raising a MissingArgumentError when the option expects an argument" do
172
+ opts = Slop.new { on :foo= }
173
+ assert_raises(Slop::MissingArgumentError) { opts.parse %w' --foo '}
420
174
  end
421
175
 
422
- test 'parsing options with options as arguments' do
423
- slop = Slop.new { on :f, :foo, true }
424
- assert_raises(Slop::MissingArgumentError) { slop.parse %w/-f --bar/ }
176
+ test "raising a MissingOptionError when a required option is missing" do
177
+ opts = Slop.new { on :foo, :required => true }
178
+ assert_raises(Slop::MissingOptionError) { opts.parse %w'' }
425
179
  end
426
180
 
427
- test 'respond_to?' do
428
- slop = Slop.new { on :f, :foo }
429
- assert slop.respond_to?('foo?')
430
- refute slop.respond_to?('foo')
181
+ test "raising InvalidOptionError when strict mode is enabled and an unknown option appears" do
182
+ opts = Slop.new :strict => true
183
+ assert_raises(Slop::InvalidOptionError) { opts.parse %w'--foo' }
184
+ assert_raises(Slop::InvalidOptionError) { opts.parse %w'-fabc' }
431
185
  end
432
186
 
433
- test 'reusable slop object (ie not using define_method for present?())' do
434
- slop = Slop.new { on :f, :foo }
435
-
436
- slop.parse %w()
437
- assert_equal false, slop.foo?
438
-
439
- slop.parse %w( --foo )
440
- assert_equal true, slop.foo?
187
+ test "multiple_switches is enabled by default" do
188
+ opts = Slop.new { on :f; on :b }
189
+ opts.parse %w[ -fb ]
190
+ assert opts.present?(:f)
191
+ assert opts.present?(:b)
441
192
  end
442
193
 
443
- test 'custom IO object' do
444
- io = StringIO.new
445
- slop = Slop.new(:help => true, :io => io)
446
- slop.on :f, :foo, 'something fooey'
447
- begin
448
- slop.parse %w/--help/
449
- rescue SystemExit
450
- end
451
- assert io.string.include? 'something fooey'
194
+ test "multiple_switches disabled" do
195
+ opts = Slop.new(:multiple_switches => false) { on :f= }
196
+ opts.parse %w[ -fabc123 ]
197
+ assert_equal 'abc123', opts[:f]
452
198
  end
453
199
 
454
- test 'exiting when using :help option' do
455
- io = StringIO.new
456
- opts = Slop.new(:help => true, :io => io)
457
- assert_raises(SystemExit) { opts.parse %w/--help/ }
200
+ test "setting/getting the banner" do
201
+ opts = Slop.new :banner => 'foo'
202
+ assert_equal 'foo', opts.banner
458
203
 
459
- opts = Slop.new(:help => true, :io => io, :exit_on_help => false)
460
- assert opts.parse %w/--help/
461
- end
204
+ opts = Slop.new
205
+ opts.banner 'foo'
206
+ assert_equal 'foo', opts.banner
462
207
 
463
- test 'ignoring case' do
464
- opts = Slop.new(:ignore_case => true)
465
- opts.on :n, :name, true
466
- opts.parse %w/--NAME lee/
467
- assert_equal 'lee', opts[:name]
208
+ opts = Slop.new
209
+ opts.banner = 'foo'
210
+ assert_equal 'foo', opts.banner
468
211
  end
469
212
 
470
- test 'autocreating options' do
471
- opts = Slop.new(:autocreate => true) do |o|
472
- o.on '--lorem', true
473
- end
474
- opts.parse %w/--hello --foo bar -a --lorem ipsum/
475
-
476
- assert opts.hello?
477
- assert opts.foo?
478
- assert_equal 'bar', opts[:foo]
479
- assert opts.a?
480
- assert_equal 'ipsum', opts[:lorem]
213
+ test "get/[] fetching an options argument value" do
214
+ opts = Slop.new { on :foo=; on :bar; on :baz }
215
+ opts.parse %w' --foo hello --bar '
216
+ assert_equal 'hello', opts[:foo]
217
+ assert_nil opts[:bar]
218
+ assert_nil opts[:baz]
481
219
  end
482
220
 
483
- test 'multiple elements for array option' do
484
- opts = Slop.new do
485
- on :a, true, :as => Array
486
- end
487
- opts.parse %w/-a foo -a bar baz -a etc/
488
-
489
- assert_equal %w/foo bar etc/, opts[:a]
221
+ test "checking for an options presence" do
222
+ opts = Slop.new { on :foo; on :bar }
223
+ opts.parse %w' --foo '
224
+ assert opts.present?(:foo)
225
+ refute opts.present?(:bar)
490
226
  end
491
227
 
492
- test ':arguments => true' do
493
- opts = Slop.new(:arguments) { on :foo }
494
- opts.parse %w/--foo bar/
228
+ test "ignoring case" do
229
+ opts = Slop.new { on :foo }
230
+ opts.parse %w' --FOO bar '
231
+ assert_nil opts[:foo]
495
232
 
233
+ opts = Slop.new(:ignore_case => true) { on :foo= }
234
+ opts.parse %w' --FOO bar '
496
235
  assert_equal 'bar', opts[:foo]
497
236
  end
498
237
 
499
- test 'long flag strings' do
500
- opts = Slop.new do
501
- on 'f', 'foo BAR'
502
- on 'bar [HELLO]'
503
- end
504
-
505
- assert opts.options[:foo].expects_argument?
506
- assert opts.options[:bar].accepts_optional_argument?
507
-
508
- assert_equal ' -f, --foo BAR ', opts.options[:foo].to_s
509
- assert_equal ' --bar [HELLO] ', opts.options[:bar].to_s
510
- end
511
-
512
- test 'not parsing options if after --' do
513
- args = %w[ foo bar -- --foo bar ]
514
- opts = Slop.parse!(args) do
515
- on :foo, true
516
- end
517
-
518
- assert_equal %w[ foo bar --foo bar ], args
519
- end
520
-
521
- test 'inline classes' do
522
- opts = Slop.new do
523
- on :foo, Array, true
524
- on Symbol, :bar, true
525
- end
526
- opts.parse %w/--foo one,two --bar hello/
527
-
528
- assert_equal %w[one two], opts[:foo]
529
- assert_equal :hello, opts[:bar]
530
- end
531
-
532
- test 'wrap and indent' do
533
- slop = Slop.new
534
-
535
- assert_equal(
536
- "Lorem ipsum dolor sit amet, consectetur\n" +
537
- "adipisicing elit, sed do eiusmod tempor\n" +
538
- "incididunt ut labore et dolore magna\n" +
539
- "aliqua.",
540
- slop.send(:wrap_and_indent, "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", 40, 0))
541
-
542
- assert_equal(
543
- " Lorem ipsum dolor sit amet,\n" +
544
- " consectetur adipisicing elit, sed\n" +
545
- " do eiusmod tempor incididunt ut\n" +
546
- " labore et dolore magna aliqua.",
547
- slop.send(:wrap_and_indent, "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", 36, 4))
548
- end
549
-
550
- test 'to_struct' do
551
- assert_nil Slop.new.to_struct
552
-
553
- slop = Slop.new { on :a, true }
554
- slop.parse %w[ -a foo -b ]
555
- struct = slop.to_struct
556
-
557
- assert_equal 'foo', struct.a
558
- assert_kind_of Struct, struct
559
- assert_raises(NoMethodError) { struct.b }
560
-
561
- pstruct = slop.to_struct('Foo')
562
- assert_kind_of Struct::Foo, pstruct
563
- end
564
-
565
- test 'returning missing options' do
566
- slop = Slop.new { on :a; on :b, :bar; on :c; }
567
- slop.parse %w[ -a ]
568
-
569
- assert_equal %w[ bar c ], slop.missing
570
- end
571
-
572
- test 'parsing an optspec and building options' do
238
+ test "parsing an optspec and building options" do
573
239
  optspec = <<-SPEC
574
240
  ruby foo.rb [options]
575
241
  --
576
242
  v,verbose enable verbose mode
577
243
  q,quiet enable quiet mode
578
- debug enable debug mode
579
- H enable hax mode (srsly)
580
244
  n,name= set your name
581
- -a,--age= set your age
245
+ p,pass=? set your password
582
246
  SPEC
583
247
  opts = Slop.optspec(optspec.gsub(/^\s+/, ''))
584
248
  opts.parse %w[ --verbose --name Lee ]
585
249
 
586
250
  assert_equal 'Lee', opts[:name]
587
- assert opts.verbose?
588
- assert_equal 'enable quiet mode', opts.options[:quiet].description
251
+ assert opts.present?(:verbose)
252
+ assert_equal 'enable quiet mode', opts.fetch_option(:quiet).description
253
+ assert opts.fetch_option(:pass).accepts_optional_argument?
589
254
  end
590
255
 
591
- test "negative integers should not be processed as options and removed" do
256
+ test "ensure negative integers are not processed as options" do
592
257
  items = %w(-1)
593
258
  Slop.parse!(items)
594
259
  assert_equal %w(-1), items
595
260
  end
596
261
 
597
- test "options taking arguments should ignore argument that look like options (wut?)" do
598
- opts = Slop.new { on :v; on :foo, :optional => true, :default => 5, :as => Integer }
599
- opts.parse %w[ -c -v ]
600
- assert_equal 5, opts[:foo]
262
+ test "separators" do
263
+ opts = Slop.new do
264
+ on :foo
265
+ separator "hello"
266
+ on :bar
267
+ end
268
+ assert_equal " --foo \nhello\n --bar ", opts.help
269
+ end
270
+
271
+ test "printing help with :help => true" do
272
+ temp_stderr do |string|
273
+ opts = Slop.new(:help => true)
274
+ opts.parse %w( --help )
275
+ assert_equal " -h, --help Display this help message.\n", string
276
+ end
601
277
  end
602
- end
278
+
279
+ end