slop 0.1.9 → 0.2.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.
- data/README.md +23 -6
- data/lib/slop.rb +22 -4
- data/lib/slop/option.rb +7 -3
- data/spec/option_spec.rb +1 -1
- data/spec/slop_spec.rb +39 -2
- metadata +4 -4
data/README.md
CHANGED
@@ -47,13 +47,30 @@ Usage
|
|
47
47
|
end
|
48
48
|
|
49
49
|
# without `-v`
|
50
|
-
s
|
50
|
+
s[:verbose] #=> false
|
51
51
|
|
52
52
|
# using `-v`
|
53
|
-
s
|
53
|
+
s[:verbose] #=> true
|
54
54
|
|
55
55
|
# using `--applicable_age`
|
56
|
-
s
|
56
|
+
s[:applicable_age] #=> 20
|
57
|
+
|
58
|
+
Want these options back in a nice readable help string? Just call `Slop.to_s`
|
59
|
+
and Slop will return a nice indented option list. You can even add a banner to
|
60
|
+
the help text using the `banner` method like so:
|
61
|
+
|
62
|
+
opts = Slop.new do
|
63
|
+
banner("Usage: foo [options]")
|
64
|
+
|
65
|
+
opt(:n, :name, "Your name", true)
|
66
|
+
end
|
67
|
+
|
68
|
+
puts opts
|
69
|
+
|
70
|
+
Returns:
|
71
|
+
|
72
|
+
Usage: foo [options]
|
73
|
+
-n, --name <name> Your name
|
57
74
|
|
58
75
|
Callbacks
|
59
76
|
---------
|
@@ -80,7 +97,7 @@ you can pass the `:as` attribute to your option.
|
|
80
97
|
s = Slop.parse("--age 20") do
|
81
98
|
opt(:age, true, :as => Integer) # :int/:integer both also work
|
82
99
|
end
|
83
|
-
s
|
100
|
+
s[:age] #=> 20 # not "20"
|
84
101
|
|
85
102
|
Smart
|
86
103
|
-----
|
@@ -110,14 +127,14 @@ You can of course also parse lists into options. Here's how:
|
|
110
127
|
s = Slop.parse("--people lee,injekt") do
|
111
128
|
opt(:people, true, :as => Array)
|
112
129
|
end
|
113
|
-
s
|
130
|
+
s[:people] #=> ["lee", "injekt"]
|
114
131
|
|
115
132
|
You can also change both the split delimiter and limit
|
116
133
|
|
117
134
|
s = Slop.parse("--people lee:injekt:bob") do
|
118
135
|
opt(:people, true, :as => Array, :delimiter => ':', :limit => 2)
|
119
136
|
end
|
120
|
-
s
|
137
|
+
s[:people] #=> ["lee", "injekt:bob"]
|
121
138
|
|
122
139
|
Contributing
|
123
140
|
------------
|
data/lib/slop.rb
CHANGED
@@ -5,7 +5,7 @@ require 'slop/option'
|
|
5
5
|
class Slop
|
6
6
|
include Enumerable
|
7
7
|
|
8
|
-
VERSION = '0.
|
8
|
+
VERSION = '0.2.0'
|
9
9
|
|
10
10
|
# Raised when an option expects an argument and none is given
|
11
11
|
class MissingArgumentError < ArgumentError; end
|
@@ -24,11 +24,20 @@ class Slop
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def initialize(&blk)
|
27
|
+
@banner = nil
|
27
28
|
@options = Set.new
|
28
29
|
@@options = @options
|
29
30
|
instance_eval(&blk) if block_given?
|
30
31
|
end
|
31
32
|
|
33
|
+
# set the help string banner
|
34
|
+
# @param [String,#to_s] banner
|
35
|
+
# @return The banner, or nil
|
36
|
+
def banner(banner=nil)
|
37
|
+
@banner = banner if banner
|
38
|
+
@banner
|
39
|
+
end
|
40
|
+
|
32
41
|
# add an option
|
33
42
|
def option(*args, &blk)
|
34
43
|
opts = args.pop if args.last.is_a?(Hash)
|
@@ -149,6 +158,15 @@ class Slop
|
|
149
158
|
@options.each { |opt| yield opt }
|
150
159
|
end
|
151
160
|
|
161
|
+
def to_s
|
162
|
+
str = ""
|
163
|
+
str << @banner + "\n" if @banner
|
164
|
+
each do |opt|
|
165
|
+
str << "#{opt}\n"
|
166
|
+
end
|
167
|
+
str
|
168
|
+
end
|
169
|
+
|
152
170
|
private
|
153
171
|
|
154
172
|
def flag_or_option?(flag)
|
@@ -173,10 +191,10 @@ class Slop
|
|
173
191
|
args.push nil if args.size == 2
|
174
192
|
args.push false if args.size == 3
|
175
193
|
|
176
|
-
# if the second argument includes a space it's
|
177
|
-
# description, so we insert a nil into the option field and
|
194
|
+
# if the second argument includes a space or some odd character it's
|
195
|
+
# probably a description, so we insert a nil into the option field and
|
178
196
|
# insert the original value into the description field
|
179
|
-
args[1..2] = [nil, args[1]]
|
197
|
+
args[1..2] = [nil, args[1]] unless args[1].to_s =~ /\A[a-zA-Z_-]*\z/
|
180
198
|
|
181
199
|
# if there's no description given but the option requires an argument, it'll
|
182
200
|
# probably look like this: `[:f, :option, true]`
|
data/lib/slop/option.rb
CHANGED
@@ -16,6 +16,11 @@ class Slop
|
|
16
16
|
# @return [Proc]
|
17
17
|
attr_reader :callback
|
18
18
|
|
19
|
+
# The maximum size of any --option, recorded for
|
20
|
+
# padding out any output string with smaller option lengths
|
21
|
+
# with the correct amount of whitespace
|
22
|
+
#
|
23
|
+
# @return [Fixnum]
|
19
24
|
@@max_option_size = 0
|
20
25
|
|
21
26
|
# @param [Hash] options Option attributes
|
@@ -146,7 +151,6 @@ class Slop
|
|
146
151
|
@option || @flag
|
147
152
|
end
|
148
153
|
|
149
|
-
# @todo Write specs for the output string
|
150
154
|
def to_s
|
151
155
|
str = "\t"
|
152
156
|
|
@@ -168,13 +172,13 @@ class Slop
|
|
168
172
|
end
|
169
173
|
end
|
170
174
|
size_diff = @@max_option_size - ((@option.size * 2) + 4)
|
171
|
-
optionstr << " " * size_diff
|
175
|
+
optionstr << " " * size_diff if size_diff > 0
|
172
176
|
str << optionstr
|
173
177
|
else
|
174
178
|
str << " " * (@@max_option_size + 2)
|
175
179
|
end
|
176
180
|
|
177
|
-
str << "\t#{@description}" if @description
|
181
|
+
str << "\t\t#{@description}" if @description
|
178
182
|
str
|
179
183
|
end
|
180
184
|
|
data/spec/option_spec.rb
CHANGED
@@ -44,7 +44,7 @@ describe Slop::Option do
|
|
44
44
|
opt.argument_value.should == ["foo:bar"]
|
45
45
|
end
|
46
46
|
|
47
|
-
it "can use a custom
|
47
|
+
it "can use a custom delimiter" do
|
48
48
|
opt = Slop::Option.new(:as => Array, :delimiter => ':')
|
49
49
|
opt.argument_value = "foo,bar"
|
50
50
|
opt.argument_value.should == ["foo,bar"]
|
data/spec/slop_spec.rb
CHANGED
@@ -13,6 +13,15 @@ describe Slop do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
describe "banner" do
|
17
|
+
it "adds a banner to the beginning of the help string" do
|
18
|
+
o = Slop.new do
|
19
|
+
banner("foo bar")
|
20
|
+
end
|
21
|
+
o.to_s.should == "foo bar\n"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
16
25
|
describe "::options" do
|
17
26
|
it "should return the last set of options" do
|
18
27
|
s = Slop.new { option(:f, :foo, "foo") }
|
@@ -136,8 +145,16 @@ describe Slop do
|
|
136
145
|
end
|
137
146
|
|
138
147
|
it "detects a description in place of an option, if one exists" do
|
139
|
-
|
140
|
-
|
148
|
+
valid = [ "Description here", "Description!", "de#scription" ]
|
149
|
+
invalid = [ "Description", "some-description" ] # these pass as valid options
|
150
|
+
|
151
|
+
valid.each do |v|
|
152
|
+
@slop.send(:pad_options, [:n, v]).should == [:n, nil, v, false]
|
153
|
+
end
|
154
|
+
|
155
|
+
invalid.each do |i|
|
156
|
+
@slop.send(:pad_options, [:n, i]).should == [:n, i, nil, false]
|
157
|
+
end
|
141
158
|
end
|
142
159
|
|
143
160
|
it "always returns an array of 4 elements" do
|
@@ -164,4 +181,24 @@ describe Slop do
|
|
164
181
|
end
|
165
182
|
end
|
166
183
|
|
184
|
+
describe "help string" do
|
185
|
+
before :all do
|
186
|
+
@o = Slop.new do
|
187
|
+
banner("Usage: foo [options]")
|
188
|
+
opt(:n, :name, "Your name")
|
189
|
+
opt(:a, :age, "Your age")
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
it "starts with a banner if one exists" do
|
194
|
+
@o.to_s.split("\n").first.should == "Usage: foo [options]"
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should include all options" do
|
198
|
+
@o.each do |option|
|
199
|
+
flag, opt, des = option.flag, option.option, option.description
|
200
|
+
[flag, opt, des].each {|a| @o.to_s.include?(a.to_s).should be_true }
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
167
204
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Lee Jarvis
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-12-
|
17
|
+
date: 2010-12-14 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|