Sha256: 7f13f7c7abb7df3fa0bf0ee0b9f91ac9b62ae67f0dde8cf77b04d170bfe05c52

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

require 'optparse'

require 'bixby-client'
require 'bixby-client/app/command'

module Bixby
  class Client

    class << self
      attr_accessor :app
    end

    class App

      attr_reader :global_options, :command, :commands

      def initialize
        @global_options = {}
        @commands = []
      end

      def run

        begin
          options.order!(ARGV)

          if global_options[:help] then
            display_help()
          end

        rescue Exception => ex
          exit if ex.kind_of? SystemExit
          STDERR.puts "error: #{ex}"
          STDERR.puts
          STDERR.puts parser
          exit 1
        end

        subcmd = ARGV.shift
        commands.each do |c|
          if c.match(subcmd) then
            ret = c.new.run(global_options, ARGV)
            exit (ret || 0)
          end
        end

        # if we reached here, no cmd given, show help?
        display_help()

      end

      def display_help
        puts "Usage: bixby [global options] command [command options] [arguments...]"
        puts
        puts "GLOBAL OPTIONS"
        puts options.summarize
        puts

        puts "COMMANDS"
        commands.each do |c|
          puts "    " + c.command_name + " - " + c.desc
        end

        exit 0
      end

      def options
        @options ||= OptionParser.new do |opts|
          opts.banner = nil

          opts.on_tail("-v", "--verbose", "Enable verbose output") {
            global_options[:verbose] = true
          }

          opts.on_tail("-h", "--help", "Display this help") {
            global_options[:help] = true
          }
        end
      end

    end
  end
end

Bixby::Client.app = Bixby::Client::App.new
require 'bixby-client/app/commands/run'
Bixby::Client.app.run

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bixby-client-0.5.3 lib/bixby-client/app.rb
bixby-client-0.5.2 lib/bixby-client/app.rb
bixby-client-0.5.1 lib/bixby-client/app.rb
bixby-client-0.5.0 lib/bixby-client/app.rb
bixby-client-0.4.1 lib/bixby-client/app.rb
bixby-client-0.4.0 lib/bixby-client/app.rb
bixby-client-0.3.0 lib/bixby-client/app.rb