class Megam::App
Constants
- NO_COMMAND_GIVEN
include(a_module) is called inside a class, and adds module methods as instance methods. extend(a_module) adds all module methods to the instance it is called on.
Attributes
attr_accessors are setters/getters in ruby. The arguments are filtered and available for use to subclasses.
Public Class Methods
# File lib/megam/app.rb 74 def initialize 75 super # The super calls the mixlib cli. 76 77 ##Traps are being set for the following when an application starts. 78 ##SIGHUP 1 Term Hangup detected on controlling terminal 79 ## or death of controlling process 80 ##SIGINT 2 Term Interrupt from keyboard 81 ##SIGQUIT 3 Core Quit from keyboard 82 trap("TERM") do 83 Megam::App.fatal!("SIGTERM received, stopping", 1) 84 end 85 86 trap("INT") do 87 Megam::App.fatal!("SIGINT received, stopping", 2) 88 end 89 90 trap("QUIT") do 91 Megam::Log.info("SIGQUIT received, call stack:\n " + caller.join("\n ")) 92 end 93 94 @text ||= Megam::Text.new(STDOUT, STDERR, STDIN, {}) 95 96 end
Private Class Methods
The exception in ruby carries the class, message and the trace. www.ruby-doc.org/core-2.0/Exception.html
# File lib/megam/app.rb 164 def debug_stacktrace(e) 165 message = "#{e.class}: #{e}\n#{e.backtrace.join("\n")}" 166 megam_stacktrace_out = "Generated at #{Time.now.to_s}\n" 167 megam_stacktrace_out += message 168 169 #after the message is formulated in the variable megam_stack_trace_out, its 170 #stored in a file named megam-stacktrace.out 171 Megam::FileCache.store("megam-stacktrace.out", megam_stacktrace_out) 172 173 ##The same error is logged in the log file saying, go look at megam-stacktrace.out for error. 174 Megam::Log.fatal("Stacktrace dumped to #{Megam::FileCache.load("megam-stacktrace.out", false)}") 175 Megam::Log.debug(message) 176 true 177 end
# File lib/megam/app.rb 185 def exit!(msg, err = -1) 186 Megam::Log.debug(msg) 187 Process.exit err 188 end
Log
a fatal error message to both STDERR and the Logger, exit the application
# File lib/megam/app.rb 180 def fatal!(msg, err = -1) 181 Megam::Log.fatal(msg) 182 Process.exit err 183 end
Public Instance Methods
# File lib/megam/app.rb 109 def parse_options(args) 110 super 111 rescue OptionParser::InvalidOption => e 112 puts "Error: " + e.to_s 113 puts self.opt_parser 114 exit(1) 115 end
Run the “birr app”. Let it roam and stay by our side.[Go birr..Does it remind of the Hutch adv.]. The first thing run does is it parses the options. Once the first level of parsing is done, ie the help, no_command, sub_command entry is verified it proceeds to call Megam_Birr with the user entered options and arguments (ARGV)
# File lib/megam/app.rb 102 def run 103 Mixlib::Log::Formatter.show_time = false 104 validate_and_parse_options 105 Megam::Birr.new.run(@named_args, config) 106 exit 0 107 end
Private Instance Methods
# File lib/megam/app.rb 139 def no_command_given? 140 ARGV.empty? 141 end
# File lib/megam/app.rb 135 def no_subcommand_given? 136 ARGV[0] =~ /^-/ 137 end
Print the help message with the exit code. If no command is given, then a fatal message is printed. The options are parsed by calling the parse_options
present in the mixlib cli as extended by the app. A error gets caught and results in an ugly stack trace, which probably needs to be shown in an elegant way. The stacK should be logged using the debug_stacktrace
method in the app class.
# File lib/megam/app.rb 155 def print_help_and_exit(exitcode=1, fatal_message=nil) 156 Megam::Log.error(fatal_message) if fatal_message 157 parse_options(ARGV) 158 exit exitcode 159 end
A check is performed to see if an option is entered, help or version
# File lib/megam/app.rb 122 def validate_and_parse_options 123 # Checking ARGV validity *before* parse_options because parse_options 124 # mangles ARGV in some situations 125 if no_command_given? 126 print_help_and_exit(1, NO_COMMAND_GIVEN) 127 elsif (want_help? || want_version?) 128 print_help_and_exit 129 else 130 @named_args = parse_options(ARGV) 131 end 132 133 end
# File lib/megam/app.rb 143 def want_help? 144 ARGV[0] =~ /^(--help|-h)$/ 145 end
# File lib/megam/app.rb 147 def want_version? 148 ARGV[0] =~ /^(--version|-v)$/ 149 end