2017-05-21 00:15:56 +02:00
|
|
|
require_relative "options"
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
module Hbc
|
|
|
|
class CLI
|
2017-05-20 19:08:03 +02:00
|
|
|
class AbstractCommand
|
2017-05-21 00:15:56 +02:00
|
|
|
include Options
|
|
|
|
|
|
|
|
option "--[no-]binaries", :binaries, true
|
|
|
|
option "--debug", :debug, false
|
|
|
|
option "--verbose", :verbose, false
|
|
|
|
option "--outdated", :outdated_only, false
|
2018-06-02 02:47:23 +02:00
|
|
|
option "--require-sha", :require_sha, false
|
2017-05-21 00:15:56 +02:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.command_name
|
2016-10-14 20:03:34 +02:00
|
|
|
@command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-05-20 19:08:03 +02:00
|
|
|
def self.abstract?
|
2017-05-29 16:36:01 -07:00
|
|
|
name.split("::").last.match?(/^Abstract[^a-z]/)
|
2017-05-20 19:08:03 +02:00
|
|
|
end
|
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.visible
|
|
|
|
true
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.help
|
2017-03-16 09:00:57 +01:00
|
|
|
nil
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-05-20 19:08:03 +02:00
|
|
|
def self.run(*args)
|
|
|
|
new(*args).run
|
|
|
|
end
|
|
|
|
|
2017-05-21 00:15:56 +02:00
|
|
|
attr_accessor :args
|
|
|
|
private :args=
|
|
|
|
|
2017-05-19 21:13:08 +02:00
|
|
|
def initialize(*args)
|
2017-05-21 00:15:56 +02:00
|
|
|
@args = process_arguments(*args)
|
2017-05-19 21:13:08 +02:00
|
|
|
end
|
2017-06-13 17:14:01 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def casks(alternative: -> { [] })
|
2017-09-11 08:37:15 +02:00
|
|
|
return @casks if defined?(@casks)
|
2017-06-13 17:14:01 +02:00
|
|
|
casks = args.empty? ? alternative.call : args
|
2017-09-11 08:37:15 +02:00
|
|
|
@casks = casks.map { |cask| CaskLoader.load(cask) }
|
|
|
|
rescue CaskUnavailableError => e
|
2018-06-02 02:47:23 +02:00
|
|
|
reason = [e.reason, *suggestion_message(e.token)].join(" ")
|
2017-09-11 08:37:15 +02:00
|
|
|
raise e.class.new(e.token, reason)
|
|
|
|
end
|
|
|
|
|
|
|
|
def suggestion_message(cask_token)
|
2018-06-02 02:47:23 +02:00
|
|
|
matches, = Search.search(cask_token)
|
2017-06-13 17:14:01 +02:00
|
|
|
|
2018-06-02 02:47:23 +02:00
|
|
|
if matches.one?
|
|
|
|
"Did you mean “#{matches.first}”?"
|
|
|
|
elsif !matches.empty?
|
2017-09-11 08:37:15 +02:00
|
|
|
"Did you mean one of these?\n"
|
2018-06-02 02:47:23 +02:00
|
|
|
.concat(Formatter.columns(matches.take(20)))
|
2017-09-11 08:37:15 +02:00
|
|
|
end
|
2017-06-13 17:14:01 +02:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|