slop 3.6.0 → 4.0.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -4
- data/LICENSE +1 -1
- data/README.md +179 -131
- data/lib/slop.rb +34 -665
- data/lib/slop/error.rb +20 -0
- data/lib/slop/option.rb +84 -181
- data/lib/slop/options.rb +141 -0
- data/lib/slop/parser.rb +111 -0
- data/lib/slop/result.rb +79 -0
- data/lib/slop/types.rb +52 -0
- data/slop.gemspec +6 -3
- data/test/error_test.rb +31 -0
- data/test/option_test.rb +16 -137
- data/test/options_test.rb +79 -0
- data/test/parser_test.rb +65 -0
- data/test/result_test.rb +85 -0
- data/test/test_helper.rb +6 -0
- data/test/types_test.rb +78 -0
- metadata +30 -21
- data/CHANGES.md +0 -309
- data/lib/slop/commands.rb +0 -196
- data/test/commands_test.rb +0 -26
- data/test/helper.rb +0 -12
- data/test/slop_test.rb +0 -518
data/lib/slop/commands.rb
DELETED
@@ -1,196 +0,0 @@
|
|
1
|
-
class Slop
|
2
|
-
class Commands
|
3
|
-
include Enumerable
|
4
|
-
|
5
|
-
attr_reader :config, :commands, :arguments
|
6
|
-
attr_writer :banner
|
7
|
-
|
8
|
-
# Create a new instance of Slop::Commands and optionally build
|
9
|
-
# Slop instances via a block. Any configuration options used in
|
10
|
-
# this method will be the default configuration options sent to
|
11
|
-
# each Slop object created.
|
12
|
-
#
|
13
|
-
# config - An optional configuration Hash.
|
14
|
-
# block - Optional block used to define commands.
|
15
|
-
#
|
16
|
-
# Examples:
|
17
|
-
#
|
18
|
-
# commands = Slop::Commands.new do
|
19
|
-
# on :new do
|
20
|
-
# on '-o', '--outdir=', 'The output directory'
|
21
|
-
# on '-v', '--verbose', 'Enable verbose mode'
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# on :generate do
|
25
|
-
# on '--assets', 'Generate assets', :default => true
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
# global do
|
29
|
-
# on '-D', '--debug', 'Enable debug mode', :default => false
|
30
|
-
# end
|
31
|
-
# end
|
32
|
-
#
|
33
|
-
# commands[:new].class #=> Slop
|
34
|
-
# commands.parse
|
35
|
-
#
|
36
|
-
def initialize(config = {}, &block)
|
37
|
-
@config = config
|
38
|
-
@commands = {}
|
39
|
-
@banner = nil
|
40
|
-
@triggered_command = nil
|
41
|
-
|
42
|
-
warn "[DEPRECATED] Slop::Commands is deprecated and will be removed in "\
|
43
|
-
"Slop version 4. Check out http://leejarvis.github.io/slop/#commands for "\
|
44
|
-
"a new implementation of commands."
|
45
|
-
|
46
|
-
if block_given?
|
47
|
-
block.arity == 1 ? yield(self) : instance_eval(&block)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
# Optionally set the banner for this command help output.
|
52
|
-
#
|
53
|
-
# banner - The String text to set the banner.
|
54
|
-
#
|
55
|
-
# Returns the String banner if one is set.
|
56
|
-
def banner(banner = nil)
|
57
|
-
@banner = banner if banner
|
58
|
-
@banner
|
59
|
-
end
|
60
|
-
|
61
|
-
# Add a Slop instance for a specific command.
|
62
|
-
#
|
63
|
-
# command - A String or Symbol key used to identify this command.
|
64
|
-
# config - A Hash of configuration options to pass to Slop.
|
65
|
-
# block - An optional block used to pass options to Slop.
|
66
|
-
#
|
67
|
-
# Returns the newly created Slop instance mapped to command.
|
68
|
-
def on(command, config = {}, &block)
|
69
|
-
commands[command.to_s] = Slop.new(@config.merge(config), &block)
|
70
|
-
end
|
71
|
-
|
72
|
-
# Add a Slop instance used when no other commands exist.
|
73
|
-
#
|
74
|
-
# config - A Hash of configuration options to pass to Slop.
|
75
|
-
# block - An optional block used to pass options to Slop.
|
76
|
-
#
|
77
|
-
# Returns the newly created Slop instance mapped to default.
|
78
|
-
def default(config = {}, &block)
|
79
|
-
on('default', config, &block)
|
80
|
-
end
|
81
|
-
|
82
|
-
# Add a global Slop instance.
|
83
|
-
#
|
84
|
-
# config - A Hash of configuration options to pass to Slop.
|
85
|
-
# block - An optional block used to pass options to Slop.
|
86
|
-
#
|
87
|
-
# Returns the newly created Slop instance mapped to global.
|
88
|
-
def global(config = {}, &block)
|
89
|
-
on('global', config, &block)
|
90
|
-
end
|
91
|
-
|
92
|
-
# Fetch the instance of Slop tied to a command.
|
93
|
-
#
|
94
|
-
# key - The String or Symbol key used to locate this command.
|
95
|
-
#
|
96
|
-
# Returns the Slop instance if this key is found, nil otherwise.
|
97
|
-
def [](key)
|
98
|
-
commands[key.to_s]
|
99
|
-
end
|
100
|
-
alias get []
|
101
|
-
|
102
|
-
# Check for a command presence.
|
103
|
-
#
|
104
|
-
# Examples:
|
105
|
-
#
|
106
|
-
# cmds.parse %w( foo )
|
107
|
-
# cmds.present?(:foo) #=> true
|
108
|
-
# cmds.present?(:bar) #=> false
|
109
|
-
#
|
110
|
-
# Returns true if the given key is present in the parsed arguments.
|
111
|
-
def present?(key)
|
112
|
-
key.to_s == @triggered_command
|
113
|
-
end
|
114
|
-
|
115
|
-
# Enumerable interface.
|
116
|
-
def each(&block)
|
117
|
-
@commands.each(&block)
|
118
|
-
end
|
119
|
-
|
120
|
-
# Parse a list of items.
|
121
|
-
#
|
122
|
-
# items - The Array of items to parse.
|
123
|
-
#
|
124
|
-
# Returns the original Array of items.
|
125
|
-
def parse(items = ARGV)
|
126
|
-
parse! items.dup
|
127
|
-
items
|
128
|
-
end
|
129
|
-
|
130
|
-
# Parse a list of items, removing any options or option arguments found.
|
131
|
-
#
|
132
|
-
# items - The Array of items to parse.
|
133
|
-
#
|
134
|
-
# Returns the original Array of items with options removed.
|
135
|
-
def parse!(items = ARGV)
|
136
|
-
if opts = commands[items[0].to_s]
|
137
|
-
@triggered_command = items.shift
|
138
|
-
execute_arguments! items
|
139
|
-
opts.parse! items
|
140
|
-
execute_global_opts! items
|
141
|
-
else
|
142
|
-
if opts = commands['default']
|
143
|
-
opts.parse! items
|
144
|
-
else
|
145
|
-
if config[:strict] && items[0]
|
146
|
-
raise InvalidCommandError, "Unknown command `#{items[0]}`"
|
147
|
-
end
|
148
|
-
end
|
149
|
-
execute_global_opts! items
|
150
|
-
end
|
151
|
-
items
|
152
|
-
end
|
153
|
-
|
154
|
-
# Returns a nested Hash with Slop options and values. See Slop#to_hash.
|
155
|
-
def to_hash
|
156
|
-
Hash[commands.map { |k, v| [k.to_sym, v.to_hash] }]
|
157
|
-
end
|
158
|
-
|
159
|
-
# Returns the help String.
|
160
|
-
def to_s
|
161
|
-
defaults = commands.delete('default')
|
162
|
-
globals = commands.delete('global')
|
163
|
-
helps = commands.reject { |_, v| v.options.none? }
|
164
|
-
if globals && globals.options.any?
|
165
|
-
helps.merge!('Global options' => globals.to_s)
|
166
|
-
end
|
167
|
-
if defaults && defaults.options.any?
|
168
|
-
helps.merge!('Other options' => defaults.to_s)
|
169
|
-
end
|
170
|
-
banner = @banner ? "#{@banner}\n" : ""
|
171
|
-
banner + helps.map { |key, opts| " #{key}\n#{opts}" }.join("\n\n")
|
172
|
-
end
|
173
|
-
alias help to_s
|
174
|
-
|
175
|
-
# Returns the inspection String.
|
176
|
-
def inspect
|
177
|
-
"#<Slop::Commands #{config.inspect} #{commands.values.map(&:inspect)}>"
|
178
|
-
end
|
179
|
-
|
180
|
-
private
|
181
|
-
|
182
|
-
# Returns nothing.
|
183
|
-
def execute_arguments!(items)
|
184
|
-
@arguments = items.take_while { |arg| !arg.start_with?('-') }
|
185
|
-
items.shift @arguments.size
|
186
|
-
end
|
187
|
-
|
188
|
-
# Returns nothing.
|
189
|
-
def execute_global_opts!(items)
|
190
|
-
if global_opts = commands['global']
|
191
|
-
global_opts.parse! items
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
end
|
196
|
-
end
|
data/test/commands_test.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class CommandsTest < TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@opts = Slop.new do |o|
|
7
|
-
o.on :v, :version
|
8
|
-
o.command :add do |add|
|
9
|
-
add.on :v, 'verbose mode'
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
test "parse! removes the command AND its options" do
|
15
|
-
items = %w'add -v'
|
16
|
-
@opts.parse! items
|
17
|
-
assert_equal [], items
|
18
|
-
end
|
19
|
-
|
20
|
-
test "parse does not remove the command or its options" do
|
21
|
-
items = %w'add -v'
|
22
|
-
@opts.parse items
|
23
|
-
assert_equal ['add', '-v'], items
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
data/test/helper.rb
DELETED
data/test/slop_test.rb
DELETED
@@ -1,518 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class SlopTest < TestCase
|
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]
|
9
|
-
end
|
10
|
-
|
11
|
-
def temp_argv(items)
|
12
|
-
old_argv = ARGV.clone
|
13
|
-
ARGV.replace items
|
14
|
-
yield
|
15
|
-
ensure
|
16
|
-
ARGV.replace old_argv
|
17
|
-
end
|
18
|
-
|
19
|
-
def temp_stdout
|
20
|
-
$stdout = StringIO.new
|
21
|
-
yield $stdout.string
|
22
|
-
ensure
|
23
|
-
$stdout = STDOUT
|
24
|
-
end
|
25
|
-
|
26
|
-
test "includes Enumerable" do
|
27
|
-
assert_includes Slop.included_modules, Enumerable
|
28
|
-
end
|
29
|
-
|
30
|
-
test "enumerates Slop::Option objects in #each" do
|
31
|
-
Slop.new { on :f; on :b; }.each { |o| assert_kind_of Slop::Option, o }
|
32
|
-
end
|
33
|
-
|
34
|
-
test "build_option" do
|
35
|
-
assert_equal ['f', nil, nil, {}], build_option(:f)
|
36
|
-
assert_equal [nil, 'foo', nil, {}], build_option(:foo)
|
37
|
-
assert_equal ['f', nil, 'Some description', {}], build_option(:f, 'Some description')
|
38
|
-
assert_equal ['f', 'foo', nil, {}], build_option(:f, :foo)
|
39
|
-
assert_equal [nil, '1.8', 'Use v. 1.8', {}], build_option('--1.8', 'Use v. 1.8')
|
40
|
-
|
41
|
-
# with arguments
|
42
|
-
assert_equal ['f', nil, nil, {:argument=>true}], build_option('f=')
|
43
|
-
assert_equal [nil, 'foo', nil, {:argument=>true}], build_option('foo=')
|
44
|
-
assert_equal [nil, 'foo', nil, {:optional_argument=>true}], build_option('foo=?')
|
45
|
-
end
|
46
|
-
|
47
|
-
test "parsing option=value" do
|
48
|
-
slop = Slop.new { on :foo= }
|
49
|
-
slop.parse %w' --foo=bar '
|
50
|
-
assert_equal 'bar', slop[:foo]
|
51
|
-
|
52
|
-
slop = Slop.new(:multiple_switches => false) { on :f=; on :b= }
|
53
|
-
slop.parse %w' -fabc -bdef '
|
54
|
-
assert_equal 'abc', slop[:f]
|
55
|
-
assert_equal 'def', slop[:b]
|
56
|
-
end
|
57
|
-
|
58
|
-
test "fetch_option" do
|
59
|
-
slop = Slop.new
|
60
|
-
opt1 = slop.on :f, :foo
|
61
|
-
opt2 = slop.on :bar
|
62
|
-
|
63
|
-
assert_equal opt1, slop.fetch_option(:foo)
|
64
|
-
assert_equal opt1, slop.fetch_option(:f)
|
65
|
-
assert_equal opt2, slop.fetch_option(:bar)
|
66
|
-
assert_equal opt2, slop.fetch_option('--bar')
|
67
|
-
assert_nil slop.fetch_option(:baz)
|
68
|
-
end
|
69
|
-
|
70
|
-
test "default all options to take arguments" do
|
71
|
-
slop = Slop.new(:arguments => true)
|
72
|
-
opt1 = slop.on :foo
|
73
|
-
opt2 = slop.on :bar, :argument => false
|
74
|
-
|
75
|
-
assert opt1.expects_argument?
|
76
|
-
refute opt2.expects_argument?
|
77
|
-
end
|
78
|
-
|
79
|
-
test "extract_option" do
|
80
|
-
slop = Slop.new
|
81
|
-
extract = proc { |flag| slop.send(:extract_option, flag) }
|
82
|
-
slop.on :opt=
|
83
|
-
|
84
|
-
assert_kind_of Array, extract['--foo']
|
85
|
-
assert_equal 'bar', extract['--foo=bar'][1]
|
86
|
-
assert_equal 'bar', extract['-f=bar'][1]
|
87
|
-
assert_nil extract['--foo'][0]
|
88
|
-
assert_kind_of Slop::Option, extract['--opt'][0]
|
89
|
-
assert_equal false, extract['--no-opt'][1]
|
90
|
-
end
|
91
|
-
|
92
|
-
test "non-options yielded to parse()" do
|
93
|
-
foo = nil
|
94
|
-
slop = Slop.new
|
95
|
-
slop.parse ['foo'] do |x| foo = x end
|
96
|
-
assert_equal 'foo', foo
|
97
|
-
end
|
98
|
-
|
99
|
-
test "::parse returns a Slop object" do
|
100
|
-
assert_kind_of Slop, Slop.parse([])
|
101
|
-
end
|
102
|
-
|
103
|
-
test "parse" do
|
104
|
-
slop = Slop.new
|
105
|
-
assert_equal ['foo'], slop.parse(%w'foo')
|
106
|
-
assert_equal ['foo'], slop.parse!(%w'foo')
|
107
|
-
end
|
108
|
-
|
109
|
-
test "parse!" do
|
110
|
-
slop = Slop.new { on :foo= }
|
111
|
-
assert_equal [], slop.parse!(%w'--foo bar')
|
112
|
-
slop = Slop.new { on :baz }
|
113
|
-
assert_equal ['etc'], slop.parse!(%w'--baz etc')
|
114
|
-
end
|
115
|
-
|
116
|
-
test "new() accepts a hash of configuration options" do
|
117
|
-
slop = Slop.new(:foo => :bar)
|
118
|
-
assert_equal :bar, slop.config[:foo]
|
119
|
-
end
|
120
|
-
|
121
|
-
test "defaulting to ARGV" do
|
122
|
-
temp_argv(%w/--name lee/) do
|
123
|
-
opts = Slop.parse { on :name= }
|
124
|
-
assert_equal 'lee', opts[:name]
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
test "automatically adding the help option" do
|
129
|
-
slop = Slop.new :help => true
|
130
|
-
refute_empty slop.options
|
131
|
-
assert_equal 'Display this help message.', slop.options.first.description
|
132
|
-
end
|
133
|
-
|
134
|
-
test "default help exits" do
|
135
|
-
temp_stdout do
|
136
|
-
slop = Slop.new :help => true
|
137
|
-
assert_raises SystemExit do
|
138
|
-
slop.parse %w/--help/
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
test ":arguments and :optional_arguments config options" do
|
144
|
-
slop = Slop.new(:arguments => true) { on :foo }
|
145
|
-
assert slop.fetch_option(:foo).expects_argument?
|
146
|
-
|
147
|
-
slop = Slop.new(:optional_arguments => true) { on :foo }
|
148
|
-
assert slop.fetch_option(:foo).accepts_optional_argument?
|
149
|
-
end
|
150
|
-
|
151
|
-
test "yielding non-options when a block is passed to parse()" do
|
152
|
-
items = []
|
153
|
-
opts = Slop.new { on :name= }
|
154
|
-
opts.parse(%w/--name lee a b c/) { |v| items << v }
|
155
|
-
assert_equal ['a', 'b', 'c'], items
|
156
|
-
end
|
157
|
-
|
158
|
-
test "on empty callback" do
|
159
|
-
opts = Slop.new
|
160
|
-
foo = nil
|
161
|
-
opts.add_callback(:empty) { foo = "bar" }
|
162
|
-
opts.parse []
|
163
|
-
assert_equal "bar", foo
|
164
|
-
end
|
165
|
-
|
166
|
-
test "on no_options callback" do
|
167
|
-
opts = Slop.new
|
168
|
-
foo = nil
|
169
|
-
opts.add_callback(:no_options) { foo = "bar" }
|
170
|
-
opts.parse %w( --foo --bar etc hello )
|
171
|
-
assert_equal "bar", foo
|
172
|
-
end
|
173
|
-
|
174
|
-
test "to_hash()" do
|
175
|
-
opts = Slop.new { on :foo=; on :bar; on :baz; on :zip }
|
176
|
-
opts.parse(%w'--foo hello --no-bar --baz')
|
177
|
-
assert_equal({ :foo => 'hello', :bar => false, :baz => true, :zip => nil }, opts.to_hash)
|
178
|
-
end
|
179
|
-
|
180
|
-
test "missing() returning all missing option keys" do
|
181
|
-
opts = Slop.new { on :foo; on :bar }
|
182
|
-
opts.parse %w'--foo'
|
183
|
-
assert_equal ['bar'], opts.missing
|
184
|
-
end
|
185
|
-
|
186
|
-
test "autocreating options" do
|
187
|
-
opts = Slop.new :autocreate => true
|
188
|
-
opts.parse %w[ --foo bar --baz ]
|
189
|
-
assert opts.fetch_option(:foo).expects_argument?
|
190
|
-
assert opts.fetch_option(:foo).autocreated?
|
191
|
-
assert_equal 'bar', opts.fetch_option(:foo).value
|
192
|
-
refute opts.fetch_option(:baz).expects_argument?
|
193
|
-
assert_equal nil, opts.fetch_option(:bar)
|
194
|
-
|
195
|
-
opts = Slop.new :autocreate => true do
|
196
|
-
on :f, :foo=
|
197
|
-
end
|
198
|
-
opts.parse %w[ --foo bar --baz stuff ]
|
199
|
-
assert_equal 'bar', opts[:foo]
|
200
|
-
assert_equal 'stuff', opts[:baz]
|
201
|
-
end
|
202
|
-
|
203
|
-
test "option terminator" do
|
204
|
-
opts = Slop.new { on :foo= }
|
205
|
-
items = %w' foo -- --foo bar '
|
206
|
-
opts.parse! items
|
207
|
-
assert_equal %w' foo --foo bar ', items
|
208
|
-
end
|
209
|
-
|
210
|
-
test "raising an InvalidArgumentError when the argument doesn't match" do
|
211
|
-
opts = Slop.new { on :foo=, :match => /^[a-z]+$/ }
|
212
|
-
assert_raises(Slop::InvalidArgumentError) { opts.parse %w' --foo b4r '}
|
213
|
-
end
|
214
|
-
|
215
|
-
test "raising a MissingArgumentError when the option expects an argument" do
|
216
|
-
opts = Slop.new { on :foo= }
|
217
|
-
assert_raises(Slop::MissingArgumentError) { opts.parse %w' --foo '}
|
218
|
-
end
|
219
|
-
|
220
|
-
test "raising a MissingOptionError when a required option is missing" do
|
221
|
-
opts = Slop.new { on :foo, :required => true }
|
222
|
-
assert_raises(Slop::MissingOptionError) { opts.parse %w'' }
|
223
|
-
end
|
224
|
-
|
225
|
-
test "raising InvalidOptionError when strict mode is enabled and an unknown option appears" do
|
226
|
-
opts = Slop.new :strict => true
|
227
|
-
assert_raises(Slop::InvalidOptionError) { opts.parse %w'--foo' }
|
228
|
-
assert_raises(Slop::InvalidOptionError) { opts.parse %w'-fabc' }
|
229
|
-
end
|
230
|
-
|
231
|
-
test "raising InvalidOptionError for multiple short options" do
|
232
|
-
opts = Slop.new :strict => true
|
233
|
-
opts.on :L
|
234
|
-
assert_raises(Slop::InvalidOptionError) { opts.parse %w'-Ly' }
|
235
|
-
|
236
|
-
# but not with no strict mode!
|
237
|
-
opts = Slop.new
|
238
|
-
opts.on :L
|
239
|
-
assert opts.parse %w'-Ly'
|
240
|
-
end
|
241
|
-
|
242
|
-
test "multiple_switches is enabled by default" do
|
243
|
-
opts = Slop.new { on :f; on :b }
|
244
|
-
opts.parse %w[ -fb ]
|
245
|
-
assert opts.present?(:f)
|
246
|
-
assert opts.present?(:b)
|
247
|
-
end
|
248
|
-
|
249
|
-
test "multiple_switches disabled" do
|
250
|
-
opts = Slop.new(:multiple_switches => false) { on :f= }
|
251
|
-
opts.parse %w[ -fabc123 ]
|
252
|
-
assert_equal 'abc123', opts[:f]
|
253
|
-
end
|
254
|
-
|
255
|
-
test "muiltiple_switches should not trash arguments" do
|
256
|
-
opts = Slop.new{ on :f; on :b }
|
257
|
-
args = opts.parse!(%w'-fb foo')
|
258
|
-
assert_equal %w'foo', args
|
259
|
-
end
|
260
|
-
|
261
|
-
test "multiple options should still accept trailing arguments" do
|
262
|
-
opts = Slop.new { on :a; on :b= }
|
263
|
-
opts.parse %w'-ab foo'
|
264
|
-
assert_equal 'foo', opts[:b]
|
265
|
-
end
|
266
|
-
|
267
|
-
test "setting/getting the banner" do
|
268
|
-
opts = Slop.new :banner => 'foo'
|
269
|
-
assert_equal 'foo', opts.banner
|
270
|
-
|
271
|
-
opts = Slop.new
|
272
|
-
opts.banner 'foo'
|
273
|
-
assert_equal 'foo', opts.banner
|
274
|
-
|
275
|
-
opts = Slop.new
|
276
|
-
opts.banner = 'foo'
|
277
|
-
assert_equal 'foo', opts.banner
|
278
|
-
end
|
279
|
-
|
280
|
-
test "get/[] fetching an options argument value" do
|
281
|
-
opts = Slop.new { on :foo=; on :bar; on :baz }
|
282
|
-
opts.parse %w' --foo hello --bar '
|
283
|
-
assert_equal 'hello', opts[:foo]
|
284
|
-
assert_equal true, opts[:bar]
|
285
|
-
assert_nil opts[:baz]
|
286
|
-
end
|
287
|
-
|
288
|
-
test "checking for an options presence" do
|
289
|
-
opts = Slop.new { on :foo; on :bar }
|
290
|
-
opts.parse %w' --foo '
|
291
|
-
assert opts.present?(:foo)
|
292
|
-
refute opts.present?(:bar)
|
293
|
-
end
|
294
|
-
|
295
|
-
test "ignoring case" do
|
296
|
-
opts = Slop.new { on :foo }
|
297
|
-
opts.parse %w' --FOO bar '
|
298
|
-
assert_nil opts[:foo]
|
299
|
-
|
300
|
-
opts = Slop.new(:ignore_case => true) { on :foo= }
|
301
|
-
opts.parse %w' --FOO bar '
|
302
|
-
assert_equal 'bar', opts[:foo]
|
303
|
-
end
|
304
|
-
|
305
|
-
test "supporting dash" do
|
306
|
-
opts = Slop.new { on :foo_bar= }
|
307
|
-
opts.parse %w' --foo-bar baz '
|
308
|
-
assert_equal 'baz', opts[:foo_bar]
|
309
|
-
assert opts.foo_bar?
|
310
|
-
end
|
311
|
-
|
312
|
-
test "supporting underscore" do
|
313
|
-
opts = Slop.new { on :foo_bar= }
|
314
|
-
opts.parse %w' --foo_bar baz '
|
315
|
-
assert_equal 'baz', opts[:foo_bar]
|
316
|
-
assert opts.foo_bar?
|
317
|
-
end
|
318
|
-
|
319
|
-
# test "parsing an optspec and building options" do
|
320
|
-
# optspec = <<-SPEC
|
321
|
-
# ruby foo.rb [options]
|
322
|
-
# --
|
323
|
-
# v,verbose enable verbose mode
|
324
|
-
# q,quiet enable quiet mode
|
325
|
-
# n,name= set your name
|
326
|
-
# p,pass=? set your password
|
327
|
-
# SPEC
|
328
|
-
# opts = Slop.optspec(optspec.gsub(/^\s+/, ''))
|
329
|
-
# opts.parse %w[ --verbose --name Lee ]
|
330
|
-
|
331
|
-
# assert_equal 'Lee', opts[:name]
|
332
|
-
# assert opts.present?(:verbose)
|
333
|
-
# assert_equal 'enable quiet mode', opts.fetch_option(:quiet).description
|
334
|
-
# assert opts.fetch_option(:pass).accepts_optional_argument?
|
335
|
-
# end
|
336
|
-
|
337
|
-
test "ensure negative integers are not processed as options" do
|
338
|
-
items = %w(-1)
|
339
|
-
Slop.parse!(items)
|
340
|
-
assert_equal %w(-1), items
|
341
|
-
end
|
342
|
-
|
343
|
-
test "separators" do
|
344
|
-
opts = Slop.new(:banner => false) do
|
345
|
-
on :foo
|
346
|
-
separator "hello"
|
347
|
-
separator "world"
|
348
|
-
on :bar
|
349
|
-
end
|
350
|
-
assert_equal " --foo \nhello\nworld\n --bar ", opts.help
|
351
|
-
|
352
|
-
opts = Slop.new do
|
353
|
-
banner "foo"
|
354
|
-
separator "bar"
|
355
|
-
end
|
356
|
-
assert_equal "foo\nbar\n", opts.help
|
357
|
-
end
|
358
|
-
|
359
|
-
test "printing help with :help => true" do
|
360
|
-
temp_stdout do |string|
|
361
|
-
opts = Slop.new(:help => true, :banner => false)
|
362
|
-
assert_raises SystemExit do
|
363
|
-
opts.parse %w( --help )
|
364
|
-
end
|
365
|
-
assert_equal " -h, --help Display this help message.\n", string
|
366
|
-
end
|
367
|
-
|
368
|
-
temp_stdout do |string|
|
369
|
-
opts = Slop.new(:help => true)
|
370
|
-
assert_raises SystemExit do
|
371
|
-
opts.parse %w( --help )
|
372
|
-
end
|
373
|
-
assert_equal "Usage: rake_test_loader [options]\n -h, --help Display this help message.\n", string
|
374
|
-
end
|
375
|
-
end
|
376
|
-
|
377
|
-
test "fallback to substituting - for _ when using <option>?" do
|
378
|
-
opts = Slop.new do
|
379
|
-
on 'foo-bar'
|
380
|
-
end
|
381
|
-
opts.parse %w( --foo-bar )
|
382
|
-
assert opts.foo_bar?
|
383
|
-
end
|
384
|
-
|
385
|
-
test "option=value syntax does NOT consume following argument" do
|
386
|
-
opts = Slop.new { on :foo=; on 'bar=?' }
|
387
|
-
args = %w' --foo=bar baz --bar=zing hello '
|
388
|
-
opts.parse!(args)
|
389
|
-
assert_equal %w' baz hello ', args
|
390
|
-
end
|
391
|
-
|
392
|
-
test "context and return value of constructor block" do
|
393
|
-
peep = nil
|
394
|
-
ret = Slop.new { peep = self }
|
395
|
-
assert_same ret, peep
|
396
|
-
assert !equal?(peep)
|
397
|
-
|
398
|
-
peep = nil
|
399
|
-
ret = Slop.new { |a| peep = self }
|
400
|
-
assert !peep.equal?(ret)
|
401
|
-
assert_same peep, self
|
402
|
-
|
403
|
-
peep = nil
|
404
|
-
ret = Slop.new { |a, b| peep = self }
|
405
|
-
assert_same ret, peep
|
406
|
-
assert !equal?(peep)
|
407
|
-
|
408
|
-
peep = nil
|
409
|
-
ret = Slop.new { |a, *rest| peep = self }
|
410
|
-
assert_same ret, peep
|
411
|
-
assert !equal?(peep)
|
412
|
-
|
413
|
-
peep = nil
|
414
|
-
ret = Slop.parse([]) { peep = self }
|
415
|
-
assert_same ret, peep
|
416
|
-
assert !equal?(peep)
|
417
|
-
|
418
|
-
peep = nil
|
419
|
-
ret = Slop.parse([]) { |a| peep = self }
|
420
|
-
assert !peep.equal?(ret)
|
421
|
-
assert_same peep, self
|
422
|
-
|
423
|
-
peep = nil
|
424
|
-
ret = Slop.parse([]) { |a, b| peep = self }
|
425
|
-
assert_same ret, peep
|
426
|
-
assert !equal?(peep)
|
427
|
-
|
428
|
-
peep = nil
|
429
|
-
ret = Slop.parse([]) { |a, *rest| peep = self }
|
430
|
-
assert_same ret, peep
|
431
|
-
assert !equal?(peep)
|
432
|
-
end
|
433
|
-
|
434
|
-
test "to_s do not break self" do
|
435
|
-
slop = Slop.new do
|
436
|
-
banner "foo"
|
437
|
-
end
|
438
|
-
|
439
|
-
assert_equal "foo", slop.banner
|
440
|
-
slop.to_s
|
441
|
-
assert_equal "foo", slop.banner
|
442
|
-
end
|
443
|
-
|
444
|
-
test "options with prefixed --no should not default to inverted behaviour unless intended" do
|
445
|
-
opts = Slop.new { on :bar }
|
446
|
-
opts.parse %w'--no-bar'
|
447
|
-
assert_equal false, opts[:bar]
|
448
|
-
|
449
|
-
opts = Slop.new { on 'no-bar' }
|
450
|
-
opts.parse %w'--no-bar'
|
451
|
-
assert_equal true, opts['no-bar']
|
452
|
-
end
|
453
|
-
|
454
|
-
test "method missing() is a private method" do
|
455
|
-
assert Slop.new.private_methods.map(&:to_sym).include?(:method_missing)
|
456
|
-
end
|
457
|
-
|
458
|
-
test "respond_to?() arity checker is similar of other objects" do
|
459
|
-
slop = Slop.new
|
460
|
-
obj = Object.new
|
461
|
-
|
462
|
-
assert_same obj.respond_to?(:__id__), slop.respond_to?(:__id__)
|
463
|
-
assert_same obj.respond_to?(:__id__, false), slop.respond_to?(:__id__, false)
|
464
|
-
assert_same obj.respond_to?(:__id__, true), slop.respond_to?(:__id__, true)
|
465
|
-
|
466
|
-
assert_raises ArgumentError do
|
467
|
-
slop.respond_to? :__id__, false, :INVALID_ARGUMENT
|
468
|
-
end
|
469
|
-
end
|
470
|
-
|
471
|
-
test "adding a runner" do
|
472
|
-
orun = proc { |r| assert_instance_of Slop, r }
|
473
|
-
arun = proc { |r| assert_equal ['foo', 'bar'], r }
|
474
|
-
|
475
|
-
Slop.parse(%w'foo --foo bar -v bar') do
|
476
|
-
on :v
|
477
|
-
on :foo=
|
478
|
-
run { |o, a| orun[o]; arun[a] }
|
479
|
-
end
|
480
|
-
end
|
481
|
-
|
482
|
-
test "ensure a runner does not execute when a help option is present" do
|
483
|
-
items = []
|
484
|
-
Slop.parse(%w'--help foo bar') do
|
485
|
-
run { |o, a| items.concat a }
|
486
|
-
end
|
487
|
-
assert_equal %w'--help foo bar', items
|
488
|
-
items.clear
|
489
|
-
temp_stdout do
|
490
|
-
assert_raises SystemExit do
|
491
|
-
Slop.parse(%w'--help foo bar', :help => true) do
|
492
|
-
run { |o, a| items.concat a }
|
493
|
-
end
|
494
|
-
end
|
495
|
-
assert_empty items
|
496
|
-
end
|
497
|
-
end
|
498
|
-
|
499
|
-
test "duplicate options should not exist, new options should replace old ones" do
|
500
|
-
i = nil
|
501
|
-
Slop.parse(%w'-v') do
|
502
|
-
on(:v) { i = 'first' }
|
503
|
-
on(:v) { i = 'second' }
|
504
|
-
end
|
505
|
-
assert_equal 'second', i
|
506
|
-
end
|
507
|
-
|
508
|
-
test "taking out the trash" do
|
509
|
-
args = []
|
510
|
-
opts = Slop.new { on :f, :foo }
|
511
|
-
opts.run { |_, a| args = a }
|
512
|
-
opts.parse! %w(--foo bar)
|
513
|
-
assert_equal %w(bar), args
|
514
|
-
opts.parse! %w(foo)
|
515
|
-
assert_equal %w(foo), args
|
516
|
-
end
|
517
|
-
|
518
|
-
end
|