Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 101 lines (88 sloc) 2.939 kb
82ff27a @wycats Change name to thor. Last commit to hermes repo.
wycats authored
1 require "#{File.dirname(__FILE__)}/getopt"
2
3 class Thor
b522f05 @wycats First pass of Thor::Runner
wycats authored
4 def self.inherited(klass)
5 subclass_files[File.expand_path(caller[0].split(":")[0])] << klass
6 subclasses << klass
7 end
8
9 def self.subclass_files
10 @subclass_files ||= Hash.new {|h,k| h[k] = []}
11 end
12
13 def self.subclasses
14 @subclasses ||= []
15 end
16
82ff27a @wycats Change name to thor. Last commit to hermes repo.
wycats authored
17 def self.method_added(meth)
18 return if !public_instance_methods.include?(meth.to_s) || !@usage
19 @descriptions ||= []
20 @usages ||= []
21 @opts ||= []
22 @descriptions << [meth.to_s, @desc]
23 @usages << [meth.to_s, @usage]
24 @opts << [meth.to_s, @method_options] if @method_options
25 @usage, @desc, @method_options = nil
26 end
27
28 def self.map(map)
29 @map = map
30 end
31
32 def self.desc(usage, description)
33 @usage, @desc = usage, description
34 end
35
36 def self.method_options(opts)
37 @method_options = opts.inject({}) do |accum, (k,v)|
38 accum.merge("--" + k.to_s => v.to_s.upcase)
39 end
40 end
41
b522f05 @wycats First pass of Thor::Runner
wycats authored
42 def self.help_list
43 return nil unless @usages
44 @help_list ||= begin
45 max_usage = @usages.max {|x,y| x.last.to_s.size <=> y.last.to_s.size}.last.size
46 max_opts = @opts.empty? ? 0 : format_opts(@opts.max {|x,y| x.last.to_s.size <=> y.last.to_s.size}.last).size
47 max_desc = @descriptions.max {|x,y| x.last.to_s.size <=> y.last.to_s.size}.last.size
48 Struct.new(:klass, :usages, :opts, :descriptions, :max).new(
49 self, @usages, @opts, @descriptions, Struct.new(:usage, :opt, :desc).new(max_usage, max_opts, max_desc)
50 )
51 end
52 end
53
54 def self.format_opts(opts)
55 return "" unless opts
56 opts.map do |opt, val|
57 if val == true || val == "BOOLEAN"
58 "[#{opt}]"
59 elsif val == "REQUIRED"
60 opt + "=" + opt.gsub(/\-/, "").upcase
61 elsif val == "OPTIONAL"
62 "[" + opt + "=" + opt.gsub(/\-/, "").upcase + "]"
63 end
64 end.join(" ")
65 end
66
82ff27a @wycats Change name to thor. Last commit to hermes repo.
wycats authored
67 def self.start
68 meth = ARGV.shift
69 params = []
70 while !ARGV.empty?
71 break if ARGV.first =~ /^\-/
72 params << ARGV.shift
73 end
74 if defined?(@map) && @map[meth]
75 meth = @map[meth].to_s
76 end
77 if @opts.assoc(meth)
78 opts = @opts.assoc(meth).last.map {|opt, val| [opt, val == true ? Getopt::BOOLEAN : Getopt.const_get(val)].flatten}
79 options = Getopt::Long.getopts(*opts)
80 params << options
81 end
82 new(meth, params).instance_variable_get("@results")
83 end
84
85 def initialize(op, params)
b522f05 @wycats First pass of Thor::Runner
wycats authored
86 @results = send(op.to_sym, *params) if public_methods.include?(op) || !methods.include?(op)
82ff27a @wycats Change name to thor. Last commit to hermes repo.
wycats authored
87 end
b522f05 @wycats First pass of Thor::Runner
wycats authored
88
82ff27a @wycats Change name to thor. Last commit to hermes repo.
wycats authored
89 desc "help", "show this screen"
90 def help
b522f05 @wycats First pass of Thor::Runner
wycats authored
91 list = self.class.help_list
82ff27a @wycats Change name to thor. Last commit to hermes repo.
wycats authored
92 puts "Options"
93 puts "-------"
b522f05 @wycats First pass of Thor::Runner
wycats authored
94 list.usages.each do |meth, usage|
95 format = "%-" + (list.max.usage + list.max.opt + 4).to_s + "s"
96 print format % (list.usages.assoc(meth)[1] + (list.opts.assoc(meth) ? " " + self.class.format_opts(list.opts.assoc(meth)[1]) : ""))
97 puts list.descriptions.assoc(meth)[1]
82ff27a @wycats Change name to thor. Last commit to hermes repo.
wycats authored
98 end
b522f05 @wycats First pass of Thor::Runner
wycats authored
99 end
100
82ff27a @wycats Change name to thor. Last commit to hermes repo.
wycats authored
101 end
Something went wrong with that request. Please try again.