From 68947f2af9ed83aa1d6ac5e54d87f3935dfd498c Mon Sep 17 00:00:00 2001 From: EricFromCanada Date: Fri, 26 Jun 2020 18:10:16 -0400 Subject: [PATCH] cmd/options: add flag to list a command's options --- Library/Homebrew/cmd/options.rb | 35 ++++++++++++++++++++++++++++++++- docs/Manpage.md | 2 ++ manpages/brew.1 | 4 ++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cmd/options.rb b/Library/Homebrew/cmd/options.rb index 397a39df0c66a..c41d27f1497de 100644 --- a/Library/Homebrew/cmd/options.rb +++ b/Library/Homebrew/cmd/options.rb @@ -3,6 +3,7 @@ require "formula" require "options" require "cli/parser" +require "commands" module Homebrew module_function @@ -20,8 +21,10 @@ def options_args 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 ." switch :debug - conflicts "--installed", "--all" + conflicts "--installed", "--all", "--command" end end @@ -32,6 +35,22 @@ def options puts_options Formula.to_a.sort elsif args.installed? puts_options Formula.installed.sort + 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 else @@ -39,6 +58,20 @@ def options end 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 / (?