optitron 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  pkg/*
2
- *.gem
2
+ *.gem
3
+ .bundle
data/Gemfile.lock CHANGED
@@ -1,16 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- optitron (0.2.1)
4
+ optitron (0.3.0)
5
5
  callsite (~> 0.0.4)
6
- ruby2ruby (~> 1.2.4)
7
- ruby_parser (~> 2.0)
8
- sexp_processor (~> 3.0.4)
6
+ method-args (~> 0.1.0)
9
7
 
10
8
  GEM
11
9
  remote: http://rubygems.org/
12
10
  specs:
13
11
  callsite (0.0.4)
12
+ method-args (0.1.0)
13
+ ruby2ruby (~> 1.2.4)
14
+ ruby_parser (~> 2.0)
15
+ sexp_processor (~> 3.0.4)
16
+ phocus (1.1)
14
17
  rake (0.8.7)
15
18
  rspec (1.3.0)
16
19
  ruby2ruby (1.2.5)
@@ -26,9 +29,8 @@ PLATFORMS
26
29
  DEPENDENCIES
27
30
  bundler (~> 1.0.0)
28
31
  callsite (~> 0.0.4)
32
+ method-args (~> 0.1.0)
29
33
  optitron!
34
+ phocus
30
35
  rake
31
36
  rspec
32
- ruby2ruby (~> 1.2.4)
33
- ruby_parser (~> 2.0)
34
- sexp_processor (~> 3.0.4)
@@ -1,94 +1,8 @@
1
1
  require 'callsite'
2
- require 'ruby2ruby'
3
- require 'ruby_parser'
4
- require 'sexp_processor'
2
+ require 'method_args'
5
3
 
6
4
  class Optitron
7
5
 
8
- class MethodArgs < SexpProcessor
9
- attr_reader :method_map
10
- def initialize(cls)
11
- @cls = cls
12
- @method_map = {}
13
- @current_class = []
14
- super()
15
- end
16
-
17
- def process_module(exp)
18
- exp.shift
19
- @current_class << exp.first.to_sym
20
- process(exp)
21
- @current_class.pop
22
- exp.clear
23
- exp
24
- end
25
-
26
- def process_class(exp)
27
- exp.shift
28
- current_class_size = @current_class.size
29
- case exp.first
30
- when Symbol
31
- @current_class << exp.first.to_sym
32
- process(exp)
33
- else
34
- if exp.first.first == :colon2
35
- exp.first.shift
36
- class_exp = exp.shift
37
- class_exp[0, class_exp.size - 1].each do |const|
38
- @current_class << const.last
39
- end
40
- @current_class << class_exp.last
41
- else
42
- raise
43
- end
44
- exp.shift
45
- process(exp.first)
46
- end
47
- @current_class.slice!(current_class_size, @current_class.size)
48
- exp.clear
49
- exp
50
- end
51
-
52
- def process_defn(exp)
53
- exp.shift
54
- @current_method = exp.shift
55
- @ruby2ruby = Ruby2Ruby.new
56
- process_args(exp.shift)
57
- scope = exp.shift
58
- exp
59
- end
60
-
61
- def process_args(exp)
62
- exp.shift
63
- arg_list = []
64
- while !exp.empty?
65
- t = exp.shift
66
- case t
67
- when Symbol
68
- arg_list << if t.to_s[0] == ?*
69
- [t.to_s[1, t.to_s.size].to_sym, :greedy]
70
- else
71
- [t, :required]
72
- end
73
- when Sexp
74
- case t.shift
75
- when :block
76
- lasgn = t.shift
77
- lasgn.shift
78
- name = lasgn.shift
79
- sub_part = arg_list.find{|arg| arg.first == name}
80
- sub_part.clear
81
- sub_part << name
82
- sub_part << :optional
83
- sub_part << @ruby2ruby.process(lasgn.last)
84
- end
85
- end
86
- end
87
- @cls
88
- @method_map[@current_method] = arg_list if @cls.name == @current_class.map{|c| c.to_s}.join('::')
89
- end
90
- end
91
-
92
6
  module ClassDsl
93
7
 
94
8
  def self.included(o)
@@ -99,7 +13,6 @@ class Optitron
99
13
  end"
100
14
  end
101
15
 
102
-
103
16
  module ClassMethods
104
17
  def method_added(m)
105
18
  if @last_desc
@@ -112,6 +25,7 @@ class Optitron
112
25
  @last_group = nil
113
26
  end
114
27
  end
28
+
115
29
  def optitron_parser
116
30
  send(:class_variable_set, :@@optitron_parser, Optitron::Parser.new) unless send(:class_variable_defined?, :@@optitron_parser)
117
31
  send(:class_variable_get, :@@optitron_parser)
@@ -122,21 +36,6 @@ class Optitron
122
36
  send(:class_variable_get, :@@optitron_dsl)
123
37
  end
124
38
 
125
- def method_args
126
- send(:class_variable_get, :@@method_args)
127
- end
128
-
129
- def build_method_args(file)
130
- unless send(:class_variable_defined?, :@@method_args)
131
- parser = RubyParser.new
132
- sexp = parser.process(File.read(file))
133
- method_args = MethodArgs.new(self)
134
- method_args.process(sexp)
135
- send(:class_variable_set, :@@method_args, method_args.method_map)
136
- end
137
- send(:class_variable_get, :@@method_args)
138
- end
139
-
140
39
  def class_opt(name, desc = nil, opts = nil)
141
40
  optitron_dsl.root.opt(name, desc, opts)
142
41
  end
@@ -146,7 +45,7 @@ class Optitron
146
45
  end
147
46
 
148
47
  def desc(desc)
149
- build_method_args(Callsite.parse(caller.first).filename)
48
+ MethodArgs.register(Callsite.parse(caller.first).filename)
150
49
  @last_desc = desc
151
50
  end
152
51
 
@@ -163,29 +62,32 @@ class Optitron
163
62
  @arg_types = types
164
63
  end
165
64
 
166
- def build
65
+ def build(&blk)
167
66
  unless @built
67
+ optitron_parser.target = blk ? blk.call : new
168
68
  target = optitron_parser.target
169
69
  optitron_dsl.root.help unless send(:class_variable_defined?, :@@suppress_help)
170
70
  @cmds.each do |(cmd_name, cmd_desc, opts)|
171
- args = method_args[cmd_name.to_sym]
172
- arity = instance_method(cmd_name).arity
71
+ method = target.class.instance_method(cmd_name.to_sym).bind(target)
173
72
  arg_types = @arg_types
174
73
  optitron_dsl.root.cmd(cmd_name, cmd_desc) do
175
74
  opts.each { |o| opt *o }
176
- args.each do |(arg_name, arg_type, arg_default)|
177
- possible_arg_type = arg_name.to_s[/_(string|hash|array|numeric|int|float)$/, 1]
178
- if possible_arg_type && (arg_types.nil? || !arg_types.first)
75
+ puts "!!! #{cmd_name.inspect}" if method.nil?
76
+ method.args.each do |arg|
77
+ possible_arg_type = arg.name.to_s[/_(string|hash|array|numeric|int|float)$/, 1]
78
+ arg_name = if possible_arg_type && (arg_types.nil? || !arg_types.first)
179
79
  possible_arg_type = possible_arg_type.to_sym
180
- arg_name = arg_name.to_s[/^(.*)_(?:string|hash|array|numeric|int|float)$/, 1]
80
+ arg.name.to_s[/^(.*)_(?:string|hash|array|numeric|int|float)$/, 1]
81
+ else
82
+ arg.name
181
83
  end
182
- arg_opts = { :default => arg_default && target.instance_eval(arg_default), :type => arg_types && arg_types.shift || possible_arg_type }
183
- case arg_type
84
+ arg_opts = { :default => arg.default_value, :type => arg_types && arg_types.shift || possible_arg_type }
85
+ case arg.type
184
86
  when :required
185
87
  arg arg_name.to_s, arg_opts
186
88
  when :optional
187
89
  arg arg_name.to_s, arg_opts.merge(:required => false)
188
- when :greedy
90
+ when :splat
189
91
  arg arg_name.to_s, arg_opts.merge(:type => :greedy)
190
92
  end
191
93
  end
@@ -197,8 +99,7 @@ class Optitron
197
99
  end
198
100
 
199
101
  def dispatch(args = ARGV, &blk)
200
- optitron_parser.target = blk ? blk.call : new
201
- build
102
+ build(&blk)
202
103
  response = optitron_parser.parse(args)
203
104
  if response.valid?
204
105
  optitron_parser.target.params = response.params
@@ -1,3 +1,3 @@
1
1
  class Optitron
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
data/optitron.gemspec CHANGED
@@ -15,13 +15,12 @@ Gem::Specification.new do |s|
15
15
  s.required_rubygems_version = ">= 1.3.6"
16
16
  s.rubyforge_project = "optitron"
17
17
 
18
- s.add_dependency "ruby_parser", "~> 2.0"
19
18
  s.add_dependency "callsite", "~> 0.0.4"
20
- s.add_dependency "ruby2ruby", "~> 1.2.4"
21
- s.add_dependency "sexp_processor", "~> 3.0.4"
19
+ s.add_dependency "method-args", "~> 0.1.0"
22
20
  s.add_development_dependency "bundler", "~> 1.0.0"
23
21
  s.add_development_dependency "rake"
24
22
  s.add_development_dependency "rspec"
23
+ s.add_development_dependency "phocus"
25
24
 
26
25
  s.files = `git ls-files`.split("\n")
27
26
  s.require_path = 'lib'
data/spec/cli_spec.rb CHANGED
@@ -110,7 +110,7 @@ describe "Optitron::Parser defaults" do
110
110
  end
111
111
 
112
112
  it "should generate the correct help" do
113
- AnotherCLIExample.build
113
+ AnotherCLIExample.build { AnotherCLIExample.new("test") }
114
114
  AnotherCLIExample.optitron_parser.help.strip.should == "Commands\n\nuse_too [one] <two=\"three\"> # Use this too\n -a/--another_opt \n\nGlobal options\n\n-v/--verbose \n-?/--help # Print help message"
115
115
  end
116
116
 
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
2
  require 'optitron'
3
+ require 'phocus'
3
4
 
4
5
  class Object
5
6
  def capture(stream)
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hull
@@ -15,28 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-13 00:00:00 -08:00
18
+ date: 2011-01-06 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: ruby_parser
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 2
32
- - 0
33
- version: "2.0"
34
- type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: callsite
38
- prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
40
23
  none: false
41
24
  requirements:
42
25
  - - ~>
@@ -47,44 +30,28 @@ dependencies:
47
30
  - 0
48
31
  - 4
49
32
  version: 0.0.4
50
- type: :runtime
51
- version_requirements: *id002
52
- - !ruby/object:Gem::Dependency
53
- name: ruby2ruby
33
+ requirement: *id001
34
+ name: callsite
54
35
  prerelease: false
55
- requirement: &id003 !ruby/object:Gem::Requirement
56
- none: false
57
- requirements:
58
- - - ~>
59
- - !ruby/object:Gem::Version
60
- hash: 23
61
- segments:
62
- - 1
63
- - 2
64
- - 4
65
- version: 1.2.4
66
36
  type: :runtime
67
- version_requirements: *id003
68
37
  - !ruby/object:Gem::Dependency
69
- name: sexp_processor
70
- prerelease: false
71
- requirement: &id004 !ruby/object:Gem::Requirement
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
72
39
  none: false
73
40
  requirements:
74
41
  - - ~>
75
42
  - !ruby/object:Gem::Version
76
- hash: 15
43
+ hash: 27
77
44
  segments:
78
- - 3
79
45
  - 0
80
- - 4
81
- version: 3.0.4
46
+ - 1
47
+ - 0
48
+ version: 0.1.0
49
+ requirement: *id002
50
+ name: method-args
51
+ prerelease: false
82
52
  type: :runtime
83
- version_requirements: *id004
84
53
  - !ruby/object:Gem::Dependency
85
- name: bundler
86
- prerelease: false
87
- requirement: &id005 !ruby/object:Gem::Requirement
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
88
55
  none: false
89
56
  requirements:
90
57
  - - ~>
@@ -95,12 +62,12 @@ dependencies:
95
62
  - 0
96
63
  - 0
97
64
  version: 1.0.0
65
+ requirement: *id003
66
+ name: bundler
67
+ prerelease: false
98
68
  type: :development
99
- version_requirements: *id005
100
69
  - !ruby/object:Gem::Dependency
101
- name: rake
102
- prerelease: false
103
- requirement: &id006 !ruby/object:Gem::Requirement
70
+ version_requirements: &id004 !ruby/object:Gem::Requirement
104
71
  none: false
105
72
  requirements:
106
73
  - - ">="
@@ -109,12 +76,26 @@ dependencies:
109
76
  segments:
110
77
  - 0
111
78
  version: "0"
79
+ requirement: *id004
80
+ name: rake
81
+ prerelease: false
112
82
  type: :development
113
- version_requirements: *id006
114
83
  - !ruby/object:Gem::Dependency
84
+ version_requirements: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirement: *id005
115
94
  name: rspec
116
95
  prerelease: false
117
- requirement: &id007 !ruby/object:Gem::Requirement
96
+ type: :development
97
+ - !ruby/object:Gem::Dependency
98
+ version_requirements: &id006 !ruby/object:Gem::Requirement
118
99
  none: false
119
100
  requirements:
120
101
  - - ">="
@@ -123,8 +104,10 @@ dependencies:
123
104
  segments:
124
105
  - 0
125
106
  version: "0"
107
+ requirement: *id006
108
+ name: phocus
109
+ prerelease: false
126
110
  type: :development
127
- version_requirements: *id007
128
111
  description: Sensible options parsing
129
112
  email:
130
113
  - joshbuddy@gmail.com