# File lib/rubyrep/command_runner.rb, line 41
    def self.run(args)
      status = 0
      options = {}

      parser = OptionParser.new do |opts|
        opts.banner = "Usage: \#{$0} [general options] command [parameters, ...]\n\nAsynchronous master-master replication of relational databases.\n"
        opts.separator ""
        opts.separator "Available options:"

        opts.on("--verbose", "Show errors with full stack trace") do
          options[:verbose] = true
        end

        opts.on("-v", "--version", "Show version information.") do
          show_version
          options = nil
        end

        opts.on_tail("--help", "Show this message") do
          $stderr.puts opts
          
          $stderr.puts "\nAvailable commands:"
          commands.sort.each do |command_name, command_hash|
            $stderr.puts "  #{command_name.ljust(15)} #{command_hash[:description]}"
          end

          options = nil
        end
      end

      begin

        # extract general options
        general_args = []
        until args.empty?
          if args[0] =~ /^-/
            general_args << args.shift
          else
            break
          end
        end

        # parse general options
        parser.parse!(general_args)

        # process commands
        if options # this will be +nil+ if the --help or --version are specified
          if args.empty?
            $stderr.puts "No command specified.\n\n"
            run(['--help'])
            status = 1
          else
            command = args[0]
            if command == 'help' and args.size == 1
              run(['--help'])
              status = 0
            elsif commands.include? command
              status = commands[command][:command].run(args.slice(1, 1_000_000))
            else
              $stderr.puts "Error: Unknown command specified.\n\n"
              run(['--help'])
              status = 1
            end
          end
        end
      rescue Exception => e
        $stderr.puts "Exception caught: #{e}"
        $stderr.puts e.backtrace if options && options[:verbose]
        status = 1
      end

      return status
    end