module Filigree::Configuration::ClassMethods
Class Methods #
Attributes
@return [Hash<Symbol, Block>] Hash of names to blocks used for auto configuration
@return [Hash<String, Option>] Hash of options with long names used as keys
@return [Hash<String, Option>] hash of options with short name used as keys
Public Class Methods
Callbacks #
# File lib/filigree/configuration.rb, line 345 def self.extended(klass) klass.install_icvars end
Public Instance Methods
Add an option to the necessary data structures.
@param [Option] opt Option
to add
@return [void]
# File lib/filigree/configuration.rb, line 215 def add_option(opt) attr_accessor opt.long @options_long[opt.long] = opt @options_short[opt.short] = opt unless opt.short.nil? end
Define an automatic configuration variable.
@param [Symbol] name Name of the configuration variable @param [Proc] block Block to be executed to generate the value
@return [void]
# File lib/filigree/configuration.rb, line 228 def auto(name, &block) @auto_blocks[name.to_sym] = block end
Define a boolean option. The variable will be set to true if the flag is seen and be false otherwise.
@param [String] long Long name of the option @param [String] short Short name of the option
@return [void]
# File lib/filigree/configuration.rb, line 239 def bool_option(long, short = nil) @next_default = false option(long, short) { true } end
Sets the default value for the next command. If a block is provided it will be used. If not, the val parameter will be.
@param [Object] val Default value @param [Proc] block Default value generator block
@return [void]
# File lib/filigree/configuration.rb, line 251 def default(val = nil, &block) @next_default = block ? block : val end
Sets the help string for the next command.
@param [String] str Command help string
@return [void]
# File lib/filigree/configuration.rb, line 260 def help(str) @help_string = str end
Install the instance class variables in the including class.
@return [void]
# File lib/filigree/configuration.rb, line 267 def install_icvars @auto_blocks = Hash.new @help_string = '' @next_default = nil @next_required = false @options_long = Hash.new @options_short = Hash.new @required = Array.new @usage = '' end
Define a new option.
@param [String] long Long option name @param [String] short Short option name @param [Array<Symbol>] conversions List of methods used to convert string arguments @param [Proc] block Block used when the option is encountered
@return [void]
# File lib/filigree/configuration.rb, line 286 def option(long, short = nil, conversions: nil, &block) long = long.to_s.gsub('-', '_') short = short.to_s if short add_option Option.new(long, short, @help_string, @next_default, conversions.nil? ? block : conversions) @required << long.to_sym if @next_required # Reset state between option declarations. @help_string = '' @next_default = nil @next_required = false end
Mark some options as required. If no names are provided then the next option to be defined is required; if names are provided they are all marked as required.
@param [Symbol] names Options to be marked as required.
@return [void]
# File lib/filigree/configuration.rb, line 308 def required(*names) if names.empty? @next_required = true else @required += names end end
@return [Array<Symbol>] Options that need to be marked as required
# File lib/filigree/configuration.rb, line 317 def required_options @required end
Define an option that takes a single string argument.
@param [String] long Long option name @param [String] short Short option name
@return [void]
# File lib/filigree/configuration.rb, line 327 def string_option(long, short = nil) option(long, short) { |str| str } end
Add's a usage string to the entire configuration object. If no string is provided the current usage string is returned.
@param [String, nil] str Usage string
@return [String] Current or new usage string
# File lib/filigree/configuration.rb, line 337 def usage(str = nil) if str then @usage = str else @usage end end