brew/Library/Homebrew/cmd/options.rb

89 lines
2.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "formula"
require "options"
2019-04-17 18:25:08 +09:00
require "cli/parser"
require "commands"
2011-03-19 09:58:42 -07:00
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
2018-11-07 21:59:04 +05:30
def options_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
2019-08-06 14:17:17 -04:00
`options` [<options>] [<formula>]
2018-11-07 21:59:04 +05:30
Show install options specific to <formula>.
2018-11-07 21:59:04 +05:30
EOS
switch "--compact",
2019-04-30 08:44:35 +01:00
description: "Show all options on a single line separated by spaces."
2018-11-07 21:59:04 +05:30
switch "--installed",
description: "Show options for formulae that are currently installed."
switch "--all",
description: "Show options for all available formulae."
flag "--command=",
description: "Show options for the specified <command>."
2020-07-30 18:40:10 +02:00
conflicts "--installed", "--all", "--command"
2018-11-07 21:59:04 +05:30
end
end
2013-06-26 15:08:45 -05:00
def options
2020-07-30 18:40:10 +02:00
args = options_args.parse
2018-11-07 21:59:04 +05:30
if args.all?
puts_options Formula.to_a.sort, args: args
2018-11-07 21:59:04 +05:30
elsif args.installed?
puts_options Formula.installed.sort, args: args
elsif !args.command.nil?
path = Commands.path(args.command)
odie "Unknown command: #{args.command}" unless path
cmd_options = if cmd_parser = CLI::Parser.from_cmd_path(path)
cmd_parser.processed_options.map do |short, long, _, desc|
[long || short, desc]
end
else
cmd_comment_options(path)
end
if args.compact?
puts cmd_options.sort.map(&:first) * " "
else
cmd_options.sort.each { |option, desc| puts "#{option}\n\t#{desc}" }
puts
end
elsif args.no_named?
raise FormulaUnspecifiedError
2013-06-26 15:08:45 -05:00
else
puts_options args.formulae, args: args
end
2011-03-19 09:58:42 -07:00
end
def cmd_comment_options(cmd_path)
options = []
comment_lines = cmd_path.read.lines.grep(/^#:/)
return options if comment_lines.empty?
# skip the comment's initial usage summary lines
comment_lines.slice(2..-1).each do |line|
if / (?<option>-[-\w]+) +(?<desc>.*)$/ =~ line
options << [option, desc]
end
end
options
end
def puts_options(formulae, args:)
2013-06-26 15:08:45 -05:00
formulae.each do |f|
next if f.options.empty?
2018-09-17 02:45:00 +02:00
2018-11-07 21:59:04 +05:30
if args.compact?
puts f.options.as_flags.sort * " "
2011-03-10 21:32:10 -08:00
else
2015-05-27 21:08:24 +08:00
puts f.full_name if formulae.length > 1
dump_options_for_formula f
2011-03-10 21:32:10 -08:00
puts
end
end
end
end