Class: Bixby::Client::App

Inherits:
Object show all
Defined in:
lib/bixby-client/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



18
19
20
21
# File 'lib/bixby-client/app.rb', line 18

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

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



16
17
18
# File 'lib/bixby-client/app.rb', line 16

def command
  @command
end

#commandsObject (readonly)

Returns the value of attribute commands.



16
17
18
# File 'lib/bixby-client/app.rb', line 16

def commands
  @commands
end

#global_optionsObject (readonly)

Returns the value of attribute global_options.



16
17
18
# File 'lib/bixby-client/app.rb', line 16

def global_options
  @global_options
end

Instance Method Details

#display_helpObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bixby-client/app.rb', line 53

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

#optionsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bixby-client/app.rb', line 68

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

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bixby-client/app.rb', line 23

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