1
0
mirror of https://github.com/jordansissel/fpm synced 2025-04-29 14:58:00 +02:00

Support single-character flags in option files.

This commit is contained in:
Jordan Sissel 2022-05-19 16:38:28 -07:00 committed by Jordan Sissel
parent 7d6b5cd58c
commit 16c7e280a7
2 changed files with 46 additions and 8 deletions

@ -618,8 +618,32 @@ class FPM::Command < Clamp::Command
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)

@ -168,14 +168,28 @@ describe FPM::Command do
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