slop 0.1.8 → 0.1.9
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 +20 -7
- data/lib/slop.rb +20 -2
- data/lib/slop/option.rb +40 -5
- data/spec/option_spec.rb +41 -0
- data/spec/slop_spec.rb +5 -0
- metadata +3 -3
data/README.md
CHANGED
@@ -16,13 +16,6 @@ Installation
|
|
16
16
|
cd slop
|
17
17
|
rake install
|
18
18
|
|
19
|
-
Features Overview
|
20
|
-
-----------------
|
21
|
-
|
22
|
-
- Callbacks
|
23
|
-
- Casting
|
24
|
-
- Lists
|
25
|
-
|
26
19
|
Usage
|
27
20
|
-----
|
28
21
|
|
@@ -89,6 +82,26 @@ you can pass the `:as` attribute to your option.
|
|
89
82
|
end
|
90
83
|
s.value_for(:age) #=> 20 # not "20"
|
91
84
|
|
85
|
+
Smart
|
86
|
+
-----
|
87
|
+
|
88
|
+
Slop is pretty smart when it comes to building your options, for example if you
|
89
|
+
want your option to have a flag attribute, but no `--option` attribute, you
|
90
|
+
can do this:
|
91
|
+
|
92
|
+
opt(:n, "Your name")
|
93
|
+
|
94
|
+
and Slop will detect a description in place of an option, so you don't have to
|
95
|
+
do this:
|
96
|
+
|
97
|
+
opt(:n, nil, "Your name")
|
98
|
+
|
99
|
+
You can also try other variations:
|
100
|
+
|
101
|
+
opt(:name, "Your name")
|
102
|
+
opt(:n, :name)
|
103
|
+
opt(:name)
|
104
|
+
|
92
105
|
Lists
|
93
106
|
-----
|
94
107
|
|
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.1.
|
8
|
+
VERSION = '0.1.9'
|
9
9
|
|
10
10
|
# Raised when an option expects an argument and none is given
|
11
11
|
class MissingArgumentError < ArgumentError; end
|
@@ -162,11 +162,29 @@ class Slop
|
|
162
162
|
end
|
163
163
|
|
164
164
|
def pad_options(args)
|
165
|
+
# if the first value is not a single character, it's probably an option
|
166
|
+
# so we just replace it with nil
|
165
167
|
args.unshift nil if args.first.nil? || args.first.size > 1
|
166
|
-
|
168
|
+
|
169
|
+
# if there's only one or two arguments, we pad the values out
|
170
|
+
# with nil values, eventually adding a 'false' to the end of the stack
|
171
|
+
# because that's the default to represent required arguments
|
172
|
+
args.push nil if args.size == 1
|
167
173
|
args.push nil if args.size == 2
|
168
174
|
args.push false if args.size == 3
|
175
|
+
|
176
|
+
# if the second argument includes a space it's probably a
|
177
|
+
# description, so we insert a nil into the option field and
|
178
|
+
# insert the original value into the description field
|
179
|
+
args[1..2] = [nil, args[1]] if args[1].to_s.include?(" ")
|
180
|
+
|
181
|
+
# if there's no description given but the option requires an argument, it'll
|
182
|
+
# probably look like this: `[:f, :option, true]`
|
183
|
+
# so we replace the third option with nil to represent the description
|
184
|
+
# and push the true to the end of the stack
|
169
185
|
args[2..3] = [nil, true] if args[2] == true
|
186
|
+
|
187
|
+
# phew
|
170
188
|
args
|
171
189
|
end
|
172
190
|
end
|
data/lib/slop/option.rb
CHANGED
@@ -16,6 +16,8 @@ class Slop
|
|
16
16
|
# @return [Proc]
|
17
17
|
attr_reader :callback
|
18
18
|
|
19
|
+
@@max_option_size = 0
|
20
|
+
|
19
21
|
# @param [Hash] options Option attributes
|
20
22
|
# @option options [Symbol,#to_s] :flag
|
21
23
|
# @option options [Symbol,#to_s] :option
|
@@ -44,6 +46,16 @@ class Slop
|
|
44
46
|
@limit = options[:limit] || 0
|
45
47
|
|
46
48
|
@argument_value = nil
|
49
|
+
|
50
|
+
if @option
|
51
|
+
if requires_argument?
|
52
|
+
size = (@option.size * 2) + 4
|
53
|
+
else
|
54
|
+
size = @option.size + 2
|
55
|
+
end
|
56
|
+
|
57
|
+
@@max_option_size = size if @@max_option_size < size
|
58
|
+
end
|
47
59
|
end
|
48
60
|
|
49
61
|
# Set the argument value
|
@@ -134,13 +146,36 @@ class Slop
|
|
134
146
|
@option || @flag
|
135
147
|
end
|
136
148
|
|
149
|
+
# @todo Write specs for the output string
|
137
150
|
def to_s
|
138
151
|
str = "\t"
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
152
|
+
|
153
|
+
if @flag
|
154
|
+
str << "-#{@flag}"
|
155
|
+
else
|
156
|
+
str << " " * 4
|
157
|
+
end
|
158
|
+
|
159
|
+
if @option
|
160
|
+
str << ", " if @flag
|
161
|
+
optionstr = "--#{@option}"
|
162
|
+
|
163
|
+
if requires_argument?
|
164
|
+
if optional_argument?
|
165
|
+
optionstr << " [#{@option}]"
|
166
|
+
else
|
167
|
+
optionstr << " <#{@option}>"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
size_diff = @@max_option_size - ((@option.size * 2) + 4)
|
171
|
+
optionstr << " " * size_diff
|
172
|
+
str << optionstr
|
173
|
+
else
|
174
|
+
str << " " * (@@max_option_size + 2)
|
175
|
+
end
|
176
|
+
|
177
|
+
str << "\t#{@description}" if @description
|
178
|
+
str
|
144
179
|
end
|
145
180
|
|
146
181
|
def inspect
|
data/spec/option_spec.rb
CHANGED
@@ -142,4 +142,45 @@ describe Slop::Option do
|
|
142
142
|
Slop::Option.new(:flag => :n).key.should == :n
|
143
143
|
end
|
144
144
|
end
|
145
|
+
|
146
|
+
describe "to_s" do
|
147
|
+
before :all do
|
148
|
+
o = Slop.new do
|
149
|
+
opt(:n, nil, "Your name", true)
|
150
|
+
opt(:a, :age, "Your age", :optional => true)
|
151
|
+
opt(:verbose, "Enable verbose mode")
|
152
|
+
opt(:p, :password, "Your password", true)
|
153
|
+
end
|
154
|
+
@opt = {}
|
155
|
+
@opt[:flag] = o.option_for(:n)
|
156
|
+
@opt[:optional] = o.option_for(:age)
|
157
|
+
@opt[:option] = o.option_for(:verbose)
|
158
|
+
@opt[:required] = o.option_for(:password)
|
159
|
+
end
|
160
|
+
|
161
|
+
it "starts with a tab space" do
|
162
|
+
@opt[:flag].to_s[0].should == "\t"
|
163
|
+
end
|
164
|
+
|
165
|
+
it "displays a flag if one exists" do
|
166
|
+
@opt[:flag].to_s[1, 2].should == "-n"
|
167
|
+
end
|
168
|
+
|
169
|
+
it "appends a comma to the flag if an option exists" do
|
170
|
+
@opt[:flag].to_s[3].should_not == ","
|
171
|
+
@opt[:optional].to_s[3].should == ","
|
172
|
+
end
|
173
|
+
|
174
|
+
it "displays an option if one exists" do
|
175
|
+
@opt[:option].to_s[5, 9].should == "--verbose"
|
176
|
+
end
|
177
|
+
|
178
|
+
it "adds square brackes to the option if the argument is optional" do
|
179
|
+
@opt[:optional].to_s[5, 11].should == "--age [age]"
|
180
|
+
end
|
181
|
+
|
182
|
+
it "adds angle brackets to the option if the argument is required" do
|
183
|
+
@opt[:required].to_s[5, 21].should == "--password <password>"
|
184
|
+
end
|
185
|
+
end
|
145
186
|
end
|
data/spec/slop_spec.rb
CHANGED
@@ -135,6 +135,11 @@ describe Slop do
|
|
135
135
|
]
|
136
136
|
end
|
137
137
|
|
138
|
+
it "detects a description in place of an option, if one exists" do
|
139
|
+
args = @slop.send(:pad_options, [:n, "Description here"])
|
140
|
+
args.should == [:n, nil, "Description here", false]
|
141
|
+
end
|
142
|
+
|
138
143
|
it "always returns an array of 4 elements" do
|
139
144
|
@args.each do |arr|
|
140
145
|
args = @slop.send(:pad_options, arr)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 9
|
9
|
+
version: 0.1.9
|
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-11 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|