slop 1.5.2 → 1.5.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/README.md +2 -2
- data/lib/slop.rb +10 -3
- data/lib/slop/option.rb +19 -2
- data/lib/slop/options.rb +2 -1
- data/test/option_test.rb +12 -0
- data/test/slop_test.rb +22 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -21,7 +21,7 @@ Usage
|
|
21
21
|
# parse assumes ARGV, otherwise you can pass it your own Array
|
22
22
|
opts = Slop.parse do
|
23
23
|
on :v, :verbose, 'Enable verbose mode' # boolean value
|
24
|
-
on :n, :name, 'Your name', true # compulsory argument
|
24
|
+
on :n, :name, 'Your name', true # option requires a compulsory argument
|
25
25
|
on :s, :sex, 'Your sex', :optional => false # the same thing
|
26
26
|
on :a, :age, 'Your age', :optional => true # optional argument
|
27
27
|
end
|
@@ -389,4 +389,4 @@ thing in Slop:
|
|
389
389
|
on :a, :age, 'Your age', true
|
390
390
|
end
|
391
391
|
|
392
|
-
opts.to_hash(true) #=> { :name => 'lee', :age => 105 }
|
392
|
+
opts.to_hash(true) #=> { :name => 'lee', :age => 105 }
|
data/lib/slop.rb
CHANGED
@@ -16,7 +16,7 @@ class Slop
|
|
16
16
|
class InvalidOptionError < RuntimeError; end
|
17
17
|
|
18
18
|
# @return [String] The current version string
|
19
|
-
VERSION = '1.5.
|
19
|
+
VERSION = '1.5.3'
|
20
20
|
|
21
21
|
# Parses the items from a CLI format into a friendly object.
|
22
22
|
#
|
@@ -65,6 +65,10 @@ class Slop
|
|
65
65
|
# @option opts [String] :banner The banner text used for the help
|
66
66
|
# @option opts [Proc, #call] :on_empty Any object that respondes to `call`
|
67
67
|
# which is executed when Slop has no items to parse
|
68
|
+
# @option opts [IO, #puts] :io ($stderr) An IO object for writing to when
|
69
|
+
# :help => true is used
|
70
|
+
# @option opts [Boolean] :exit_on_help (true) When false and coupled with
|
71
|
+
# the :help option, Slop will not exit inside of the `help` option
|
68
72
|
def initialize(*opts, &block)
|
69
73
|
sloptions = {}
|
70
74
|
sloptions.merge! opts.pop if opts.last.is_a? Hash
|
@@ -83,14 +87,17 @@ class Slop
|
|
83
87
|
@on_empty = sloptions[:on_empty]
|
84
88
|
@sloptions = sloptions
|
85
89
|
|
90
|
+
io = sloptions[:io] || $stderr
|
91
|
+
eoh = true if sloptions[:exit_on_help].nil?
|
92
|
+
|
86
93
|
if block_given?
|
87
94
|
block.arity == 1 ? yield(self) : instance_eval(&block)
|
88
95
|
end
|
89
96
|
|
90
97
|
if sloptions[:help]
|
91
98
|
on :h, :help, 'Print this help message', :tail => true do
|
92
|
-
puts help
|
93
|
-
exit
|
99
|
+
io.puts help
|
100
|
+
exit if eoh
|
94
101
|
end
|
95
102
|
end
|
96
103
|
end
|
data/lib/slop/option.rb
CHANGED
@@ -21,6 +21,9 @@ class Slop
|
|
21
21
|
# regexp, otherwise Slop will raise an InvalidArgumentError
|
22
22
|
attr_reader :match
|
23
23
|
|
24
|
+
# @return [Object] true/false, or an optional help string to append
|
25
|
+
attr_reader :help
|
26
|
+
|
24
27
|
# @overload argument_value=(value)
|
25
28
|
# Set this options argument value
|
26
29
|
# @param [Object] value The value you'd like applied to this option
|
@@ -40,6 +43,7 @@ class Slop
|
|
40
43
|
# @option options [Integer] :limit (0)
|
41
44
|
# @option options [Boolean] :tail (false)
|
42
45
|
# @option options [Regexp] :match
|
46
|
+
# @option options [Boolean, String] :help
|
43
47
|
def initialize(slop, short, long, description, argument, options={}, &blk)
|
44
48
|
@slop = slop
|
45
49
|
@short_flag = short
|
@@ -52,6 +56,8 @@ class Slop
|
|
52
56
|
|
53
57
|
@tail = options[:tail]
|
54
58
|
@match = options[:match]
|
59
|
+
@help = options[:help]
|
60
|
+
@help = true if @help.nil?
|
55
61
|
|
56
62
|
@forced = false
|
57
63
|
@argument_value = nil
|
@@ -60,7 +66,12 @@ class Slop
|
|
60
66
|
@limit = options[:limit] || 0
|
61
67
|
|
62
68
|
if @long_flag && @long_flag.size > @slop.longest_flag
|
63
|
-
@
|
69
|
+
if @help.respond_to? :to_str
|
70
|
+
size = @long_flag.size + @help.size
|
71
|
+
else
|
72
|
+
size = @long_flag.size
|
73
|
+
end
|
74
|
+
@slop.longest_flag = size
|
64
75
|
end
|
65
76
|
|
66
77
|
@callback = blk if block_given?
|
@@ -127,7 +138,13 @@ class Slop
|
|
127
138
|
|
128
139
|
if @long_flag
|
129
140
|
out += "--#{@long_flag}"
|
130
|
-
|
141
|
+
if @help.respond_to? :to_str
|
142
|
+
out += " #{@help}"
|
143
|
+
size = @long_flag.size + @help.size + 1
|
144
|
+
else
|
145
|
+
size = @long_flag.size
|
146
|
+
end
|
147
|
+
diff = @slop.longest_flag - size
|
131
148
|
spaces = " " * (diff + 6)
|
132
149
|
out += spaces
|
133
150
|
else
|
data/lib/slop/options.rb
CHANGED
data/test/option_test.rb
CHANGED
@@ -110,6 +110,18 @@ class OptionTest < TestCase
|
|
110
110
|
assert slop.to_s.strip =~ /foo$/
|
111
111
|
end
|
112
112
|
|
113
|
+
test 'do not print help for options with :help => false' do
|
114
|
+
slop = Slop.new
|
115
|
+
slop.on :f, :foo, :help => false
|
116
|
+
refute slop.help.include?('foo')
|
117
|
+
end
|
118
|
+
|
119
|
+
test 'appends a help string with :help => "string"' do
|
120
|
+
slop = Slop.new
|
121
|
+
slop.on :n, :name, 'Your name', true, :help => '<YOUR NAME HERE>'
|
122
|
+
assert_equal ' -n, --name <YOUR NAME HERE> Your name', slop.options[:name].to_s
|
123
|
+
end
|
124
|
+
|
113
125
|
test 'argument matching' do
|
114
126
|
slop = Slop.new
|
115
127
|
slop.on :f, :foo, true, :match => /^h/
|
data/test/slop_test.rb
CHANGED
@@ -317,4 +317,26 @@ class SlopTest < TestCase
|
|
317
317
|
slop = Slop.new { on :f, :foo, true }
|
318
318
|
assert_raises(Slop::MissingArgumentError) { slop.parse %w/-f --bar/ }
|
319
319
|
end
|
320
|
+
|
321
|
+
test 'custom IO object' do
|
322
|
+
require 'stringio'
|
323
|
+
io = StringIO.new
|
324
|
+
slop = Slop.new(:help => true, :io => io)
|
325
|
+
slop.on :f, :foo, 'something fooey'
|
326
|
+
begin
|
327
|
+
slop.parse %w/--help/
|
328
|
+
rescue SystemExit
|
329
|
+
end
|
330
|
+
assert io.string.include? 'something fooey'
|
331
|
+
end
|
332
|
+
|
333
|
+
test 'exiting when using :help option' do
|
334
|
+
require 'stringio'
|
335
|
+
io = StringIO.new
|
336
|
+
opts = Slop.new(:help => true, :io => io)
|
337
|
+
assert_raises(SystemExit) { opts.parse %w/--help/ }
|
338
|
+
|
339
|
+
opts = Slop.new(:help => true, :io => io, :exit_on_help => false)
|
340
|
+
assert opts.parse %w/--help/
|
341
|
+
end
|
320
342
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: slop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.5.
|
5
|
+
version: 1.5.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Lee Jarvis
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-23 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|