slop 0.1.0 → 0.1.4
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 +33 -15
- data/lib/slop.rb +10 -12
- data/lib/slop/option.rb +17 -0
- data/spec/slop_spec.rb +10 -10
- metadata +4 -4
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
Slop
|
2
2
|
====
|
3
3
|
|
4
|
-
Slop is a simple option
|
4
|
+
Slop is a simple option collector with an easy to remember syntax and friendly API.
|
5
5
|
|
6
|
-
**NOTE** This software is still
|
6
|
+
**NOTE** This software is still unstable, it contains bugs and lacks core
|
7
7
|
features
|
8
8
|
|
9
9
|
Installation
|
@@ -24,28 +24,30 @@ Usage
|
|
24
24
|
-----
|
25
25
|
|
26
26
|
s = Slop.parse(ARGV) do
|
27
|
-
option
|
28
|
-
option
|
29
|
-
option
|
27
|
+
option(:v, :verbose, "Enable verbose mode", :default => false)
|
28
|
+
option(:n, :name, "Your name", true) # compulsory argument
|
29
|
+
option(:c, :country, "Your country", argument => true) # the same thing
|
30
30
|
|
31
|
-
option
|
32
|
-
option
|
31
|
+
option(:a, :age, "Your age", true, :optional => true) # optional argument
|
32
|
+
option(:address, "Your address", :optional => true) # the same
|
33
33
|
|
34
34
|
# shortcut option aliases
|
35
|
-
opt
|
36
|
-
o
|
35
|
+
opt(:height, "Your height")
|
36
|
+
o(:weight, "Your weight)
|
37
37
|
end
|
38
38
|
|
39
39
|
# using `--name Lee -a 100`
|
40
40
|
s.options_hash #=> {:verbose=>false, :name=>"Lee", :age=>"100", :address=>nil}
|
41
41
|
s.value_for(:name) #=> "Lee"
|
42
|
+
|
43
|
+
# or grab the Option object directly
|
42
44
|
option = s.option_for(:name)
|
43
45
|
option.description #=> "Your name"
|
44
46
|
|
45
47
|
# You can also use switch values to set options according to arguments
|
46
48
|
s = Slop.parse(ARGV) do
|
47
|
-
option
|
48
|
-
option
|
49
|
+
option(:v, :verbose, :default => false, :switch => true)
|
50
|
+
option(:applicable_age, :default => 10, :switch => 20)
|
49
51
|
end
|
50
52
|
|
51
53
|
# without `-v`
|
@@ -57,6 +59,22 @@ Usage
|
|
57
59
|
# using `--applicable_age`
|
58
60
|
s.value_for(:applicable_age) #=> 20
|
59
61
|
|
62
|
+
Callbacks
|
63
|
+
---------
|
64
|
+
|
65
|
+
If you'd like to trigger an event when an option is used, you can pass a
|
66
|
+
block to your option. Here's how:
|
67
|
+
|
68
|
+
Slop.parse(ARGV) do
|
69
|
+
opt(:v, :version, "Display the version") do
|
70
|
+
puts "Version 1.0.1"
|
71
|
+
exit
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
Now when using the `--version` option on the command line, the trigger will
|
76
|
+
be called and its contents executed.
|
77
|
+
|
60
78
|
Casting
|
61
79
|
-------
|
62
80
|
|
@@ -64,7 +82,7 @@ If you want to return values of specific types, for example a Symbol or Integer
|
|
64
82
|
you can pass the `:as` attribute to your option.
|
65
83
|
|
66
84
|
s = Slop.parse("--age 20") do
|
67
|
-
opt
|
85
|
+
opt(:age, true, :as => Integer) # :int/:integer both also work
|
68
86
|
end
|
69
87
|
s.value_for(:age) #=> 20 # not "20"
|
70
88
|
|
@@ -72,7 +90,7 @@ Slop will also check your default attributes type to see if it can cast the new
|
|
72
90
|
value to the same type.
|
73
91
|
|
74
92
|
s = Slop.parse("--port 110") do
|
75
|
-
opt
|
93
|
+
opt(port, true, :default => 80)
|
76
94
|
end
|
77
95
|
s.value_for(:port) #=> 110
|
78
96
|
|
@@ -82,14 +100,14 @@ Lists
|
|
82
100
|
You can of course also parse lists into options. Here's how:
|
83
101
|
|
84
102
|
s = Slop.parse("--people lee,injekt") do
|
85
|
-
opt
|
103
|
+
opt(:people, true, :as => Array)
|
86
104
|
end
|
87
105
|
s.value_for(:people) #=> ["lee", "injekt"]
|
88
106
|
|
89
107
|
You can also change both the split delimiter and limit
|
90
108
|
|
91
109
|
s = Slop.parse("--people lee:injekt:bob") do
|
92
|
-
opt
|
110
|
+
opt(:people, true, :as => Array, :delimiter => ':', :limit => 2)
|
93
111
|
end
|
94
112
|
s.value_for(:people) #=> ["lee", "injekt:bob"]
|
95
113
|
|
data/lib/slop.rb
CHANGED
@@ -3,13 +3,15 @@ require 'set'
|
|
3
3
|
require 'slop/option'
|
4
4
|
|
5
5
|
class Slop
|
6
|
-
VERSION = '0.1.
|
6
|
+
VERSION = '0.1.4'
|
7
7
|
|
8
|
+
# Raised when an option expects an argument and none is given
|
8
9
|
class MissingArgumentError < ArgumentError; end
|
9
10
|
|
10
11
|
# @return [Set]
|
11
12
|
attr_reader :options
|
12
13
|
|
14
|
+
# Sugar for new(..).parse(stuff)
|
13
15
|
def self.parse(values=[], &blk)
|
14
16
|
new(&blk).parse(values)
|
15
17
|
end
|
@@ -40,7 +42,7 @@ class Slop
|
|
40
42
|
options = Hash[attributes.zip(args)]
|
41
43
|
options.merge!(opts)
|
42
44
|
options[:as] = options[:default].class if options.key?(:default)
|
43
|
-
|
45
|
+
options[:callback] = blk if block_given?
|
44
46
|
|
45
47
|
@options << Option.new(options)
|
46
48
|
end
|
@@ -67,9 +69,8 @@ class Slop
|
|
67
69
|
next unless option = option_for(opt) # skip unknown values for now
|
68
70
|
index = values.index(value)
|
69
71
|
|
70
|
-
if option.
|
71
|
-
|
72
|
-
end
|
72
|
+
option.execute_callback if option.has_callback?
|
73
|
+
option.switch_argument_value if option.has_switch?
|
73
74
|
|
74
75
|
if option.requires_argument?
|
75
76
|
value = values.at(index + 1)
|
@@ -93,11 +94,6 @@ class Slop
|
|
93
94
|
self
|
94
95
|
end
|
95
96
|
|
96
|
-
# traverse options
|
97
|
-
def each_option
|
98
|
-
@options.each {|o| yield o }
|
99
|
-
end
|
100
|
-
|
101
97
|
# A simple Hash of options with option labels or flags as keys
|
102
98
|
# and option values as.. values.
|
103
99
|
#
|
@@ -112,12 +108,13 @@ class Slop
|
|
112
108
|
out
|
113
109
|
end
|
114
110
|
alias :to_hash :options_hash
|
111
|
+
alias :to_h :options_hash
|
115
112
|
|
116
113
|
# Find an option using its flag or label
|
117
114
|
#
|
118
115
|
# @example
|
119
116
|
# s = Slop.new do
|
120
|
-
# option
|
117
|
+
# option(:n, :name, "Your name")
|
121
118
|
# end
|
122
119
|
#
|
123
120
|
# s.option_for(:name).description #=> "Your name"
|
@@ -134,7 +131,7 @@ class Slop
|
|
134
131
|
#
|
135
132
|
# @example When passing --name Lee
|
136
133
|
# s = Slop.new do
|
137
|
-
# option
|
134
|
+
# option(:n, :name, true)
|
138
135
|
# end
|
139
136
|
#
|
140
137
|
# s.value_for(:name) #=> "Lee"
|
@@ -144,4 +141,5 @@ class Slop
|
|
144
141
|
return unless option = option_for(flag)
|
145
142
|
option.argument_value
|
146
143
|
end
|
144
|
+
alias :[] :value_for
|
147
145
|
end
|
data/lib/slop/option.rb
CHANGED
@@ -6,6 +6,7 @@ class Slop
|
|
6
6
|
attr_reader :description
|
7
7
|
attr_reader :argument_value
|
8
8
|
attr_reader :default
|
9
|
+
attr_reader :callback
|
9
10
|
|
10
11
|
def initialize(options={}, &blk)
|
11
12
|
@options = options
|
@@ -17,6 +18,7 @@ class Slop
|
|
17
18
|
@argument ||= @optional
|
18
19
|
@default = options[:default]
|
19
20
|
@as = options[:as]
|
21
|
+
@callback = options[:callback]
|
20
22
|
|
21
23
|
# Array properties
|
22
24
|
@delimiter = options[:delimiter] || ','
|
@@ -68,6 +70,16 @@ class Slop
|
|
68
70
|
!!@options[:switch]
|
69
71
|
end
|
70
72
|
|
73
|
+
# @return [Boolean] true if the option has a callback
|
74
|
+
def has_callback?
|
75
|
+
!!@callback
|
76
|
+
end
|
77
|
+
|
78
|
+
# execute this options callback
|
79
|
+
def execute_callback
|
80
|
+
@callback.call if has_callback?
|
81
|
+
end
|
82
|
+
|
71
83
|
# does the option require an argument?
|
72
84
|
# @return [Boolean]
|
73
85
|
def requires_argument?
|
@@ -80,6 +92,11 @@ class Slop
|
|
80
92
|
@options[:optional]
|
81
93
|
end
|
82
94
|
|
95
|
+
# @return
|
96
|
+
def [](key)
|
97
|
+
@options[key]
|
98
|
+
end
|
99
|
+
|
83
100
|
# Replace options argument value with the switch value supplied, used
|
84
101
|
# when supplying the `switch` option making switch flags easy to alter
|
85
102
|
#
|
data/spec/slop_spec.rb
CHANGED
@@ -14,21 +14,20 @@ describe Slop do
|
|
14
14
|
end.should be_kind_of(Slop::Option)
|
15
15
|
end
|
16
16
|
|
17
|
-
it "adds an option with a block to alter option attributes" do
|
18
|
-
s = Slop.new do
|
19
|
-
option :n, :name, "Set your name!", true do |o|
|
20
|
-
o[:default] = "Lee"
|
21
|
-
end
|
22
|
-
end
|
23
|
-
s.option_for(:name).default.should == "Lee"
|
24
|
-
end
|
25
|
-
|
26
17
|
it "takes no more than 4 arguments" do
|
27
18
|
lambda do
|
28
19
|
Slop.new { option :a, :b, :c, :d, :e }
|
29
20
|
end.should raise_error(ArgumentError, "Argument size must be no more than 4")
|
30
21
|
end
|
31
22
|
|
23
|
+
it "accepts a block which assigns an option callback" do
|
24
|
+
s = Slop.parse("-v") do
|
25
|
+
opt(:v, :version, "Display version") { "Version 1" }
|
26
|
+
end
|
27
|
+
s.option_for(:version).callback.should be_kind_of(Proc)
|
28
|
+
s.option_for(:version).callback.call.should == "Version 1"
|
29
|
+
end
|
30
|
+
|
32
31
|
it "does not parse option values unless option.argument is true" do
|
33
32
|
Slop.parse("--name Lee") { opt :name }.value_for(:name).should be_nil
|
34
33
|
Slop.parse("--name Lee") { opt :name, true }.value_for(:name).should == "Lee"
|
@@ -46,7 +45,7 @@ describe Slop do
|
|
46
45
|
end
|
47
46
|
end
|
48
47
|
|
49
|
-
describe "value_for" do
|
48
|
+
describe "value_for/[]" do
|
50
49
|
it "returns the value of an option" do
|
51
50
|
s = Slop.parse("--name Lee") do
|
52
51
|
opt :n, :name, "Your name", true
|
@@ -56,6 +55,7 @@ describe Slop do
|
|
56
55
|
|
57
56
|
it "returns a default option if none is given" do
|
58
57
|
Slop.new { opt :name, true, :default => "Lee" }.value_for(:name).should == "Lee"
|
58
|
+
Slop.new { opt :name, true, :default => "Lee" }[:name].should == "Lee"
|
59
59
|
end
|
60
60
|
|
61
61
|
it "returns nil if an option does not exist" do
|
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
|
+
- 4
|
9
|
+
version: 0.1.4
|
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-11-
|
17
|
+
date: 2010-11-30 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -80,6 +80,6 @@ rubyforge_project:
|
|
80
80
|
rubygems_version: 1.3.7
|
81
81
|
signing_key:
|
82
82
|
specification_version: 3
|
83
|
-
summary: Option
|
83
|
+
summary: Option gathering made easy
|
84
84
|
test_files: []
|
85
85
|
|