optitron 0.0.6 → 0.0.7
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/lib/optitron/option.rb +18 -10
- data/lib/optitron/parser.rb +6 -6
- data/lib/optitron/response.rb +3 -3
- data/lib/optitron/tokenizer.rb +3 -3
- data/lib/optitron/version.rb +1 -1
- data/spec/option_spec.rb +4 -0
- metadata +4 -4
data/lib/optitron/option.rb
CHANGED
@@ -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
|
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?(:
|
88
|
-
tokens.delete_at(opt_tok_index).
|
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?(:
|
99
|
-
tokens.delete_at(opt_tok_index).
|
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?(:
|
110
|
-
values << tokens.delete_at(opt_tok_index).
|
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?(:
|
120
|
-
values << tokens.delete_at(opt_tok_index).
|
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?(:
|
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?
|
data/lib/optitron/parser.rb
CHANGED
@@ -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?(:
|
23
|
-
if cmd_tok = potential_cmd_toks.find { |t| @commands[t.
|
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.
|
26
|
-
options += @commands[cmd_tok.
|
27
|
-
args = @commands[cmd_tok.
|
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.
|
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
|
data/lib/optitron/response.rb
CHANGED
@@ -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.
|
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.
|
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.
|
53
|
+
add_error('unrecognized', token.lit)
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
data/lib/optitron/tokenizer.rb
CHANGED
@@ -6,7 +6,7 @@ class Optitron
|
|
6
6
|
tokenize
|
7
7
|
end
|
8
8
|
|
9
|
-
Value = Struct.new(:
|
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
|
18
|
+
when /^--([^=]+)$/ then NamedWithValue.new($1, nil)
|
19
19
|
when /^-(.*)/ then $1.split('').map{|letter| Named.new(letter)}
|
20
|
-
else
|
20
|
+
else Value.new(t)
|
21
21
|
end
|
22
22
|
}
|
23
23
|
@tokens.flatten!
|
data/lib/optitron/version.rb
CHANGED
data/spec/option_spec.rb
CHANGED
@@ -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:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
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-
|
18
|
+
date: 2010-08-17 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|