Skip to content

Commit add3459

Browse files
committed
Add an option_globally_or_per_branch_boolean CLI option helper
1 parent 8d4b413 commit add3459

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

lib/octocatalog-diff/cli/options.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def self.option_globally_or_per_branch(opts = {})
103103
datatype = opts.fetch(:datatype, '')
104104
return option_globally_or_per_branch_string(opts) if datatype.is_a?(String)
105105
return option_globally_or_per_branch_array(opts) if datatype.is_a?(Array)
106+
return option_globally_or_per_branch_boolean(opts) if datatype.is_a?(TrueClass) || datatype.is_a?(FalseClass)
106107
raise ArgumentError, "option_globally_or_per_branch not equipped to handle #{datatype.class}"
107108
end
108109

@@ -177,6 +178,40 @@ def self.option_globally_or_per_branch_array(opts = {})
177178
end
178179
end
179180

181+
# See description of `option_globally_or_per_branch`. This implements the logic for a boolean value.
182+
# @param :parser [OptionParser object] The OptionParser argument
183+
# @param :options [Hash] Options hash being constructed; this is modified in this method.
184+
# @param :cli_name [String] Name of option on command line (e.g. puppet-binary)
185+
# @param :option_name [Symbol] Name of option in the options hash (e.g. :puppet_binary)
186+
# @param :desc [String] Description of option on the command line; will have "for the XX branch" appended
187+
def self.option_globally_or_per_branch_boolean(opts)
188+
parser = opts.fetch(:parser)
189+
options = opts.fetch(:options)
190+
cli_name = opts.fetch(:cli_name)
191+
option_name = opts.fetch(:option_name)
192+
desc = opts.fetch(:desc)
193+
194+
flag = cli_name
195+
from_option = "from_#{option_name}".to_sym
196+
to_option = "to_#{option_name}".to_sym
197+
parser.on("--[no-]#{flag}", "#{desc} globally") do |x|
198+
translated = translate_option(opts[:translator], x)
199+
options[to_option] = translated
200+
options[from_option] = translated
201+
post_process(opts[:post_process], options)
202+
end
203+
parser.on("--[no-]to-#{flag}", "#{desc} for the to branch") do |x|
204+
translated = translate_option(opts[:translator], x)
205+
options[to_option] = translated
206+
post_process(opts[:post_process], options)
207+
end
208+
parser.on("--[no-]from-#{flag}", "#{desc} for the from branch") do |x|
209+
translated = translate_option(opts[:translator], x)
210+
options[from_option] = translated
211+
post_process(opts[:post_process], options)
212+
end
213+
end
214+
180215
# If a validator was provided, run the validator on the supplied value. The validator is expected to
181216
# throw an error if there is a problem. Note that the validator runs *before* the translator if both
182217
# a validator and translator are supplied.

0 commit comments

Comments
 (0)