Class: BrewLib::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ CLI

Returns a new instance of CLI.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/brew_lib.rb', line 48

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

      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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/brew_lib.rb', line 108

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

#optionsObject

Application options from the command line



96
97
98
# File 'lib/brew_lib.rb', line 96

def options
  @options ||= OpenStruct.new
end

#options_set_defaultObject

set default values for options



101
102
103
104
105
# File 'lib/brew_lib.rb', line 101

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/brew_lib.rb', line 70

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