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

cli_parser: Add support for short flag options #5184

Merged
merged 1 commit into from
Oct 26, 2018
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
20 changes: 12 additions & 8 deletions Library/Homebrew/cli_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,24 @@ def comma_array(name, description: nil)
end
end

def flag(name, description: nil, required_for: nil, depends_on: nil)
if name.end_with? "="
def flag(*names, description: nil, required_for: nil, depends_on: nil)
if names.any? { |name| name.end_with? "=" }
required = OptionParser::REQUIRED_ARGUMENT
name.chomp! "="
else
required = OptionParser::OPTIONAL_ARGUMENT
end
description = option_to_description(name) if description.nil?
process_option(name, description)
@parser.on(name, *wrap_option_desc(description), required) do |option_value|
Homebrew.args[option_to_name(name)] = option_value
names.map! { |name| name.chomp "=" }
description = option_to_description(*names) if description.nil?
process_option(*names, description)
@parser.on(*names, *wrap_option_desc(description), required) do |option_value|
names.each do |name|
Homebrew.args[option_to_name(name)] = option_value
end
end

set_constraints(name, required_for: required_for, depends_on: depends_on)
names.each do |name|
set_constraints(name, required_for: required_for, depends_on: depends_on)
end
end

def conflicts(*options)
Expand Down
14 changes: 14 additions & 0 deletions Library/Homebrew/test/cli_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@
end
end

describe "test short flag options" do
subject(:parser) {
described_class.new do
flag "-f", "--filename=", description: "Name of the file"
end
}

it "parses a short flag option with its argument" do
parser.parse(["--filename=random.txt"])
expect(Homebrew.args.filename).to eq "random.txt"
expect(Homebrew.args.f).to eq "random.txt"
end
end

describe "test constraints for flag options" do
subject(:parser) {
described_class.new do
Expand Down