Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/options: add flag to list a command's options #7839

Merged
merged 1 commit into from Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion Library/Homebrew/cmd/options.rb
Expand Up @@ -3,6 +3,7 @@
require "formula"
require "options"
require "cli/parser"
require "commands"

module Homebrew
module_function
Expand All @@ -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 <command>."
switch :debug
conflicts "--installed", "--all"
conflicts "--installed", "--all", "--command"
end
end

Expand All @@ -32,13 +35,43 @@ 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|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So much nicer 😍

[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
puts_options args.formulae
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|
EricFromCanada marked this conversation as resolved.
Show resolved Hide resolved
if / (?<option>-[-\w]+) +(?<desc>.*)$/ =~ line
options << [option, desc]
end
end
options
end

def puts_options(formulae)
formulae.each do |f|
next if f.options.empty?
Expand Down
2 changes: 2 additions & 0 deletions docs/Manpage.md
Expand Up @@ -340,6 +340,8 @@ Show install options specific to *`formula`*.
Show options for formulae that are currently installed.
* `--all`:
Show options for all available formulae.
* `--command`:
Show options for the specified *`command`*.

### `outdated` [*`options`*] [*`formula`*]

Expand Down
4 changes: 4 additions & 0 deletions manpages/brew.1
Expand Up @@ -456,6 +456,10 @@ Show options for formulae that are currently installed\.
\fB\-\-all\fR
Show options for all available formulae\.
.
.TP
\fB\-\-command\fR
Show options for the specified \fIcommand\fR\.
.
.SS "\fBoutdated\fR [\fIoptions\fR] [\fIformula\fR]"
List installed formulae that have an updated version available\. By default, version information is displayed in interactive shells, and suppressed otherwise\.
.
Expand Down