slop 0.1.4 → 0.1.5

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 CHANGED
@@ -26,7 +26,7 @@ Usage
26
26
  s = Slop.parse(ARGV) do
27
27
  option(:v, :verbose, "Enable verbose mode", :default => false)
28
28
  option(:n, :name, "Your name", true) # compulsory argument
29
- option(:c, :country, "Your country", argument => true) # the same thing
29
+ option(:c, :country, "Your country", :argument => true) # the same thing
30
30
 
31
31
  option(:a, :age, "Your age", true, :optional => true) # optional argument
32
32
  option(:address, "Your address", :optional => true) # the same
data/lib/slop/option.rb CHANGED
@@ -1,13 +1,32 @@
1
1
  class Slop
2
2
  class Option
3
3
 
4
+ # @return [Symbol,#to_s]
4
5
  attr_reader :flag
6
+
7
+ # @return [Symbol,#to_s]
5
8
  attr_reader :option
9
+
10
+ # @return [String]
6
11
  attr_reader :description
7
- attr_reader :argument_value
12
+
13
+ # @return [Object]
8
14
  attr_reader :default
15
+
16
+ # @return [Proc]
9
17
  attr_reader :callback
10
18
 
19
+ # @param [Hash] options Option attributes
20
+ # @option options [Symbol,#to_s] :flag
21
+ # @option options [Symbol,#to_s] :option
22
+ # @option options [Symbol,#to_s] :description
23
+ # @option options [Boolean] :argument
24
+ # @option options [Boolean] :optional
25
+ # @option options [Object] :default
26
+ # @option options [Object] :as
27
+ # @option options [Proc] :callback
28
+ # @option options [String,#to_s] :delimiter
29
+ # @option options [Integer] :limit
11
30
  def initialize(options={}, &blk)
12
31
  @options = options
13
32
  @flag = options[:flag]
@@ -22,7 +41,7 @@ class Slop
22
41
 
23
42
  # Array properties
24
43
  @delimiter = options[:delimiter] || ','
25
- @limit = options[:limit] || 1
44
+ @limit = options[:limit] || 0
26
45
 
27
46
  @argument_value = nil
28
47
  end
@@ -106,7 +125,7 @@ class Slop
106
125
  # Now when the `-v` or `--verbose` option is supplied, verbose will
107
126
  # be set to `true`, rather than the default `false` option
108
127
  def switch_argument_value
109
- @argument_value = @option[:switch]
128
+ @argument_value = @options[:switch]
110
129
  end
111
130
 
112
131
  # return a key for an option, prioritize
data/lib/slop.rb CHANGED
@@ -3,7 +3,7 @@ require 'set'
3
3
  require 'slop/option'
4
4
 
5
5
  class Slop
6
- VERSION = '0.1.4'
6
+ VERSION = '0.1.5'
7
7
 
8
8
  # Raised when an option expects an argument and none is given
9
9
  class MissingArgumentError < ArgumentError; end
@@ -11,6 +11,11 @@ class Slop
11
11
  # @return [Set]
12
12
  attr_reader :options
13
13
 
14
+ # @return [Set] the last set of options used
15
+ def self.options
16
+ @@options
17
+ end
18
+
14
19
  # Sugar for new(..).parse(stuff)
15
20
  def self.parse(values=[], &blk)
16
21
  new(&blk).parse(values)
@@ -18,6 +23,7 @@ class Slop
18
23
 
19
24
  def initialize(&blk)
20
25
  @options = Set.new
26
+ @@options = @options
21
27
  instance_eval(&blk) if block_given?
22
28
  end
23
29
 
@@ -33,10 +39,8 @@ class Slop
33
39
  args.unshift nil if args.first.size > 1
34
40
  args.push nil if args.size == 2
35
41
  args.push false if args.size == 3
36
- if args[2] == true
37
- args[2] = nil
38
- args[3] = true
39
- end
42
+
43
+ args[2..3] = [nil, true] if args[2] == true
40
44
 
41
45
  attributes = [:flag, :option, :description, :argument]
42
46
  options = Hash[attributes.zip(args)]
@@ -60,15 +64,17 @@ class Slop
60
64
  # Parse an Array (usually ARGV) of options
61
65
  #
62
66
  # @param [Array, #split] Array or String of options to parse
67
+ # @raise [MissingArgumentError] raised when a compulsory argument is missing
63
68
  def parse(values=[])
64
69
  values = values.split(/\s+/) if values.respond_to?(:split)
65
70
 
66
71
  values.each do |value|
67
- if value[1] == '-' or value[0] == '-'
68
- opt = value.size == 2 ? value[1] : value[2..-1]
69
- next unless option = option_for(opt) # skip unknown values for now
72
+ if flag_or_option?(value)
73
+ opt = value.size == 2 ? value[1] : value[2..-1]
70
74
  index = values.index(value)
71
75
 
76
+ next unless option = option_for(opt) # skip unknown values for now
77
+
72
78
  option.execute_callback if option.has_callback?
73
79
  option.switch_argument_value if option.has_switch?
74
80
 
@@ -76,13 +82,13 @@ class Slop
76
82
  value = values.at(index + 1)
77
83
 
78
84
  unless option.optional_argument?
79
- if not value or value[0] == '-' or value[1] == '-'
85
+ if not value or flag_or_option?(value)
80
86
  raise MissingArgumentError,
81
87
  "#{option.key} requires a compulsory argument, none given"
82
88
  end
83
89
  end
84
90
 
85
- unless !value or value[0] == '-' or value[1] == '-'
91
+ unless not value or flag_or_option?(value)
86
92
  option.argument_value = values.delete_at(values.index(value))
87
93
  end
88
94
  end
@@ -142,4 +148,11 @@ class Slop
142
148
  option.argument_value
143
149
  end
144
150
  alias :[] :value_for
151
+
152
+ private
153
+
154
+ def flag_or_option?(flag)
155
+ return unless flag
156
+ flag[1, 2].include?('-')
157
+ end
145
158
  end
data/spec/option_spec.rb CHANGED
@@ -2,4 +2,144 @@ require File.expand_path('../../lib/slop', __FILE__)
2
2
 
3
3
  describe Slop::Option do
4
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 delimited" 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
5
145
  end
data/spec/slop_spec.rb CHANGED
@@ -7,6 +7,15 @@ describe Slop do
7
7
  end
8
8
  end
9
9
 
10
+ describe "::options" do
11
+ it "should return the last set of options" do
12
+ s = Slop.new { option(:f, :foo, "foo") }
13
+ Slop.options.should == s.options
14
+ p = Slop.new { option(:b, :bar, "bar") }
15
+ Slop.options.should == p.options
16
+ end
17
+ end
18
+
10
19
  describe "option" do
11
20
  it "adds an option" do
12
21
  @slop.options.find do |opt|
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 5
9
+ version: 0.1.5
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-30 00:00:00 +00:00
17
+ date: 2010-12-02 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency