optimus-prime 0.0.4

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.
@@ -0,0 +1,65 @@
1
+ require 'optparse'
2
+
3
+ $LOAD_PATH.unshift File.dirname(__FILE__)
4
+
5
+ require 'optimus_prime/core_ext/object'
6
+ require 'optimus_prime/command'
7
+ require 'optimus_prime/optor'
8
+
9
+ module OptimusPrime
10
+ def self.included(klass)
11
+ klass.class_eval do
12
+ extend ClassMethods
13
+
14
+ command :help do |cmd|
15
+ ##
16
+ # Show this help message
17
+ if cmd and self.class.commands.include?(cmd)
18
+ puts help(cmd)
19
+ else
20
+ puts "Commands:"
21
+ puts self.class.commands.map { |name| "- #{name}" }
22
+ end
23
+ end
24
+
25
+ def initialize
26
+ self.class.init(self)
27
+ end
28
+
29
+ def help(name)
30
+ self.class.help(name)
31
+ end
32
+ end
33
+ end
34
+
35
+ module ClassMethods
36
+ def __optor__
37
+ @__optor__ ||= Optor.new(self, ARGV.dup)
38
+ end
39
+
40
+ def init(instance)
41
+ __optor__.init(instance)
42
+ end
43
+
44
+ def help(name)
45
+ __optor__.help(name)
46
+ end
47
+
48
+ def command(name, &block)
49
+ __optor__.command(name, block)
50
+ end
51
+
52
+ def commands
53
+ __optor__.commands.keys
54
+ end
55
+
56
+ def flag(*flags)
57
+ flags.each { |name| __optor__.flag(name) }
58
+ end
59
+
60
+ def option(*names)
61
+ options = names.last.is_a?(Hash) ? names.pop : {}
62
+ names.each { |name| __optor__.option(name, options) }
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,41 @@
1
+ module OptimusPrime
2
+ class Command
3
+ def initialize(handler, caller=nil)
4
+ @handler, @caller = handler, (caller && caller[1])
5
+ end
6
+
7
+ def help
8
+ @help ||= begin
9
+ lines = file.split(/\n/)
10
+ result = []
11
+ i = 0
12
+ while (string = lines[line+i]) =~ /\s*##?/
13
+ result << string.gsub(/^\s*#*\s?/, '')
14
+ i += 1
15
+ end
16
+ result.shift if result.first.empty?
17
+ result.pop if result.last.empty?
18
+ result.join("\n")
19
+ end
20
+ end
21
+
22
+ def file
23
+ @file ||= File.read(@caller.split(':').first)
24
+ end
25
+
26
+ def line
27
+ @caller.split(':').last.to_i
28
+ end
29
+
30
+ def arity
31
+ @handler.arity
32
+ end
33
+
34
+ def to_proc
35
+ case handler = @handler
36
+ when Proc then handler
37
+ else @handler.to_proc
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,22 @@
1
+ class Object
2
+ unless respond_to?(:instance_exec)
3
+ module InstanceExecHelper; end
4
+ include InstanceExecHelper
5
+ def instance_exec(*args, &block)
6
+ begin
7
+ old_critical, Thread.critical = Thread.critical, true
8
+ n = 0
9
+ n += 1 while respond_to?(mname="__instance_exec#{n}")
10
+ InstanceExecHelper.module_eval{ define_method(mname, &block) }
11
+ ensure
12
+ Thread.critical = old_critical
13
+ end
14
+ begin
15
+ ret = send(mname, *args)
16
+ ensure
17
+ InstanceExecHelper.module_eval{ remove_method(mname) } rescue nil
18
+ end
19
+ ret
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,74 @@
1
+ module OptimusPrime
2
+ class Optor
3
+ def initialize(klass, args)
4
+ @klass, @args = klass, args
5
+ end
6
+
7
+ def options
8
+ @options ||= begin
9
+ list = [''] # need single options for getopts. psh.
10
+ list += @option_list || []
11
+ OptionParser.getopts(@args, *list)
12
+ end
13
+ end
14
+
15
+ def commands
16
+ @commands ||= {}
17
+ end
18
+
19
+ def command(name, handler)
20
+ commands[name.to_s] = handler && Command.new(handler, caller)
21
+ end
22
+
23
+ def help(name)
24
+ commands[name].help
25
+ end
26
+
27
+ def flag(name)
28
+ @option_list ||= []
29
+ @option_list << name.to_s
30
+ end
31
+
32
+ def option(name, options={})
33
+ @option_list ||= []
34
+ @option_list << name.to_s + ':'
35
+
36
+ if options[:prompt]
37
+ @prompt_list ||= {}
38
+ @prompt_list[name.to_s] = options[:prompt]
39
+ end
40
+ end
41
+
42
+ def init(instance)
43
+ options.each do |key,val|
44
+ instance.instance_variable_set("@#{key}", val)
45
+ end
46
+
47
+ Array(@prompt_list).each do |key, prompt|
48
+ next if options[key]
49
+ $stdout.print prompt + ' '
50
+ options[key] = $stdin.gets.chomp
51
+ instance.instance_variable_set("@#{key}", options[key])
52
+ end
53
+
54
+ args = @args.dup
55
+ args.each do |val|
56
+ args.delete(val)
57
+ run_command(instance, val, args)
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def get_command(instance, name)
64
+ Command.new(@klass.instance_method(name).bind(instance))
65
+ end
66
+
67
+ def run_command(instance, name, args)
68
+ command = commands[name] || get_command(instance, name)
69
+ block_args = []
70
+ command.arity.times { block_args << args.shift }
71
+ instance.instance_exec(*block_args, &command)
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optimus-prime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Pat Nakajima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: patnakajima@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/optimus_prime/command.rb
26
+ - lib/optimus_prime/optor.rb
27
+ - lib/optimus_prime.rb
28
+ - lib/optimus_prime/core_ext/object.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/nakajima/optimus-prime
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Easy command line options.
57
+ test_files: []
58
+