slop 0.2.0 → 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.gitignore +4 -0
- data/.yardopts +1 -0
- data/README.md +87 -67
- data/Rakefile +7 -0
- data/lib/slop.rb +105 -162
- data/lib/slop/option.rb +70 -152
- data/lib/slop/version.rb +3 -0
- data/slop.gemspec +17 -0
- data/test/helper.rb +14 -0
- data/test/option_test.rb +80 -0
- data/test/slop_test.rb +113 -0
- metadata +23 -38
- data/spec/option_spec.rb +0 -186
- data/spec/slop_spec.rb +0 -204
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 2
|
8
|
-
- 0
|
9
|
-
version: 0.2.0
|
4
|
+
prerelease: 6
|
5
|
+
version: 1.0.0.rc1
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Lee Jarvis
|
@@ -14,24 +10,10 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
13
|
+
date: 2011-03-16 00:00:00 +00:00
|
18
14
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
21
|
-
name: rspec
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
none: false
|
25
|
-
requirements:
|
26
|
-
- - "="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 2
|
30
|
-
- 1
|
31
|
-
- 0
|
32
|
-
version: 2.1.0
|
33
|
-
type: :development
|
34
|
-
version_requirements: *id001
|
15
|
+
dependencies: []
|
16
|
+
|
35
17
|
description: A simple DSL for gathering options and parsing the command line
|
36
18
|
email: lee@jarvis.co
|
37
19
|
executables: []
|
@@ -41,14 +23,21 @@ extensions: []
|
|
41
23
|
extra_rdoc_files: []
|
42
24
|
|
43
25
|
files:
|
26
|
+
- .gemtest
|
27
|
+
- .gitignore
|
28
|
+
- .yardopts
|
44
29
|
- LICENSE
|
45
30
|
- README.md
|
31
|
+
- Rakefile
|
46
32
|
- lib/slop.rb
|
47
33
|
- lib/slop/option.rb
|
48
|
-
-
|
49
|
-
-
|
34
|
+
- lib/slop/version.rb
|
35
|
+
- slop.gemspec
|
36
|
+
- test/helper.rb
|
37
|
+
- test/option_test.rb
|
38
|
+
- test/slop_test.rb
|
50
39
|
has_rdoc: true
|
51
|
-
homepage: http://
|
40
|
+
homepage: http://github.com/injekt/slop
|
52
41
|
licenses: []
|
53
42
|
|
54
43
|
post_install_message:
|
@@ -61,25 +50,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
50
|
requirements:
|
62
51
|
- - ">="
|
63
52
|
- !ruby/object:Gem::Version
|
64
|
-
|
65
|
-
- 1
|
66
|
-
- 9
|
67
|
-
- 1
|
68
|
-
version: 1.9.1
|
53
|
+
version: "0"
|
69
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
55
|
none: false
|
71
56
|
requirements:
|
72
|
-
- - "
|
57
|
+
- - ">"
|
73
58
|
- !ruby/object:Gem::Version
|
74
|
-
|
75
|
-
- 0
|
76
|
-
version: "0"
|
59
|
+
version: 1.3.1
|
77
60
|
requirements: []
|
78
61
|
|
79
62
|
rubyforge_project:
|
80
|
-
rubygems_version: 1.
|
63
|
+
rubygems_version: 1.5.0
|
81
64
|
signing_key:
|
82
65
|
specification_version: 3
|
83
66
|
summary: Option gathering made easy
|
84
|
-
test_files:
|
85
|
-
|
67
|
+
test_files:
|
68
|
+
- test/helper.rb
|
69
|
+
- test/option_test.rb
|
70
|
+
- test/slop_test.rb
|
data/spec/option_spec.rb
DELETED
@@ -1,186 +0,0 @@
|
|
1
|
-
require File.expand_path('../../lib/slop', __FILE__)
|
2
|
-
|
3
|
-
describe Slop::Option do
|
4
|
-
|
5
|
-
describe "argument_value" do
|
6
|
-
it "should return the default value if no value is found" do
|
7
|
-
Slop::Option.new(:default => :foo).argument_value.should == :foo
|
8
|
-
end
|
9
|
-
|
10
|
-
it "is nil if there's no argument value and no default" do
|
11
|
-
Slop::Option.new().argument_value.should be_nil
|
12
|
-
end
|
13
|
-
|
14
|
-
it "converts arguments into an integer with the :as => Integer flag" do
|
15
|
-
opt = Slop::Option.new(:as => Integer)
|
16
|
-
opt.argument_value = "1"
|
17
|
-
opt.argument_value.should == 1
|
18
|
-
opt.argument_value.should be_kind_of Integer
|
19
|
-
end
|
20
|
-
|
21
|
-
it "converts arguments into a symbol with the :as => Symbol flag" do
|
22
|
-
opt = Slop::Option.new(:as => Symbol)
|
23
|
-
opt.argument_value = "lee"
|
24
|
-
opt.argument_value.should == :lee
|
25
|
-
opt.argument_value.should be_kind_of Symbol
|
26
|
-
end
|
27
|
-
|
28
|
-
describe "with the :as Array option" do
|
29
|
-
it "returns nil if no argument_value is set" do
|
30
|
-
Slop::Option.new(:as => Array).argument_value.should be_nil
|
31
|
-
end
|
32
|
-
|
33
|
-
it "returns an Array" do
|
34
|
-
opt = Slop::Option.new(:as => Array)
|
35
|
-
opt.argument_value = "foo"
|
36
|
-
opt.argument_value.should be_kind_of Array
|
37
|
-
end
|
38
|
-
|
39
|
-
it "uses , as the default delimiter" do
|
40
|
-
opt = Slop::Option.new(:as => Array)
|
41
|
-
opt.argument_value = "foo,bar"
|
42
|
-
opt.argument_value.should == ["foo", "bar"]
|
43
|
-
opt.argument_value = "foo:bar"
|
44
|
-
opt.argument_value.should == ["foo:bar"]
|
45
|
-
end
|
46
|
-
|
47
|
-
it "can use a custom delimiter" do
|
48
|
-
opt = Slop::Option.new(:as => Array, :delimiter => ':')
|
49
|
-
opt.argument_value = "foo,bar"
|
50
|
-
opt.argument_value.should == ["foo,bar"]
|
51
|
-
opt.argument_value = "foo:bar"
|
52
|
-
opt.argument_value = ["foo", "bar"]
|
53
|
-
end
|
54
|
-
|
55
|
-
it "can uses a custom limit" do
|
56
|
-
opt = Slop::Option.new(:as => Array, :limit => 3)
|
57
|
-
opt.argument_value = "foo,bar,baz,etc"
|
58
|
-
opt.argument_value.should == ["foo", "bar", "baz,etc"]
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "has_flag?" do
|
64
|
-
it "is true if the option contains a flag" do
|
65
|
-
Slop::Option.new().has_flag?(:n).should be_false
|
66
|
-
Slop::Option.new(:flag => :n).has_flag?(:n).should be_true
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe "has_option?" do
|
71
|
-
it "is true if the option constains an.. option" do
|
72
|
-
Slop::Option.new().has_option?(:name).should be_false
|
73
|
-
Slop::Option.new(:option => :name).has_option?(:name).should be_true
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
describe "has_default?" do
|
78
|
-
it "is true if the option contains a default value" do
|
79
|
-
Slop::Option.new(:default => 'Lee').has_default?.should be_true
|
80
|
-
Slop::Option.new().has_default?.should be_false
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
describe "has_switch?" do
|
85
|
-
it "is true if the option contains a switchable value" do
|
86
|
-
Slop::Option.new().has_switch?.should be_false
|
87
|
-
Slop::Option.new(:switch => 'injekt').has_switch?.should be_true
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe "has_callback?" do
|
92
|
-
it "is true if the option has a callback" do
|
93
|
-
Slop::Option.new().has_callback?.should be_false
|
94
|
-
Slop::Option.new(:callback => proc { }).has_callback?.should be_true
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
describe "execute_callback" do
|
99
|
-
it "executes a callback" do
|
100
|
-
opt = Slop::Option.new(:callback => proc { 'foo' })
|
101
|
-
opt.execute_callback.should == 'foo'
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
describe "requires_argument?" do
|
106
|
-
it "returns true if the option requires an argument" do
|
107
|
-
Slop::Option.new().requires_argument?.should be_false
|
108
|
-
Slop::Option.new(:argument => true).requires_argument?.should be_true
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
describe "optional_argument?" do
|
113
|
-
it "returns true if the option argument is optional" do
|
114
|
-
Slop::Option.new(:argument => true).optional_argument?.should be_false
|
115
|
-
Slop::Option.new(:argument => true, :optional => true).optional_argument?.should be_true
|
116
|
-
Slop::Option.new(:optional => true).optional_argument?.should be_true
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
describe "[]" do
|
121
|
-
it "should return an options value" do
|
122
|
-
Slop::Option.new()[:foo].should be_nil
|
123
|
-
Slop::Option.new(:foo => 'bar')[:foo].should == 'bar'
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe "switch_argument_value" do
|
128
|
-
it "replaces an options argument value with the switch value" do
|
129
|
-
opt = Slop::Option.new(:default => 'foo', :switch => 'bar')
|
130
|
-
opt.argument_value.should == 'foo'
|
131
|
-
opt.switch_argument_value
|
132
|
-
opt.argument_value.should == 'bar'
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
describe "key" do
|
137
|
-
it "returns the option if both a flag and option exist" do
|
138
|
-
Slop::Option.new(:flag => :n, :option => :name).key.should == :name
|
139
|
-
end
|
140
|
-
|
141
|
-
it "returns the flag if there is no option" do
|
142
|
-
Slop::Option.new(:flag => :n).key.should == :n
|
143
|
-
end
|
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
|
186
|
-
end
|
data/spec/slop_spec.rb
DELETED
@@ -1,204 +0,0 @@
|
|
1
|
-
require File.expand_path('../../lib/slop', __FILE__)
|
2
|
-
|
3
|
-
describe Slop do
|
4
|
-
before :all do
|
5
|
-
@slop = Slop.new do
|
6
|
-
option :v, :verbose, "Enable verbose mode"
|
7
|
-
end
|
8
|
-
end
|
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
|
-
|
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
|
-
|
25
|
-
describe "::options" do
|
26
|
-
it "should return the last set of options" do
|
27
|
-
s = Slop.new { option(:f, :foo, "foo") }
|
28
|
-
Slop.options.should == s.options
|
29
|
-
p = Slop.new { option(:b, :bar, "bar") }
|
30
|
-
Slop.options.should == p.options
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe "option" do
|
35
|
-
it "adds an option" do
|
36
|
-
@slop.options.find do |opt|
|
37
|
-
opt.flag == :v
|
38
|
-
end.should be_kind_of(Slop::Option)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "takes no more than 4 arguments" do
|
42
|
-
lambda do
|
43
|
-
Slop.new { option :a, :b, :c, :d, :e }
|
44
|
-
end.should raise_error(ArgumentError, "Argument size must be no more than 4")
|
45
|
-
end
|
46
|
-
|
47
|
-
it "accepts a block which assigns an option callback" do
|
48
|
-
s = Slop.parse("-v") do
|
49
|
-
opt(:v, :version, "Display version") { "Version 1" }
|
50
|
-
end
|
51
|
-
s.option_for(:version).callback.should be_kind_of(Proc)
|
52
|
-
s.option_for(:version).callback.call.should == "Version 1"
|
53
|
-
end
|
54
|
-
|
55
|
-
it "does not parse option values unless option.argument is true" do
|
56
|
-
Slop.parse("--name Lee") { opt :name }.value_for(:name).should be_nil
|
57
|
-
Slop.parse("--name Lee") { opt :name, true }.value_for(:name).should == "Lee"
|
58
|
-
Slop.parse("--name Lee") { opt :name, :argument => true }.value_for(:name).should == "Lee"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
describe "options_hash" do
|
63
|
-
it "returns a hash" do
|
64
|
-
@slop.options_hash.should be_kind_of(Hash)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe "option_for" do
|
69
|
-
it "returns an option" do
|
70
|
-
@slop.option_for(:v).should be_kind_of(Slop::Option)
|
71
|
-
end
|
72
|
-
|
73
|
-
it "returns nil otherwise" do
|
74
|
-
@slop.option_for(:nothing).should be_nil
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
describe "value_for/[]" do
|
79
|
-
it "returns the value of an option" do
|
80
|
-
s = Slop.parse("--name Lee") do
|
81
|
-
opt :n, :name, "Your name", true
|
82
|
-
end
|
83
|
-
s.value_for(:name).should == "Lee"
|
84
|
-
end
|
85
|
-
|
86
|
-
it "returns a default option if none is given" do
|
87
|
-
Slop.new { opt :name, true, :default => "Lee" }.value_for(:name).should == "Lee"
|
88
|
-
Slop.new { opt :name, true, :default => "Lee" }[:name].should == "Lee"
|
89
|
-
end
|
90
|
-
|
91
|
-
it "returns nil if an option does not exist" do
|
92
|
-
Slop.new.value_for(:name).should be_nil
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
describe "parse" do
|
97
|
-
it "returns self (Slop)" do
|
98
|
-
Slop.parse.should be_kind_of(Slop)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "parses a string" do
|
102
|
-
Slop.parse("--name Lee") { opt :name, true }.value_for(:name).should == "Lee"
|
103
|
-
end
|
104
|
-
|
105
|
-
it "parses an array" do
|
106
|
-
Slop.parse(%w"--name Lee") { opt :name, true }.value_for(:name).should == "Lee"
|
107
|
-
end
|
108
|
-
|
109
|
-
it "raises MissingArgumentError if no argument is given to a compulsory option" do
|
110
|
-
lambda { Slop.parse("--name") { opt :name, true } }.should raise_error(Slop::MissingArgumentError, /name/)
|
111
|
-
end
|
112
|
-
|
113
|
-
it "does not raise MissingArgumentError if the optional attribute is true" do
|
114
|
-
Slop.parse("--name") { opt :name, true, :optional => true }.value_for(:name).should be_nil
|
115
|
-
end
|
116
|
-
|
117
|
-
it "does not require argument to be true if optional is true" do
|
118
|
-
Slop.parse("--name Lee") { opt :name, :optional => true }.value_for(:name).should == "Lee"
|
119
|
-
end
|
120
|
-
|
121
|
-
it "responds to both long options and single character flags" do
|
122
|
-
Slop.parse("--name Lee") { opt :name, true }[:name].should == "Lee"
|
123
|
-
Slop.parse("-n Lee") { opt :n, :name, true }[:name].should == "Lee"
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe "options" do
|
128
|
-
it "returns a set" do
|
129
|
-
@slop.options.should be_kind_of Set
|
130
|
-
end
|
131
|
-
|
132
|
-
it "contains a set of Slop::Option" do
|
133
|
-
@slop.options.each do |opt|
|
134
|
-
opt.should be_kind_of(Slop::Option)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
describe "pad_options (private method)" do
|
140
|
-
before(:all) do
|
141
|
-
@args = [
|
142
|
-
[:n], [:n, :name], [:n, :name, "Desc"], [:n, :name, "Desc", true],
|
143
|
-
[:name], [:n, "Desc"], [:n, true], [:name, "Desc"], [:name, true]
|
144
|
-
]
|
145
|
-
end
|
146
|
-
|
147
|
-
it "detects a description in place of an option, if one exists" do
|
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
|
158
|
-
end
|
159
|
-
|
160
|
-
it "always returns an array of 4 elements" do
|
161
|
-
@args.each do |arr|
|
162
|
-
args = @slop.send(:pad_options, arr)
|
163
|
-
args.should be_kind_of(Array)
|
164
|
-
args.size.should == 4
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
it "ends with a true or false class object" do
|
169
|
-
@args.each do |arr|
|
170
|
-
[true, false].include?(@slop.send(:pad_options, arr).last).should be_true
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
describe "flag_or_option?" do
|
176
|
-
it "should be true if the string is a flag or an option" do
|
177
|
-
good = ["-f", "--flag"]
|
178
|
-
bad = ["-flag", "f", "flag", "f-lag", "flag-", "--", "-"]
|
179
|
-
good.each {|g| @slop.send(:flag_or_option?, g).should be_true }
|
180
|
-
bad.each {|b| @slop.send(:flag_or_option?, b).should be_false }
|
181
|
-
end
|
182
|
-
end
|
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
|
204
|
-
end
|