@@ -13,6 +13,9 @@ class Options
13
13
# The usage banner.
14
14
BANNER = 'Usage: catalog-diff -n <hostname> [-f <from environment>] [-t <to environment>]' . freeze
15
15
16
+ # An error class specifically for passing information to the document build task.
17
+ class DocBuildError < RuntimeError ; end
18
+
16
19
# List of classes
17
20
def self . classes
18
21
@classes ||= [ ]
@@ -85,6 +88,7 @@ def self.option_classes
85
88
# option will populate any of the 'to' and 'from' variants that are missing.
86
89
# @param :datatype [?] Expected data type
87
90
def self . option_globally_or_per_branch ( opts = { } )
91
+ opts [ :filename ] = caller [ 0 ] . split ( ':' ) . first
88
92
datatype = opts . fetch ( :datatype , '' )
89
93
return option_globally_or_per_branch_string ( opts ) if datatype . is_a? ( String )
90
94
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)
108
112
from_option = "from_#{ option_name } " . to_sym
109
113
to_option = "to_#{ option_name } " . to_sym
110
114
parser . on ( "--#{ flag } " , "#{ desc } globally" ) do |x |
111
- validate_option ( opts [ :validator ] , x )
115
+ validate_option ( opts , x )
112
116
translated = translate_option ( opts [ :translator ] , x )
113
117
options [ to_option ] ||= translated
114
118
options [ from_option ] ||= translated
115
119
post_process ( opts [ :post_process ] , options )
116
120
end
117
121
parser . on ( "--to-#{ flag } " , "#{ desc } for the to branch" ) do |x |
118
- validate_option ( opts [ :validator ] , x )
122
+ validate_option ( opts , x )
119
123
options [ to_option ] = translate_option ( opts [ :translator ] , x )
120
124
post_process ( opts [ :post_process ] , options )
121
125
end
122
126
parser . on ( "--from-#{ flag } " , "#{ desc } for the from branch" ) do |x |
123
- validate_option ( opts [ :validator ] , x )
127
+ validate_option ( opts , x )
124
128
options [ from_option ] = translate_option ( opts [ :translator ] , x )
125
129
post_process ( opts [ :post_process ] , options )
126
130
end
@@ -143,20 +147,20 @@ def self.option_globally_or_per_branch_array(opts = {})
143
147
from_option = "from_#{ option_name } " . to_sym
144
148
to_option = "to_#{ option_name } " . to_sym
145
149
parser . on ( "--#{ flag } " , Array , "#{ desc } globally" ) do |x |
146
- validate_option ( opts [ :validator ] , x )
150
+ validate_option ( opts , x )
147
151
translated = translate_option ( opts [ :translator ] , x )
148
152
options [ to_option ] ||= [ ]
149
153
options [ to_option ] . concat translated
150
154
options [ from_option ] ||= [ ]
151
155
options [ from_option ] . concat translated
152
156
end
153
157
parser . on ( "--to-#{ flag } " , Array , "#{ desc } for the to branch" ) do |x |
154
- validate_option ( opts [ :validator ] , x )
158
+ validate_option ( opts , x )
155
159
options [ to_option ] ||= [ ]
156
160
options [ to_option ] . concat translate_option ( opts [ :translator ] , x )
157
161
end
158
162
parser . on ( "--from-#{ flag } " , Array , "#{ desc } for the from branch" ) do |x |
159
- validate_option ( opts [ :validator ] , x )
163
+ validate_option ( opts , x )
160
164
options [ from_option ] ||= [ ]
161
165
options [ from_option ] . concat translate_option ( opts [ :translator ] , x )
162
166
end
@@ -165,9 +169,14 @@ def self.option_globally_or_per_branch_array(opts = {})
165
169
# If a validator was provided, run the validator on the supplied value. The validator is expected to
166
170
# throw an error if there is a problem. Note that the validator runs *before* the translator if both
167
171
# a validator and translator are supplied.
168
- # @param validator [Code] Validation function
172
+ # @param opts [Hash] Options hash
169
173
# @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 ]
171
180
return true unless validator
172
181
validator . call ( value )
173
182
end
0 commit comments