Class: BaseApp
- Inherits:
-
Object
- Object
- BaseApp
- Defined in:
- lib/base_app.rb
Instance Attribute Summary collapse
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #command_line_arguments ⇒ Object
- #construct_opts ⇒ Object
- #create_getter_setter(name) ⇒ Object
-
#exists?(file) ⇒ Boolean
file util helpers.
- #get_template(template_name) ⇒ Object
- #help_text ⇒ Object
- #help_text_firstline ⇒ Object
-
#initialize ⇒ BaseApp
constructor
A new instance of BaseApp.
- #mkdir(dir) ⇒ Object
- #parse_command_line_options ⇒ Object
- #process_template(template_name, additional_properties = {}) ⇒ Object
- #process_template_to_file(name, target, props = {}) ⇒ Object
- #write_file(target, *content) ⇒ Object
Constructor Details
#initialize ⇒ BaseApp
Returns a new instance of BaseApp.
7 8 9 10 11 |
# File 'lib/base_app.rb', line 7 def initialize @status = 0 @options = {} @required_opts = [] end |
Instance Attribute Details
#status ⇒ Object
Returns the value of attribute status.
5 6 7 |
# File 'lib/base_app.rb', line 5 def status @status end |
Class Method Details
.main ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/base_app.rb', line 114 def self.main app = self.new app. if app.help $stderr.print app.help_text exit app.status end max_width = app.command_line_arguments.map {|x| x[1].length}.max if app.verbose $stderr.puts "#$0 Parameters:" app.command_line_arguments.each do |opt_spec| short, arg, descr = opt_spec option = arg.gsub(/=.?$/,'').gsub('-','_') $stderr.printf( " %*s : %s\n", max_width, arg.gsub(/=.?$/,''), app.send(option.to_sym)) end $stderr.puts "" end app.run exit app.status end |
Instance Method Details
#command_line_arguments ⇒ Object
13 14 15 16 |
# File 'lib/base_app.rb', line 13 def command_line_arguments [['v','verbose','Be more verbose.'], ['h', 'help', 'Request help.']] end |
#construct_opts ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/base_app.rb', line 18 def construct_opts opts = OptionParser.new do |opts| command_line_arguments.each do |argspec| short, long, description, required, default_value = argspec param_name, param_type = long.split '=' @required_opts << param_name if required @options[param_name] = default_value if default_value create_getter_setter(long) param_required = long.index("=") ? " VAL" : "" opts.on("-#{short}", "--#{long}#{param_name}", description) do |val| @options[param_name] = val setter = param_name.gsub("-", "_") + "=" self.send(setter,val) end end end end |
#create_getter_setter(name) ⇒ Object
36 37 38 39 |
# File 'lib/base_app.rb', line 36 def create_getter_setter(name) name = name.gsub("-", "_").gsub(/=.?/, "") self.class.send(:attr_accessor, name) unless self.respond_to?(name) end |
#exists?(file) ⇒ Boolean
file util helpers
52 53 54 |
# File 'lib/base_app.rb', line 52 def exists? file File.exists? file end |
#get_template(template_name) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/base_app.rb', line 78 def get_template(template_name) unless @templates @templates = {} template_data = DATA.read template_data.scan(/(__.+?__.+?__.+?__)/m).each do |template| template = template[0] template =~ /^(__.+?__)$/m name = $1 template.gsub! /^__.+?__$/m, '' @templates[name] = template end end @templates["__#{template_name.to_s.upcase}__"] or raise "Error: no template named: #{template_name} (#{@templates.keys.join(", ")})" end |
#help_text ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/base_app.rb', line 64 def help_text text = help_text_firstline command_line_arguments.each do |argspec| short, long, description, required, default_value = argspec short_text = (long =~ /=/) ? "#{short}=s" : short text << sprintf(" -%-3s | --%-20s %s%s\n", short_text, long, description, (required ? "(required)" : "")) end text end |
#help_text_firstline ⇒ Object
60 61 62 |
# File 'lib/base_app.rb', line 60 def help_text_firstline "#$0: \n" end |
#mkdir(dir) ⇒ Object
56 57 58 |
# File 'lib/base_app.rb', line 56 def mkdir dir FileUtils.mkdir(dir) unless exists?(dir) end |
#parse_command_line_options ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/base_app.rb', line 41 def opts = construct_opts opts.parse! @required_opts.each do |name| unless @options[name] raise "Error, required argument '#{name}' must be supplied." end end end |
#process_template(template_name, additional_properties = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/base_app.rb', line 94 def process_template (template_name, additional_properties={} ) template = get_template template_name @properties.merge(additional_properties).each do |prop,val| term = "{{#{prop}}}" printf "substituting: %-40s => %s\n", term, val template.gsub! term, val end template end |
#process_template_to_file(name, target, props = {}) ⇒ Object
74 75 76 |
# File 'lib/base_app.rb', line 74 def process_template_to_file(name,target,props={}) write_file(target, process_template(name,target,props)) end |
#write_file(target, *content) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/base_app.rb', line 106 def write_file(target,*content) File.open(target,'w') do |f| content.each do |c| f.puts c.to_s end end end |