Skip to content

Commit 910e03e

Browse files
committed
Hack the validator to return the caller when a special value is passed in
1 parent 0a835b4 commit 910e03e

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/octocatalog-diff/cli/options.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class Options
1313
# The usage banner.
1414
BANNER = 'Usage: catalog-diff -n <hostname> [-f <from environment>] [-t <to environment>]'.freeze
1515

16+
# An error class specifically for passing information to the document build task.
17+
class DocBuildError < RuntimeError; end
18+
1619
# List of classes
1720
def self.classes
1821
@classes ||= []
@@ -85,6 +88,7 @@ def self.option_classes
8588
# option will populate any of the 'to' and 'from' variants that are missing.
8689
# @param :datatype [?] Expected data type
8790
def self.option_globally_or_per_branch(opts = {})
91+
opts[:filename] = caller[0].split(':').first
8892
datatype = opts.fetch(:datatype, '')
8993
return option_globally_or_per_branch_string(opts) if datatype.is_a?(String)
9094
return option_globally_or_per_branch_array(opts) if datatype.is_a?(Array)
@@ -108,19 +112,19 @@ def self.option_globally_or_per_branch_string(opts)
108112
from_option = "from_#{option_name}".to_sym
109113
to_option = "to_#{option_name}".to_sym
110114
parser.on("--#{flag}", "#{desc} globally") do |x|
111-
validate_option(opts[:validator], x)
115+
validate_option(opts, x)
112116
translated = translate_option(opts[:translator], x)
113117
options[to_option] ||= translated
114118
options[from_option] ||= translated
115119
post_process(opts[:post_process], options)
116120
end
117121
parser.on("--to-#{flag}", "#{desc} for the to branch") do |x|
118-
validate_option(opts[:validator], x)
122+
validate_option(opts, x)
119123
options[to_option] = translate_option(opts[:translator], x)
120124
post_process(opts[:post_process], options)
121125
end
122126
parser.on("--from-#{flag}", "#{desc} for the from branch") do |x|
123-
validate_option(opts[:validator], x)
127+
validate_option(opts, x)
124128
options[from_option] = translate_option(opts[:translator], x)
125129
post_process(opts[:post_process], options)
126130
end
@@ -143,20 +147,20 @@ def self.option_globally_or_per_branch_array(opts = {})
143147
from_option = "from_#{option_name}".to_sym
144148
to_option = "to_#{option_name}".to_sym
145149
parser.on("--#{flag}", Array, "#{desc} globally") do |x|
146-
validate_option(opts[:validator], x)
150+
validate_option(opts, x)
147151
translated = translate_option(opts[:translator], x)
148152
options[to_option] ||= []
149153
options[to_option].concat translated
150154
options[from_option] ||= []
151155
options[from_option].concat translated
152156
end
153157
parser.on("--to-#{flag}", Array, "#{desc} for the to branch") do |x|
154-
validate_option(opts[:validator], x)
158+
validate_option(opts, x)
155159
options[to_option] ||= []
156160
options[to_option].concat translate_option(opts[:translator], x)
157161
end
158162
parser.on("--from-#{flag}", Array, "#{desc} for the from branch") do |x|
159-
validate_option(opts[:validator], x)
163+
validate_option(opts, x)
160164
options[from_option] ||= []
161165
options[from_option].concat translate_option(opts[:translator], x)
162166
end
@@ -165,9 +169,14 @@ def self.option_globally_or_per_branch_array(opts = {})
165169
# If a validator was provided, run the validator on the supplied value. The validator is expected to
166170
# throw an error if there is a problem. Note that the validator runs *before* the translator if both
167171
# a validator and translator are supplied.
168-
# @param validator [Code] Validation function
172+
# @param opts [Hash] Options hash
169173
# @param value [?] Value to validate (typically a String but can really be anything)
170-
def self.validate_option(validator, value)
174+
def self.validate_option(opts, value)
175+
# Special value to help build documentation automatically, since the source file location
176+
# for `option_globally_or_per_branch` is always this file.
177+
raise DocBuildError, opts[:filename] if value == :DOC_BUILD_FILENAME
178+
179+
validator = opts[:validator]
171180
return true unless validator
172181
validator.call(value)
173182
end

rake/doc.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ def options
5555
next if long.nil?
5656
long.each do |_longopt, val|
5757
filename = val.instance_variable_get('@block').source_location[0]
58-
next unless filename.start_with?(CODE_PATH + '/') || filename == CODE_PATH + '.rb'
58+
if filename == CODE_PATH + '.rb'
59+
begin
60+
val.instance_variable_get('@block').call(:DOC_BUILD_FILENAME)
61+
rescue OctocatalogDiff::Cli::Options::DocBuildError => e
62+
filename = e.message
63+
end
64+
end
65+
next unless filename.start_with?(CODE_PATH + '/')
5966

6067
arg = val.instance_variable_get('@arg')
6168
arg.strip! if arg.is_a?(String)

0 commit comments

Comments
 (0)