optitron 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +11 -0
- data/lib/optitron/option.rb +4 -4
- data/lib/optitron/parser.rb +6 -3
- data/lib/optitron/response.rb +11 -7
- data/lib/optitron/version.rb +1 -1
- data/spec/errors_spec.rb +54 -0
- metadata +4 -5
- data/ideas.txt +0 -43
- data/test.rb +0 -17
data/README.rdoc
CHANGED
@@ -52,3 +52,14 @@ And <tt>@parser.parse(%w(-v install file))</tt> gives back:
|
|
52
52
|
=> ["file"]
|
53
53
|
response.params
|
54
54
|
=> {"verbose" => true}
|
55
|
+
|
56
|
+
If you try parsing invalid parameters, get back friendly error messages
|
57
|
+
|
58
|
+
@parser.parse(%w()).error_messages
|
59
|
+
=> ["Unknown command"]
|
60
|
+
@parser.parse(%w(something)).error_messages
|
61
|
+
=> ["Something is an unknown command"]
|
62
|
+
@parser.parse(%w(install)).error_messages
|
63
|
+
=> ["File is required"]
|
64
|
+
@parser.parse(%w(kill --pid=something)).error_messages
|
65
|
+
=> ["Pid is invalid"]
|
data/lib/optitron/option.rb
CHANGED
@@ -82,7 +82,7 @@ class Optitron
|
|
82
82
|
elsif default
|
83
83
|
default
|
84
84
|
else
|
85
|
-
response.add_error(opt.name
|
85
|
+
response.add_error("required", opt.name)
|
86
86
|
end
|
87
87
|
response.params_array << [self, value]
|
88
88
|
when :array
|
@@ -95,7 +95,7 @@ class Optitron
|
|
95
95
|
when :hash
|
96
96
|
values = []
|
97
97
|
if opt_tok.respond_to?(:value)
|
98
|
-
response.add_error(
|
98
|
+
response.add_error("not in the form key:value", name) if opt_tok.value[':'].nil?
|
99
99
|
values << opt_tok.value.split(':', 2)
|
100
100
|
end
|
101
101
|
while tokens[opt_tok_index].respond_to?(:val) and !tokens[opt_tok_index].val[':'].nil?
|
@@ -143,11 +143,11 @@ class Optitron
|
|
143
143
|
response.args_with_tokens.last.last << arg_tok
|
144
144
|
end
|
145
145
|
if required? and response.args_with_tokens.last.last.size.zero?
|
146
|
-
response.add_error(
|
146
|
+
response.add_error("required", name)
|
147
147
|
end
|
148
148
|
else
|
149
149
|
if arg_tokens.size.zero? and required?
|
150
|
-
response.add_error(
|
150
|
+
response.add_error("required", name)
|
151
151
|
elsif !arg_tokens.size.zero?
|
152
152
|
arg_tok = arg_tokens.shift
|
153
153
|
tokens.delete_at(tokens.index(arg_tok))
|
data/lib/optitron/parser.rb
CHANGED
@@ -11,14 +11,17 @@ class Optitron
|
|
11
11
|
response = Response.new(tokens)
|
12
12
|
options = @options
|
13
13
|
args = nil
|
14
|
-
if !@commands.empty?
|
15
|
-
|
14
|
+
if !@commands.empty?
|
15
|
+
potential_cmd_toks = tokens.select { |t| t.respond_to?(:val) }
|
16
|
+
if cmd_tok = potential_cmd_toks.find { |t| @commands[t.val] }
|
16
17
|
tokens.delete(cmd_tok)
|
17
18
|
response.command = cmd_tok.val
|
18
19
|
options += @commands[cmd_tok.val].options
|
19
20
|
args = @commands[cmd_tok.val].args
|
20
21
|
else
|
21
|
-
|
22
|
+
potential_cmd_toks.first ?
|
23
|
+
response.add_error('an unknown command', potential_cmd_toks.first.val) :
|
24
|
+
response.add_error('unknown command')
|
22
25
|
end
|
23
26
|
end
|
24
27
|
parse_options(tokens, options, response)
|
data/lib/optitron/response.rb
CHANGED
@@ -8,12 +8,16 @@ class Optitron
|
|
8
8
|
@args_with_tokens = []
|
9
9
|
@args = []
|
10
10
|
@command = nil
|
11
|
-
@errors =
|
11
|
+
@errors = []
|
12
12
|
@params = {}
|
13
13
|
end
|
14
14
|
|
15
|
-
def add_error(
|
16
|
-
@errors
|
15
|
+
def add_error(type, field = nil)
|
16
|
+
@errors << [type, field]
|
17
|
+
end
|
18
|
+
|
19
|
+
def error_messages
|
20
|
+
@errors.map{|(error, field)| field ? "#{field} is #{error}".capitalize : error.capitalize}
|
17
21
|
end
|
18
22
|
|
19
23
|
def compile_params
|
@@ -21,7 +25,7 @@ class Optitron
|
|
21
25
|
begin
|
22
26
|
params[key.name] = key.validate(value)
|
23
27
|
rescue
|
24
|
-
add_error(key.name
|
28
|
+
add_error('invalid', key.name)
|
25
29
|
params[key.name] = value
|
26
30
|
end
|
27
31
|
end
|
@@ -33,7 +37,7 @@ class Optitron
|
|
33
37
|
begin
|
34
38
|
tok.is_a?(Array) ? tok.map{ |t| arg.validate(t.val) } : arg.validate(tok.val)
|
35
39
|
rescue
|
36
|
-
add_error(arg.name
|
40
|
+
add_error('invalid', arg.name)
|
37
41
|
tok.is_a?(Array) ? tok.map{ |t| t.val } : tok.val
|
38
42
|
end
|
39
43
|
}
|
@@ -41,12 +45,12 @@ class Optitron
|
|
41
45
|
unless @tokens.empty?
|
42
46
|
@tokens.select{|t| t.respond_to?(:name)}.each do |named_token|
|
43
47
|
@tokens.delete(named_token)
|
44
|
-
add_error(named_token.name
|
48
|
+
add_error('unrecognized', named_token.name)
|
45
49
|
end
|
46
50
|
|
47
51
|
if @errors.empty?
|
48
52
|
@tokens.each do |token|
|
49
|
-
add_error(token.val
|
53
|
+
add_error('unrecognized', token.val)
|
50
54
|
end
|
51
55
|
end
|
52
56
|
end
|
data/lib/optitron/version.rb
CHANGED
data/spec/errors_spec.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Optitron::Parser errors" do
|
4
|
+
it "should fail on invalid commands" do
|
5
|
+
@parser = Optitron.new {
|
6
|
+
cmd "command"
|
7
|
+
}
|
8
|
+
response = @parser.parse(%w[])
|
9
|
+
response.valid?.should be_false
|
10
|
+
response.error_messages.should == ["Unknown command"]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should fail on missing args" do
|
14
|
+
@parser = Optitron.new {
|
15
|
+
cmd "command" do
|
16
|
+
arg "argument"
|
17
|
+
end
|
18
|
+
}
|
19
|
+
response = @parser.parse(%w[command])
|
20
|
+
response.valid?.should be_false
|
21
|
+
response.error_messages.should == ["Argument is required"]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should fail on invalid opts" do
|
25
|
+
@parser = Optitron.new {
|
26
|
+
cmd "command" do
|
27
|
+
opt "option", :type => :numeric
|
28
|
+
end
|
29
|
+
}
|
30
|
+
response = @parser.parse(%w[command --option=asd])
|
31
|
+
response.valid?.should be_false
|
32
|
+
response.error_messages.should == ["Option is invalid"]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should fail on extra args" do
|
36
|
+
@parser = Optitron.new {
|
37
|
+
cmd "command" do
|
38
|
+
arg "argument"
|
39
|
+
end
|
40
|
+
}
|
41
|
+
response = @parser.parse(%w[command argument extra-argument])
|
42
|
+
response.valid?.should be_false
|
43
|
+
response.error_messages.should == ["Extra-argument is unrecognized"]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should fail on unrecognized options" do
|
47
|
+
@parser = Optitron.new {
|
48
|
+
cmd "command"
|
49
|
+
}
|
50
|
+
response = @parser.parse(%w[command --option])
|
51
|
+
response.valid?.should be_false
|
52
|
+
response.error_messages.should == ["Option is unrecognized"]
|
53
|
+
end
|
54
|
+
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: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Joshua Hull
|
@@ -50,7 +50,6 @@ files:
|
|
50
50
|
- Gemfile
|
51
51
|
- README.rdoc
|
52
52
|
- Rakefile
|
53
|
-
- ideas.txt
|
54
53
|
- lib/optitron.rb
|
55
54
|
- lib/optitron/dsl.rb
|
56
55
|
- lib/optitron/option.rb
|
@@ -61,13 +60,13 @@ files:
|
|
61
60
|
- optitron.gemspec
|
62
61
|
- spec/arg_spec.rb
|
63
62
|
- spec/dispatch_spec.rb
|
63
|
+
- spec/errors_spec.rb
|
64
64
|
- spec/other_spec.rb
|
65
65
|
- spec/short_name_spec.rb
|
66
66
|
- spec/simple_spec.rb
|
67
67
|
- spec/spec.opts
|
68
68
|
- spec/spec_helper.rb
|
69
69
|
- spec/type_spec.rb
|
70
|
-
- test.rb
|
71
70
|
has_rdoc: true
|
72
71
|
homepage: http://rubygems.org/gems/optitron
|
73
72
|
licenses: []
|
data/ideas.txt
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
ordered params
|
2
|
-
named params
|
3
|
-
default values
|
4
|
-
|
5
|
-
Optitron::Parse
|
6
|
-
|
7
|
-
parser = Optitron::Parser.new
|
8
|
-
parser.literal('test')
|
9
|
-
parser.required(:test).integer.parse_with {
|
10
|
-
ordered(:test2)
|
11
|
-
ordered(:test3).optional.default('value')
|
12
|
-
named(:something)
|
13
|
-
}
|
14
|
-
|
15
|
-
parser.required(:test2).type(:integer).parse_with
|
16
|
-
|
17
|
-
Optitron.transform {
|
18
|
-
opt 'no-color', "Uncolored output", :short => 'c'
|
19
|
-
opt 'quiet', "Don't output anything"
|
20
|
-
cmd "install" {
|
21
|
-
desc "This is the group to install from"
|
22
|
-
arg "group", :requied => false
|
23
|
-
opt "noop"
|
24
|
-
}
|
25
|
-
cmd "list" {
|
26
|
-
arg "group", :requied => false
|
27
|
-
opt "format", 'human', :valid => ['human', 'xml', 'json'] <--- and this???
|
28
|
-
opt "depth", nil, :type => :number (allow nil is implied)
|
29
|
-
}
|
30
|
-
}
|
31
|
-
|
32
|
-
=> {:cmd => 'install', :args => {'group' => 'development}, :options => {'quiet' => true}}
|
33
|
-
|
34
|
-
Optitron.new {
|
35
|
-
...
|
36
|
-
..
|
37
|
-
}.parse( ... )
|
38
|
-
|
39
|
-
same as
|
40
|
-
|
41
|
-
Optitron.transform ?
|
42
|
-
|
43
|
-
|
data/test.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
$LOAD_PATH << 'lib'
|
2
|
-
require 'optitron'
|
3
|
-
|
4
|
-
parser = Optitron::Parser.new { parser.literal('test') }
|
5
|
-
parser.ordered('test').mandatory.match('test')
|
6
|
-
parser.ordered('test2').mandatory.match('test2')
|
7
|
-
#
|
8
|
-
#p parser.parse(['test'])
|
9
|
-
#p parser.parse(['test', 'test2'])
|
10
|
-
|
11
|
-
parser = Optitron::Parser.new
|
12
|
-
parser.ordered('test').mandatory.match('test')
|
13
|
-
parser.named('a').alias('after').string.mandatory
|
14
|
-
#p parser.parse(['test', '--after=val'])
|
15
|
-
#p parser.parse(['test', '-a', 'val'])
|
16
|
-
p parser.parse(['--after=val', 'test'])
|
17
|
-
p parser.parse(['-a', 'val', 'test'])
|