nakajima-optimus-prime 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,62 @@
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)
33
+ @option_list ||= []
34
+ @option_list << name.to_s + ':'
35
+ end
36
+
37
+ def init(instance)
38
+ options.each do |key,val|
39
+ instance.instance_variable_set("@#{key}", val)
40
+ end
41
+
42
+ args = @args.dup
43
+ args.each do |val|
44
+ args.delete(val)
45
+ run_command(instance, val, args)
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def get_command(instance, name)
52
+ Command.new(@klass.instance_method(name).bind(instance))
53
+ end
54
+
55
+ def run_command(instance, name, args)
56
+ command = commands[name] || get_command(instance, name)
57
+ block_args = []
58
+ command.arity.times { block_args << args.shift }
59
+ instance.instance_exec(*block_args, &command)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'nakajima'
3
+ require 'optparse'
4
+
5
+ $LOAD_PATH.unshift File.dirname(__FILE__)
6
+
7
+ require 'optimus_prime/command'
8
+ require 'optimus_prime/optor'
9
+
10
+ module OptimusPrime
11
+ def self.included(klass)
12
+ klass.class_eval do
13
+ extend ClassMethods
14
+
15
+ command :help do |cmd|
16
+ ##
17
+ # Show this help message
18
+ if cmd and self.class.commands.include?(cmd)
19
+ puts help(cmd)
20
+ else
21
+ puts "Commands:"
22
+ puts self.class.commands.map { |name| "- #{name}" }
23
+ end
24
+ end
25
+
26
+ def initialize
27
+ self.class.init(self)
28
+ end
29
+
30
+ def help(name)
31
+ self.class.help(name)
32
+ end
33
+ end
34
+ end
35
+
36
+ module ClassMethods
37
+ def __optor__
38
+ @__optor__ ||= Optor.new(self, ARGV.dup)
39
+ end
40
+
41
+ def init(instance)
42
+ __optor__.init(instance)
43
+ end
44
+
45
+ def help(name)
46
+ __optor__.help(name)
47
+ end
48
+
49
+ def command(name, &block)
50
+ __optor__.command(name, block)
51
+ end
52
+
53
+ def commands
54
+ __optor__.commands.keys
55
+ end
56
+
57
+ def flag(*flags)
58
+ flags.each { |name| __optor__.flag(name) }
59
+ end
60
+
61
+ def option(*names)
62
+ names.each { |name| __optor__.option(name) }
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nakajima-optimus-prime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
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
+ has_rdoc: false
29
+ homepage: http://github.com/nakajima/optimus-prime
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Easy command line options.
54
+ test_files: []
55
+