#!/usr/bin/env ruby require "rbnotes" include Rbnotes class App def initialize @gopts = {} end def options @gopts end def parse_global_options(args) while args.size > 0 arg = args.shift case arg when "-c", "--conf" file = args.shift raise ArgumentError, args.unshift(arg) if file.nil? file = File.expand_path(file) raise ArgumentError, "no such file: %s" % file unless FileTest.exist?(file) @gopts[:conf_file] = file when "-v", "--version" args.clear args.unshift("version") break when "-h", "--help" args.clear args.unshift("help") break else args.unshift(arg) break end end end def run(args) cmd = args.shift Commands.load(cmd).execute(args, Rbnotes.conf(@gopts[:conf_file])) end end app = App.new begin app.parse_global_options(ARGV) app.run(ARGV) rescue Errno::EPIPE => _ # Fix issue #61: When the pipeline which rbnotes connects is # discarded by the other program, the execption was raised. It does # not end abnormally for rbnotes. So, just ignores the exception. exit 0 rescue NoArgumentError => _ # Fix issue #80: Typically, this error raises when a command tries # to read the standard input for its arguments and gets nil. It # means user wants to cancel to execute. So, just ignore the error # and exit. exit 0 rescue MissingArgumentError, MissingTimestampError, NoEditorError, ProgramAbortError, Textrepo::InvalidTimestampStringError, InvalidTimestampPatternError, InvalidTimestampPatternAsDateError, NoConfFileError, NoTemplateFileError, ArgumentError, Errno::EACCES => e puts e.message exit 1 end