Module: EverydayCliUtils::OptionUtil
- Defined in:
- lib/everyday-cli-utils/option.rb
Instance Method Summary collapse
- #apply_options(layer, opts = {}) ⇒ Object
- #banner(banner) ⇒ Object
- #default_options(opts = {}) ⇒ Object
- #default_settings(settings = {}) ⇒ Object
- #defaults_option(file_path, names, settings = {}) ⇒ Object
- #global_defaults_option(file_path, names, settings = {}) ⇒ Object
- #help ⇒ Object
- #help_option(names, settings = {}) ⇒ Object
- #help_str=(str) ⇒ Object
- #option(opt_name, names, settings = {}, &block) ⇒ Object
- #option_list ⇒ Object
- #option_with_param(opt_name, names, settings = {}, &block) ⇒ Object
- #options ⇒ Object
- #opts ⇒ Object
- #parse!(argv = ARGV) ⇒ Object
- #show_defaults_option(layers, names, settings = {}) ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#apply_options(layer, opts = {}) ⇒ Object
243 244 245 246 |
# File 'lib/everyday-cli-utils/option.rb', line 243 def (layer, opts = {}) ||= OptionList.new opts.each { |opt| .update(opt[0], opt[1], layer) } end |
#banner(banner) ⇒ Object
248 249 250 251 |
# File 'lib/everyday-cli-utils/option.rb', line 248 def () ||= OptionList.new . = end |
#default_options(opts = {}) ⇒ Object
238 239 240 241 |
# File 'lib/everyday-cli-utils/option.rb', line 238 def (opts = {}) ||= OptionList.new opts.each { |opt| .set(opt[0], opt[1]) } end |
#default_settings(settings = {}) ⇒ Object
233 234 235 236 |
# File 'lib/everyday-cli-utils/option.rb', line 233 def default_settings(settings = {}) ||= OptionList.new .default_settings = settings end |
#defaults_option(file_path, names, settings = {}) ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/everyday-cli-utils/option.rb', line 203 def defaults_option(file_path, names, settings = {}) ||= OptionList.new @set_defaults = false @defaults_file = File.(file_path) @exit_on_save = !settings.has_key?(:exit_on_save) || settings[:exit_on_save] names << settings[:desc] if settings.has_key?(:desc) .opts.on(*names) { @set_defaults = true } end |
#global_defaults_option(file_path, names, settings = {}) ⇒ Object
212 213 214 215 216 217 218 219 |
# File 'lib/everyday-cli-utils/option.rb', line 212 def global_defaults_option(file_path, names, settings = {}) ||= OptionList.new @set_global_defaults = false @global_defaults_file = File.(file_path) @exit_on_global_save = !settings.has_key?(:exit_on_save) || settings[:exit_on_save] names << settings[:desc] if settings.has_key?(:desc) .opts.on(*names) { @set_global_defaults = true } end |
#help ⇒ Object
265 266 267 268 |
# File 'lib/everyday-cli-utils/option.rb', line 265 def help ||= OptionList.new .help end |
#help_option(names, settings = {}) ⇒ Object
225 226 227 228 229 230 231 |
# File 'lib/everyday-cli-utils/option.rb', line 225 def help_option(names, settings = {}) ||= OptionList.new @display_help = false @exit_on_print = !settings.has_key?(:exit_on_print) || settings[:exit_on_print] names << settings[:desc] if settings.has_key?(:desc) .opts.on(*names) { @display_help = true } end |
#help_str=(str) ⇒ Object
275 276 277 |
# File 'lib/everyday-cli-utils/option.rb', line 275 def help_str=(str) .help_str = str end |
#option(opt_name, names, settings = {}, &block) ⇒ Object
193 194 195 196 |
# File 'lib/everyday-cli-utils/option.rb', line 193 def option(opt_name, names, settings = {}, &block) ||= OptionList.new .register(:option, opt_name, names, settings, &block) end |
#option_list ⇒ Object
261 262 263 |
# File 'lib/everyday-cli-utils/option.rb', line 261 def option_list end |
#option_with_param(opt_name, names, settings = {}, &block) ⇒ Object
198 199 200 201 |
# File 'lib/everyday-cli-utils/option.rb', line 198 def option_with_param(opt_name, names, settings = {}, &block) ||= OptionList.new .register(:option_with_param, opt_name, names, settings, &block) end |
#options ⇒ Object
257 258 259 |
# File 'lib/everyday-cli-utils/option.rb', line 257 def .composite(:global, :local, :arg) end |
#opts ⇒ Object
253 254 255 |
# File 'lib/everyday-cli-utils/option.rb', line 253 def opts .opts end |
#parse!(argv = ARGV) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/everyday-cli-utils/option.rb', line 279 def parse!(argv = ARGV) ||= OptionList.new :global, YAML::load_file(@global_defaults_file) unless @global_defaults_file.nil? || !File.exist?(@global_defaults_file) :local, YAML::load_file(@defaults_file) unless @defaults_file.nil? || !File.exist?(@defaults_file) .parse!(argv) if @display_help puts help exit 0 if @exit_on_print end if @set_global_defaults IO.write(@global_defaults_file, .composite(:global, :arg).to_yaml) if @exit_on_global_save puts 'Global defaults set' exit 0 end elsif @set_defaults IO.write(@defaults_file, .composite(:local, :arg).to_yaml) if @exit_on_save puts 'Defaults set' exit 0 end end end |
#show_defaults_option(layers, names, settings = {}) ⇒ Object
221 222 223 |
# File 'lib/everyday-cli-utils/option.rb', line 221 def show_defaults_option(layers, names, settings = {}) end |
#to_s ⇒ Object
270 271 272 273 |
# File 'lib/everyday-cli-utils/option.rb', line 270 def to_s ||= OptionList.new .to_s end |