lib/taskjuggler/Tj3AppBase.rb in taskjuggler-0.0.11 vs lib/taskjuggler/Tj3AppBase.rb in taskjuggler-0.1.0
- old
+ new
@@ -18,10 +18,13 @@
require 'taskjuggler/TjTime'
require 'taskjuggler/TextFormatter'
class TaskJuggler
+ class TjRuntimeError < RuntimeError
+ end
+
class Tj3AppBase
def initialize
# Indent and width of options. The deriving class may has to change
# this.
@@ -29,10 +32,11 @@
@optsSummaryIndent = 5
# Show some progress information by default
@silent = false
@configFile = nil
@mandatoryArgs = ''
+ @mininumRubyVersion = '1.9.2'
end
def processArguments(argv)
@opts = OptionParser.new
@opts.summary_width = @optsSummaryWidth
@@ -81,41 +85,60 @@
files
end
def main(argv = ARGV)
+ if Gem::Version.new(RUBY_VERSION.dup) <
+ Gem::Version.new(@mininumRubyVersion)
+ error('This program requires at least Ruby version ' +
+ "#{@mininumRubyVersion}!")
+ end
+
# Install signal handler to exit gracefully on CTRL-C.
intHandler = Kernel.trap('INT') do
- error("\nAborting on user request!")
+ error("Aborting on user request!")
end
- args = processArguments(argv)
+ begin
+ args = processArguments(argv)
- # If DEBUG mode has been enabled, we restore the INT trap handler again
- # to get Ruby backtrackes.
- Kernel.trap('INT', intHandler) if $DEBUG
+ # If DEBUG mode has been enabled, we restore the INT trap handler again
+ # to get Ruby backtrackes.
+ Kernel.trap('INT', intHandler) if $DEBUG
- unless @silent
- puts "#{AppConfig.softwareName} v#{AppConfig.version} - " +
- "#{AppConfig.packageInfo}\n\n" +
- "Copyright (c) #{AppConfig.copyright.join(', ')}\n" +
- " by #{AppConfig.authors.join(', ')}\n\n" +
- "#{AppConfig.license}\n"
- end
+ unless @silent
+ puts "#{AppConfig.softwareName} v#{AppConfig.version} - " +
+ "#{AppConfig.packageInfo}\n\n" +
+ "Copyright (c) #{AppConfig.copyright.join(', ')}\n" +
+ " by #{AppConfig.authors.join(', ')}\n\n" +
+ "#{AppConfig.license}\n"
+ end
- @rc = RuntimeConfig.new(AppConfig.packageName, @configFile)
+ @rc = RuntimeConfig.new(AppConfig.packageName, @configFile)
- args
+ appMain(args)
+ rescue Exception => e
+ if e.is_a?(SystemExit) || e.is_a?(Interrupt)
+ # Don't show backtrace on user interrupt unless we are in debug mode.
+ $stderr.puts e.backtrace.join("\n") if $DEBUG
+ 1
+ else
+ error("Ups, you have triggered a bug in #{AppConfig.softwareName}!\n" +
+ "#{e}\n" +
+ e.backtrace.join("\n") +
+ "Please see the user manual on how to get this bug fixed!")
+ end
+ end
end
private
def quit
exit 0
end
def error(message, exitVal = 1)
- $stderr.puts "ERROR: #{message}"
+ $stderr.puts "\nERROR: #{message}"
exit exitVal
end
def format(str, indent = nil)
indent = @optsSummaryWidth + @optsSummaryIndent + 1 unless indent