module Filigree::Configuration::ClassMethods

Class Methods #

Attributes

auto_blocks[R]

@return [Hash<Symbol, Block>] Hash of names to blocks used for auto configuration

options_long[R]

@return [Hash<String, Option>] Hash of options with long names used as keys

options_short[R]

@return [Hash<String, Option>] hash of options with short name used as keys

Public Class Methods

extended(klass) click to toggle source

Callbacks #

# File lib/filigree/configuration.rb, line 345
def self.extended(klass)
        klass.install_icvars
end

Public Instance Methods

add_option(opt) click to toggle source

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
auto(name, &block) click to toggle source

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
bool_option(long, short = nil) click to toggle source

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
default(val = nil, &block) click to toggle source

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
help(str) click to toggle source

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_icvars() click to toggle source

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
option(long, short = nil, conversions: nil, &block) click to toggle source

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
required(*names) click to toggle source

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
required_options() click to toggle source

@return [Array<Symbol>] Options that need to be marked as required

# File lib/filigree/configuration.rb, line 317
def required_options
        @required
end
string_option(long, short = nil) click to toggle source

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
usage(str = nil) click to toggle source

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