Permalink
Browse files

use method-args

  • Loading branch information...
Josh Hull
Josh Hull committed Jan 6, 2011
1 parent 0a9840b commit 296c9bfd5e5ca99025b31161b8b3cba0a911634c
Showing with 31 additions and 127 deletions.
  1. +2 −1 .gitignore
  2. +8 −6 Gemfile.lock
  3. +17 −116 lib/optitron/class_dsl.rb
  4. +2 −3 optitron.gemspec
  5. +1 −1 spec/cli_spec.rb
  6. +1 −0 spec/spec_helper.rb
View
@@ -1,2 +1,3 @@
pkg/*
-*.gem
+*.gem
+.bundle
View
@@ -3,14 +3,17 @@ PATH
specs:
optitron (0.2.2)
callsite (~> 0.0.4)
- ruby2ruby (~> 1.2.4)
- ruby_parser (~> 2.0)
- sexp_processor (~> 3.0.4)
+ method-args (~> 0.1.0)
GEM
remote: http://rubygems.org/
specs:
callsite (0.0.4)
+ method-args (0.1.0)
+ ruby2ruby (~> 1.2.4)
+ ruby_parser (~> 2.0)
+ sexp_processor (~> 3.0.4)
+ phocus (1.1)
rake (0.8.7)
rspec (1.3.0)
ruby2ruby (1.2.5)
@@ -26,9 +29,8 @@ PLATFORMS
DEPENDENCIES
bundler (~> 1.0.0)
callsite (~> 0.0.4)
+ method-args (~> 0.1.0)
optitron!
+ phocus
rake
rspec
- ruby2ruby (~> 1.2.4)
- ruby_parser (~> 2.0)
- sexp_processor (~> 3.0.4)
View
@@ -1,94 +1,8 @@
require 'callsite'
-require 'ruby2ruby'
-require 'ruby_parser'
-require 'sexp_processor'
+require 'method_args'
class Optitron
- class MethodArgs < SexpProcessor
- attr_reader :method_map
- def initialize(cls)
- @cls = cls
- @method_map = {}
- @current_class = []
- super()
- end
-
- def process_module(exp)
- exp.shift
- @current_class << exp.first.to_sym
- process(exp)
- @current_class.pop
- exp.clear
- exp
- end
-
- def process_class(exp)
- exp.shift
- current_class_size = @current_class.size
- case exp.first
- when Symbol
- @current_class << exp.first.to_sym
- process(exp)
- else
- if exp.first.first == :colon2
- exp.first.shift
- class_exp = exp.shift
- class_exp[0, class_exp.size - 1].each do |const|
- @current_class << const.last
- end
- @current_class << class_exp.last
- else
- raise
- end
- exp.shift
- process(exp.first)
- end
- @current_class.slice!(current_class_size, @current_class.size)
- exp.clear
- exp
- end
-
- def process_defn(exp)
- exp.shift
- @current_method = exp.shift
- @ruby2ruby = Ruby2Ruby.new
- process_args(exp.shift)
- scope = exp.shift
- exp
- end
-
- def process_args(exp)
- exp.shift
- arg_list = []
- while !exp.empty?
- t = exp.shift
- case t
- when Symbol
- arg_list << if t.to_s[0] == ?*
- [t.to_s[1, t.to_s.size].to_sym, :greedy]
- else
- [t, :required]
- end
- when Sexp
- case t.shift
- when :block
- lasgn = t.shift
- lasgn.shift
- name = lasgn.shift
- sub_part = arg_list.find{|arg| arg.first == name}
- sub_part.clear
- sub_part << name
- sub_part << :optional
- sub_part << @ruby2ruby.process(lasgn.last)
- end
- end
- end
- @cls
- @method_map[@current_method] = arg_list if @cls.name == @current_class.map{|c| c.to_s}.join('::')
- end
- end
-
module ClassDsl
def self.included(o)
@@ -99,7 +13,6 @@ class << self;
end"
end
-
module ClassMethods
def method_added(m)
if @last_desc
@@ -112,6 +25,7 @@ def method_added(m)
@last_group = nil
end
end
+
def optitron_parser
send(:class_variable_set, :@@optitron_parser, Optitron::Parser.new) unless send(:class_variable_defined?, :@@optitron_parser)
send(:class_variable_get, :@@optitron_parser)
@@ -122,21 +36,6 @@ def optitron_dsl
send(:class_variable_get, :@@optitron_dsl)
end
- def method_args
- send(:class_variable_get, :@@method_args)
- end
-
- def build_method_args(file)
- unless send(:class_variable_defined?, :@@method_args)
- parser = RubyParser.new
- sexp = parser.process(File.read(file))
- method_args = MethodArgs.new(self)
- method_args.process(sexp)
- send(:class_variable_set, :@@method_args, method_args.method_map)
- end
- send(:class_variable_get, :@@method_args)
- end
-
def class_opt(name, desc = nil, opts = nil)
optitron_dsl.root.opt(name, desc, opts)
end
@@ -146,7 +45,7 @@ def dont_use_help
end
def desc(desc)
- build_method_args(Callsite.parse(caller.first).filename)
+ MethodArgs.register(Callsite.parse(caller.first).filename)
@last_desc = desc
end
@@ -163,29 +62,32 @@ def arg_types(*types)
@arg_types = types
end
- def build
+ def build(&blk)
unless @built
+ optitron_parser.target = blk ? blk.call : new
target = optitron_parser.target
optitron_dsl.root.help unless send(:class_variable_defined?, :@@suppress_help)
@cmds.each do |(cmd_name, cmd_desc, opts)|
- args = method_args[cmd_name.to_sym]
- arity = instance_method(cmd_name).arity
+ method = target.class.instance_method(cmd_name.to_sym).bind(target)
arg_types = @arg_types
optitron_dsl.root.cmd(cmd_name, cmd_desc) do
opts.each { |o| opt *o }
- args.each do |(arg_name, arg_type, arg_default)|
- possible_arg_type = arg_name.to_s[/_(string|hash|array|numeric|int|float)$/, 1]
- if possible_arg_type && (arg_types.nil? || !arg_types.first)
+ puts "!!! #{cmd_name.inspect}" if method.nil?
+ method.args.each do |arg|
+ possible_arg_type = arg.name.to_s[/_(string|hash|array|numeric|int|float)$/, 1]
+ arg_name = if possible_arg_type && (arg_types.nil? || !arg_types.first)
possible_arg_type = possible_arg_type.to_sym
- arg_name = arg_name.to_s[/^(.*)_(?:string|hash|array|numeric|int|float)$/, 1]
+ arg.name.to_s[/^(.*)_(?:string|hash|array|numeric|int|float)$/, 1]
+ else
+ arg.name
end
- arg_opts = { :default => arg_default && target.instance_eval(arg_default), :type => arg_types && arg_types.shift || possible_arg_type }
- case arg_type
+ arg_opts = { :default => arg.default_value, :type => arg_types && arg_types.shift || possible_arg_type }
+ case arg.type
when :required
arg arg_name.to_s, arg_opts
when :optional
arg arg_name.to_s, arg_opts.merge(:required => false)
- when :greedy
+ when :splat
arg arg_name.to_s, arg_opts.merge(:type => :greedy)
end
end
@@ -197,8 +99,7 @@ def build
end
def dispatch(args = ARGV, &blk)
- optitron_parser.target = blk ? blk.call : new
- build
+ build(&blk)
response = optitron_parser.parse(args)
if response.valid?
optitron_parser.target.params = response.params
View
@@ -15,13 +15,12 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3.6"
s.rubyforge_project = "optitron"
- s.add_dependency "ruby_parser", "~> 2.0"
s.add_dependency "callsite", "~> 0.0.4"
- s.add_dependency "ruby2ruby", "~> 1.2.4"
- s.add_dependency "sexp_processor", "~> 3.0.4"
+ s.add_dependency "method-args", "~> 0.1.0"
s.add_development_dependency "bundler", "~> 1.0.0"
s.add_development_dependency "rake"
s.add_development_dependency "rspec"
+ s.add_development_dependency "phocus"
s.files = `git ls-files`.split("\n")
s.require_path = 'lib'
View
@@ -110,7 +110,7 @@ def use_too(one, two = 'three')
end
it "should generate the correct help" do
- AnotherCLIExample.build
+ AnotherCLIExample.build { AnotherCLIExample.new("test") }
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"
end
View
@@ -1,5 +1,6 @@
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require 'optitron'
+require 'phocus'
class Object
def capture(stream)

0 comments on commit 296c9bf

Please sign in to comment.