Class: BrewLib::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/brew_lib.rb

Overview

BrewLib::CLI

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ CLI

Returns a new instance of CLI.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/brew_lib.rb', line 41

def initialize(argv = ARGV)
  exception_handling do
    options_set_default
    OptionParser.new do |opts|
      # noinspection RubyNilAnalysis
      opts.banner = "#{NAME} {options}"
      opts.separator ""
      opts.separator "Options:"

      opts.on_tail("-h", "--help", "-H", "Display this help message.") do
        puts opts
        exit
      end

      options_spec.each { |args| opts.on(*args) }
    end.parse(argv)
  end
  BrewLib.post if options.post
end

Instance Method Details

#exception_handlingObject

Provide standard exception handling for the given block.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/brew_lib.rb', line 90

def exception_handling
  yield
rescue SystemExit
  # Exit silently with current status
  raise
rescue OptionParser::InvalidOption => e
  warn e.message
  exit(false)
rescue StandardError => e
  # Exit with error message
  puts e
  puts e.backtrace unless e.backtrace.nil?
  puts e.cause unless e.cause.nil?
  puts e.chain unless e.chain.nil?
  exit(false)
end

#optionsObject

Application options from the command line



78
79
80
# File 'lib/brew_lib.rb', line 78

def options
  @options ||= OpenStruct.new
end

#options_set_defaultObject

set default values for options



83
84
85
86
87
# File 'lib/brew_lib.rb', line 83

def options_set_default
  options.post = false
  options.silent = false
  options.verbose = false
end

#options_specObject

A list of all the standard options used in rake, suitable for passing to OptionParser.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/brew_lib.rb', line 63

def options_spec # :nodoc:
  [
    ["--post", "-p", "Post install ", ->(_) { options.post = true }],
    ["--silent", "-s", "Like --quiet, but also suppresses the 'in directory' announcement.",
     ->(_) { options.silent = true }],
    ["--verbose", "-v", "Log message to standard output.", ->(_) { options.verbose = true }],
    ["--version", "-V", "Display the program version.",
     lambda { |_|
       puts BrewLib::VERSION
       exit
     }]
  ]
end