2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-17 19:42:27 +09:00
|
|
|
require "ostruct"
|
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
module CLI
|
|
|
|
class Args < OpenStruct
|
2019-09-22 20:13:11 +05:30
|
|
|
attr_accessor :processed_options
|
2019-04-17 19:42:27 +09:00
|
|
|
# undefine tap to allow --tap argument
|
|
|
|
undef tap
|
|
|
|
|
|
|
|
def initialize(argv:)
|
|
|
|
super
|
|
|
|
@argv = argv
|
2019-09-22 20:13:11 +05:30
|
|
|
@processed_options = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def option_to_name(option)
|
|
|
|
option.sub(/\A--?/, "")
|
|
|
|
.tr("-", "_")
|
|
|
|
end
|
|
|
|
|
|
|
|
def cli_args
|
|
|
|
return @cli_args unless @cli_args.nil?
|
|
|
|
|
|
|
|
@cli_args = []
|
|
|
|
processed_options.each do |short, long|
|
|
|
|
option = long || short
|
|
|
|
switch = "#{option_to_name(option)}?".to_sym
|
|
|
|
flag = option_to_name(option).to_sym
|
|
|
|
if @table[switch].instance_of? TrueClass
|
|
|
|
@cli_args << option
|
|
|
|
elsif @table[flag].instance_of? TrueClass
|
|
|
|
@cli_args << option
|
|
|
|
elsif @table[flag].instance_of? String
|
|
|
|
@cli_args << option + "=" + @table[flag]
|
|
|
|
elsif @table[flag].instance_of? Array
|
|
|
|
@cli_args << option + "=" + @table[flag].join(",")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@cli_args
|
|
|
|
end
|
|
|
|
|
|
|
|
def options_only
|
|
|
|
@options_only ||= cli_args.select { |arg| arg.start_with?("-") }
|
|
|
|
end
|
|
|
|
|
|
|
|
def flags_only
|
|
|
|
@flags_only ||= cli_args.select { |arg| arg.start_with?("--") }
|
2019-04-17 19:42:27 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|