optitron 0.0.3 → 0.0.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.
@@ -14,6 +14,11 @@ class Optitron
14
14
  interpolate_type(default)
15
15
  @default = default
16
16
  end
17
+
18
+ def inclusion_test=(tester)
19
+ interpolate_type(tester.first)
20
+ @inclusion_test = tester
21
+ end
17
22
 
18
23
  def interpolate_type(default)
19
24
  @type = case default
@@ -31,7 +36,7 @@ class Optitron
31
36
  end
32
37
 
33
38
  def validate(val)
34
- case @type
39
+ validated_type = case @type
35
40
  when :boolean
36
41
  if TRUE_BOOLEAN_VALUES.include?(val)
37
42
  true
@@ -51,6 +56,8 @@ class Optitron
51
56
  else
52
57
  raise
53
58
  end
59
+ @inclusion_test.include?(validated_type) or raise if @inclusion_test
60
+ validated_type
54
61
  end
55
62
 
56
63
  class Opt < Option
@@ -61,6 +68,7 @@ class Optitron
61
68
  end
62
69
  @name, @desc = name, desc
63
70
  @type = opts && opts[:type] || :boolean
71
+ self.inclusion_test = opts[:in] if opts && opts[:in]
64
72
  self.default = opts && opts.key?(:default) ? opts[:default] : (@type == :boolean ? false : nil)
65
73
  end
66
74
 
@@ -80,15 +88,19 @@ class Optitron
80
88
  tokens.delete_at(opt_tok_index).val
81
89
  end
82
90
  response.params_array << [self, value.nil? ? !default : value]
83
- when :numeric
84
- value = if opt_tok.name == name and opt_tok.respond_to?(:value)
85
- opt_tok.value
91
+ when :numeric, :string
92
+ value = if opt_tok.name == name
93
+ if opt_tok.respond_to?(:value)
94
+ opt_tok.value
95
+ else
96
+ response.add_error("missing", opt_tok.name)
97
+ end
86
98
  elsif tokens[opt_tok_index].respond_to?(:val)
87
99
  tokens.delete_at(opt_tok_index).val
88
100
  elsif default
89
101
  default
90
102
  else
91
- response.add_error("required", opt.name)
103
+ response.add_error("required", opt_tok.name)
92
104
  end
93
105
  response.params_array << [self, value]
94
106
  when :array
@@ -128,13 +140,14 @@ class Optitron
128
140
  end
129
141
 
130
142
  class Arg < Option
131
- attr_accessor :greedy
143
+ attr_accessor :greedy, :inclusion_test
132
144
  alias_method :greedy?, :greedy
133
145
  def initialize(name = nil, desc = nil, opts = nil)
134
146
  if desc.is_a?(Hash)
135
147
  desc, opts = nil, desc
136
148
  end
137
149
  @name, @desc = name, desc
150
+ self.inclusion_test = opts[:in] if opts && opts[:in]
138
151
  @required = opts && opts.key?(:required) ? opts[:required] : true
139
152
  @type = opts && opts[:type]
140
153
  end
@@ -1,3 +1,3 @@
1
1
  class Optitron
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Optitron::Parser options" do
4
+ context "long names vs short names" do
5
+ before(:each) do
6
+ @parser = Optitron.new {
7
+ opt "option", :type => :string
8
+ }
9
+ end
10
+
11
+ it "should parse '-o test'" do
12
+ @parser.parse(%w(-o test)).params.should == {'option' => 'test'}
13
+ end
14
+
15
+ it "should parse '--option=test'" do
16
+ @parser.parse(%w(--option=test)).params.should == {'option' => 'test'}
17
+ end
18
+
19
+ it "shouldn't parse '--option test'" do
20
+ @parser.parse(%w(--option test)).valid?.should be_false
21
+ end
22
+
23
+ it "shouldn't parse '-option test'" do
24
+ @parser.parse(%w(-option test)).valid?.should be_false
25
+ end
26
+ end
27
+
28
+ context "multiple options" do
29
+ before(:each) do
30
+ @parser = Optitron.new {
31
+ opt "1"
32
+ opt "2"
33
+ opt "3"
34
+ opt "option", :type => :string
35
+ }
36
+ end
37
+
38
+ it "should parse '-123o test'" do
39
+ @parser.parse(%w(-123o test)).params.should == {'option' => 'test', '1' => true, '2' => true, '3' => true}
40
+ end
41
+ end
42
+ end
@@ -102,4 +102,29 @@ describe "Optitron::Parser types" do
102
102
  end
103
103
  end
104
104
  end
105
+
106
+ context "inclusion" do
107
+ before(:each) {
108
+ @parser = Optitron.new {
109
+ opt 'pid', :in => [1, 2, 3, 4]
110
+ opt 'range', :in => 0...100
111
+ }
112
+ }
113
+
114
+ it "should parse 'parse --pid=3'" do
115
+ @parser.parse(%w(--pid=3)).params.should == {'pid' => 3}
116
+ end
117
+
118
+ it "should parse 'parse --range=56'" do
119
+ @parser.parse(%w(--range=56)).params.should == {'range' => 56}
120
+ end
121
+
122
+ it "shouldn't parse 'parse --range=156'" do
123
+ @parser.parse(%w(--range=156)).valid?.should be_false
124
+ end
125
+
126
+ it "shouldn't parse 'parse --pid=9'" do
127
+ @parser.parse(%w(--pid=9)).valid?.should be_false
128
+ end
129
+ end
105
130
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optitron
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -62,12 +62,13 @@ files:
62
62
  - spec/default_spec.rb
63
63
  - spec/dispatch_spec.rb
64
64
  - spec/errors_spec.rb
65
+ - spec/option_spec.rb
65
66
  - spec/other_spec.rb
66
67
  - spec/short_name_spec.rb
67
68
  - spec/simple_spec.rb
68
69
  - spec/spec.opts
69
70
  - spec/spec_helper.rb
70
- - spec/type_spec.rb
71
+ - spec/validation_spec.rb
71
72
  has_rdoc: true
72
73
  homepage: http://rubygems.org/gems/optitron
73
74
  licenses: []