Skip to content

Commit

Permalink
Support single-character flags in option files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansissel committed Oct 29, 2022
1 parent 7d6b5cd commit 16c7e28
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
28 changes: 26 additions & 2 deletions lib/fpm/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,32 @@ def load_options(path)
while args.any?
arg = args.shift

# Lookup the Clamp flag by its --flag-name
option = self.class.find_option(arg)
# Lookup the Clamp option by its --flag-name or short name like -f
if arg =~ /^-/
# Single-letter options like -a or -z
if single_letter = arg.match(/^(-[A-Za-z0-9])(.*)$/)
puts "Found single letter entry #{single_letter.match(1)} from arg #{arg}"
option = self.class.find_option(single_letter.match(1))
arg, remainder = single_letter.match(1), single_letter.match(2)
if option.flag?
# Flags aka switches take no arguments, so we push the rest of the 'arg' entry back onto the args list

# For combined letter flags, like `-abc`, we want to consume the
# `-a` and then push `-bc` back to be processed.
# Only do this if there's more flags, like, not for `-a` but yes for `-abc`
args.unshift("-" + remainder) unless remainder.empty?
else
# Single letter options that take arguments, like `-ohello` same as `-o hello`

# For single letter flags with values, like `-ofilename` aka `-o filename`, push the remainder ("filename")
# back onto the args list so that it is consumed when we extract the flag value.
args.unshift(remainder) unless remainder.empty?
end
elsif arg.match(/^--/)
# Lookup the flag by its long --flag-name
option = self.class.find_option(arg)
end
end

# Extract the flag value, if any, from the remaining args list.
value = option.extract_value(arg, args)
Expand Down
26 changes: 20 additions & 6 deletions spec/fpm/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,28 @@
context "with multiple single-letter flags on a single line" do
subject { FPM::Command.new("fpm") }

before do
File.write(path, [
"-ffff"
].join($/))
context "separately" do
before do
File.write(path, [
"-f -f -f -f"
].join($/))
end

it "should work" do
subject.parse(["--fpm-options-file", path])
end
end

it "should work" do
subject.parse(["--fpm-options-file", path])
context "together" do
before do
File.write(path, [
"-ffff"
].join($/))
end

it "should work" do
subject.parse(["--fpm-options-file", path])
end
end
end

Expand Down

0 comments on commit 16c7e28

Please sign in to comment.