Skip to content

Commit 9686ef9

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

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

lib/octocatalog-diff/cli/options.rb

Lines changed: 40 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,45 @@ 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+
# @param :datatype [Boolean] Indicates the default value which should be set for this option.
188+
def self.option_globally_or_per_branch_boolean(opts)
189+
parser = opts.fetch(:parser)
190+
options = opts.fetch(:options)
191+
cli_name = opts.fetch(:cli_name)
192+
option_name = opts.fetch(:option_name)
193+
desc = opts.fetch(:desc)
194+
datatype = opts.fetch(:datatype)
195+
196+
flag = cli_name
197+
from_option = "from_#{option_name}".to_sym
198+
to_option = "to_#{option_name}".to_sym
199+
parser.on("--[no-]#{flag}", "#{desc} globally") do |x|
200+
translated = translate_option(opts[:translator], x)
201+
options[to_option] = translated
202+
options[from_option] = translated
203+
post_process(opts[:post_process], options)
204+
end
205+
parser.on("--[no-]to-#{flag}", "#{desc} for the to branch") do |x|
206+
translated = translate_option(opts[:translator], x)
207+
options[to_option] = translated
208+
post_process(opts[:post_process], options)
209+
end
210+
parser.on("--[no-]from-#{flag}", "#{desc} for the from branch") do |x|
211+
translated = translate_option(opts[:translator], x)
212+
options[from_option] = translated
213+
post_process(opts[:post_process], options)
214+
end
215+
# Set value to default in datatype, if nothing has been determined yet:
216+
options[from_option] = datatype unless options.key?(from_option)
217+
options[to_option] = datatype unless options.key?(to_option)
218+
end
219+
180220
# If a validator was provided, run the validator on the supplied value. The validator is expected to
181221
# throw an error if there is a problem. Note that the validator runs *before* the translator if both
182222
# a validator and translator are supplied.

0 commit comments

Comments
 (0)