slop 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -13
- data/lib/slop.rb +20 -11
- data/lib/slop/option.rb +4 -4
- data/spec/slop_spec.rb +30 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -3,9 +3,6 @@ Slop
|
|
3
3
|
|
4
4
|
Slop is a simple option collector with an easy to remember syntax and friendly API.
|
5
5
|
|
6
|
-
**NOTE** This software is still unstable, it contains bugs and lacks core
|
7
|
-
features
|
8
|
-
|
9
6
|
Installation
|
10
7
|
------------
|
11
8
|
|
@@ -17,8 +14,14 @@ Installation
|
|
17
14
|
|
18
15
|
git clone git://github.com/injekt/slop.git
|
19
16
|
cd slop
|
20
|
-
|
21
|
-
|
17
|
+
rake install
|
18
|
+
|
19
|
+
Features Overview
|
20
|
+
-----------------
|
21
|
+
|
22
|
+
- Callbacks
|
23
|
+
- Casting
|
24
|
+
- Lists
|
22
25
|
|
23
26
|
Usage
|
24
27
|
-----
|
@@ -86,14 +89,6 @@ you can pass the `:as` attribute to your option.
|
|
86
89
|
end
|
87
90
|
s.value_for(:age) #=> 20 # not "20"
|
88
91
|
|
89
|
-
Slop will also check your default attributes type to see if it can cast the new
|
90
|
-
value to the same type.
|
91
|
-
|
92
|
-
s = Slop.parse("--port 110") do
|
93
|
-
opt(port, true, :default => 80)
|
94
|
-
end
|
95
|
-
s.value_for(:port) #=> 110
|
96
|
-
|
97
92
|
Lists
|
98
93
|
-----
|
99
94
|
|
data/lib/slop.rb
CHANGED
@@ -3,7 +3,9 @@ require 'set'
|
|
3
3
|
require 'slop/option'
|
4
4
|
|
5
5
|
class Slop
|
6
|
-
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
VERSION = '0.1.7'
|
7
9
|
|
8
10
|
# Raised when an option expects an argument and none is given
|
9
11
|
class MissingArgumentError < ArgumentError; end
|
@@ -36,16 +38,9 @@ class Slop
|
|
36
38
|
raise ArgumentError, "Argument size must be no more than 4"
|
37
39
|
end
|
38
40
|
|
39
|
-
args.unshift nil if args.first.size > 1
|
40
|
-
args.push nil if args.size == 2
|
41
|
-
args.push false if args.size == 3
|
42
|
-
|
43
|
-
args[2..3] = [nil, true] if args[2] == true
|
44
|
-
|
45
41
|
attributes = [:flag, :option, :description, :argument]
|
46
|
-
options = Hash[attributes.zip(args)]
|
42
|
+
options = Hash[attributes.zip(pad_options(args))]
|
47
43
|
options.merge!(opts)
|
48
|
-
options[:as] = options[:default].class if options.key?(:default)
|
49
44
|
options[:callback] = blk if block_given?
|
50
45
|
|
51
46
|
@options << Option.new(options)
|
@@ -127,7 +122,7 @@ class Slop
|
|
127
122
|
#
|
128
123
|
# @return [Option] the option flag or label
|
129
124
|
def option_for(flag)
|
130
|
-
|
125
|
+
find do |opt|
|
131
126
|
opt.has_flag?(flag) || opt.has_option?(flag)
|
132
127
|
end
|
133
128
|
end
|
@@ -142,17 +137,31 @@ class Slop
|
|
142
137
|
#
|
143
138
|
# s.value_for(:name) #=> "Lee"
|
144
139
|
#
|
145
|
-
#
|
146
140
|
def value_for(flag)
|
147
141
|
return unless option = option_for(flag)
|
148
142
|
option.argument_value
|
149
143
|
end
|
150
144
|
alias :[] :value_for
|
151
145
|
|
146
|
+
# Implement #each so our options set is enumerable
|
147
|
+
def each
|
148
|
+
return enum_for(:each) unless block_given?
|
149
|
+
@options.each { |opt| yield opt }
|
150
|
+
end
|
151
|
+
|
152
152
|
private
|
153
153
|
|
154
154
|
def flag_or_option?(flag)
|
155
155
|
return unless flag
|
156
156
|
flag[1, 2].include?('-')
|
157
157
|
end
|
158
|
+
|
159
|
+
def pad_options(args)
|
160
|
+
args.unshift nil if args.first.nil? || args.first.size > 1
|
161
|
+
args.push nil if args.size < 2
|
162
|
+
args.push nil if args.size == 2
|
163
|
+
args.push false if args.size == 3
|
164
|
+
args[2..3] = [nil, true] if args[2] == true
|
165
|
+
args
|
166
|
+
end
|
158
167
|
end
|
data/lib/slop/option.rb
CHANGED
@@ -58,10 +58,10 @@ class Slop
|
|
58
58
|
@argument_value ||= @default
|
59
59
|
return unless @argument_value
|
60
60
|
|
61
|
-
case @as.to_s
|
62
|
-
when '
|
63
|
-
when '
|
64
|
-
when '
|
61
|
+
case @as.to_s.downcase[0, 3]
|
62
|
+
when 'arr'; @argument_value.split(@delimiter, @limit)
|
63
|
+
when 'int'; @argument_value.to_i
|
64
|
+
when 'sym'; @argument_value.to_sym
|
65
65
|
else
|
66
66
|
@argument_value
|
67
67
|
end
|
data/spec/slop_spec.rb
CHANGED
@@ -7,6 +7,12 @@ describe Slop do
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
+
it "is enumerable" do
|
11
|
+
Enumerable.instance_methods.each do |meth|
|
12
|
+
@slop.respond_to?(meth).should be_true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
10
16
|
describe "::options" do
|
11
17
|
it "should return the last set of options" do
|
12
18
|
s = Slop.new { option(:f, :foo, "foo") }
|
@@ -116,4 +122,27 @@ describe Slop do
|
|
116
122
|
end
|
117
123
|
end
|
118
124
|
|
119
|
-
|
125
|
+
describe "pad_options (private method)" do
|
126
|
+
before(:all) do
|
127
|
+
@args = [
|
128
|
+
[:n], [:n, :name], [:n, :name, "Desc"], [:n, :name, "Desc", true],
|
129
|
+
[:name], [:n, "Desc"], [:n, true], [:name, "Desc"], [:name, true]
|
130
|
+
]
|
131
|
+
end
|
132
|
+
|
133
|
+
it "always returns an array of 4 elements" do
|
134
|
+
@args.each do |arr|
|
135
|
+
args = @slop.send(:pad_options, arr)
|
136
|
+
args.should be_kind_of(Array)
|
137
|
+
args.size.should == 4
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
it "ends with a true or false class object" do
|
142
|
+
@args.each do |arr|
|
143
|
+
[true, false].include?(@slop.send(:pad_options, arr).last).should be_true
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
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
|
+
- 7
|
9
|
+
version: 0.1.7
|
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-09 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|