optitron 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -76,16 +76,24 @@ class Optitron
76
76
  tok.respond_to?(:name) and [name, short_name].include?(tok.name)
77
77
  end
78
78
 
79
+ def find_matching_token(tokens)
80
+ tokens.find do |t|
81
+ if t.respond_to?(:name) and (t.name == name or t.name == short_name)
82
+ t.respond_to?(:value) ^ (t.name == short_name)
83
+ end
84
+ end
85
+ end
86
+
79
87
  def consume(response, tokens)
80
- if opt_tok = tokens.find{|t| t.respond_to?(:name) and (t.name == short_name or t.name == name)}
88
+ if opt_tok = find_matching_token(tokens)
81
89
  opt_tok_index = tokens.index(opt_tok)
82
90
  tokens.delete_at(opt_tok_index)
83
91
  case @type
84
92
  when :boolean
85
93
  value = if opt_tok.respond_to?(:value)
86
94
  opt_tok.value
87
- elsif opt_tok.name == short_name and tokens[opt_tok_index].respond_to?(:val) and BOOLEAN_VALUES.include?(tokens[opt_tok_index].val)
88
- tokens.delete_at(opt_tok_index).val
95
+ elsif opt_tok.name == short_name and tokens[opt_tok_index].respond_to?(:lit) and BOOLEAN_VALUES.include?(tokens[opt_tok_index].lit)
96
+ tokens.delete_at(opt_tok_index).lit
89
97
  end
90
98
  response.params_array << [self, value.nil? ? !default : value]
91
99
  when :numeric, :string
@@ -95,8 +103,8 @@ class Optitron
95
103
  else
96
104
  response.add_error("missing", opt_tok.name)
97
105
  end
98
- elsif tokens[opt_tok_index].respond_to?(:val)
99
- tokens.delete_at(opt_tok_index).val
106
+ elsif tokens[opt_tok_index].respond_to?(:lit)
107
+ tokens.delete_at(opt_tok_index).lit
100
108
  elsif default
101
109
  default
102
110
  else
@@ -106,8 +114,8 @@ class Optitron
106
114
  when :array
107
115
  values = []
108
116
  values << opt_tok.value if opt_tok.respond_to?(:value)
109
- while tokens[opt_tok_index].respond_to?(:val)
110
- values << tokens.delete_at(opt_tok_index).val
117
+ while tokens[opt_tok_index].respond_to?(:lit)
118
+ values << tokens.delete_at(opt_tok_index).lit
111
119
  end
112
120
  response.params_array << [self, values]
113
121
  when :hash
@@ -116,8 +124,8 @@ class Optitron
116
124
  response.add_error("not in the form key:value", name) if opt_tok.value[':'].nil?
117
125
  values << opt_tok.value.split(':', 2)
118
126
  end
119
- while tokens[opt_tok_index].respond_to?(:val) and !tokens[opt_tok_index].val[':'].nil?
120
- values << tokens.delete_at(opt_tok_index).val.split(':', 2)
127
+ while tokens[opt_tok_index].respond_to?(:lit) and !tokens[opt_tok_index].lit[':'].nil?
128
+ values << tokens.delete_at(opt_tok_index).lit.split(':', 2)
121
129
  end
122
130
  response.params_array << [self, Hash[values]]
123
131
  else
@@ -153,7 +161,7 @@ class Optitron
153
161
  end
154
162
 
155
163
  def consume(response, tokens)
156
- arg_tokens = tokens.select{ |tok| tok.respond_to?(:val) }
164
+ arg_tokens = tokens.select{ |tok| tok.respond_to?(:lit) }
157
165
  if type == :greedy
158
166
  response.args_with_tokens << [self, []]
159
167
  while !arg_tokens.size.zero?
@@ -19,15 +19,15 @@ class Optitron
19
19
  options = @options
20
20
  args = @args
21
21
  if !@commands.empty?
22
- potential_cmd_toks = tokens.select { |t| t.respond_to?(:val) }
23
- if cmd_tok = potential_cmd_toks.find { |t| @commands[t.val] }
22
+ potential_cmd_toks = tokens.select { |t| t.respond_to?(:lit) }
23
+ if cmd_tok = potential_cmd_toks.find { |t| @commands[t.lit] }
24
24
  tokens.delete(cmd_tok)
25
- response.command = cmd_tok.val
26
- options += @commands[cmd_tok.val].options
27
- args = @commands[cmd_tok.val].args
25
+ response.command = cmd_tok.lit
26
+ options += @commands[cmd_tok.lit].options
27
+ args = @commands[cmd_tok.lit].args
28
28
  else
29
29
  potential_cmd_toks.first ?
30
- response.add_error('an unknown command', potential_cmd_toks.first.val) :
30
+ response.add_error('an unknown command', potential_cmd_toks.first.lit) :
31
31
  response.add_error('unknown command')
32
32
  end
33
33
  end
@@ -35,10 +35,10 @@ class Optitron
35
35
  compile_params
36
36
  @args = @args_with_tokens.map { |(arg, tok)|
37
37
  begin
38
- tok.is_a?(Array) ? tok.map{ |t| arg.validate(t.val) } : arg.validate(tok.val)
38
+ tok.is_a?(Array) ? tok.map{ |t| arg.validate(t.lit) } : arg.validate(tok.lit)
39
39
  rescue
40
40
  add_error('invalid', arg.name)
41
- tok.is_a?(Array) ? tok.map{ |t| t.val } : tok.val
41
+ tok.is_a?(Array) ? tok.map{ |t| t.lit } : tok.lit
42
42
  end
43
43
  }
44
44
  @args.flatten!
@@ -50,7 +50,7 @@ class Optitron
50
50
 
51
51
  if @errors.empty?
52
52
  @tokens.each do |token|
53
- add_error('unrecognized', token.val)
53
+ add_error('unrecognized', token.lit)
54
54
  end
55
55
  end
56
56
  end
@@ -6,7 +6,7 @@ class Optitron
6
6
  tokenize
7
7
  end
8
8
 
9
- Value = Struct.new(:val)
9
+ Value = Struct.new(:lit)
10
10
  Named = Struct.new(:name)
11
11
  NamedWithValue = Struct.new(:name, :value)
12
12
 
@@ -15,9 +15,9 @@ class Optitron
15
15
  @tokens = @opts.map {|t|
16
16
  case t
17
17
  when /^--([^=]+)=([^=]+)$/ then NamedWithValue.new($1, $2)
18
- when /^--([^=]+)$/ then Named.new($1)
18
+ when /^--([^=]+)$/ then NamedWithValue.new($1, nil)
19
19
  when /^-(.*)/ then $1.split('').map{|letter| Named.new(letter)}
20
- else Value.new(t)
20
+ else Value.new(t)
21
21
  end
22
22
  }
23
23
  @tokens.flatten!
@@ -1,3 +1,3 @@
1
1
  class Optitron
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -44,6 +44,10 @@ describe "Optitron::Parser options" do
44
44
  @parser.parse(%w(--verbose true)).valid?.should be_false
45
45
  end
46
46
 
47
+ it "shouldn't parse '--v'" do
48
+ @parser.parse(%w(--v)).valid?.should be_false
49
+ end
50
+
47
51
  it "shouldn't parse '--verbose=true'" do
48
52
  @parser.parse(%w(--verbose=true)).valid?.should be_true
49
53
  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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-16 00:00:00 -07:00
18
+ date: 2010-08-17 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency