Class: Mercenary::Option

### Home Classes Methods

### In Files

- lib/mercenary/option.rb

### Parent

Object

### Methods

- ::new
- #eql?
- #for_option_parser
- #formatted_switches
- #hash
- #switches
- #to_s

### Class/Module Index

Quicksearch

- Mercenary
- Mercenary::Command
- Mercenary::Option
- Mercenary::Presenter
- Mercenary::Program

No matching classes.

# Mercenary::Option

### Attributes

config_key[R]
description[R]
long[R]
return_type[R]
short[R]

### Public Class Methods

new(config_key, info) click to toggle source

Public: Create a new Option

config_key - the key in the config hash to which the value of this option

    will map

info - an array containing first the switches, then an optional

    return type (e.g. Array), then a description of the option

Returns nothing

    # File lib/mercenary/option.rb, line 13
    def initialize(config_key, info)
      @config_key  = config_key
      while arg = info.shift
        begin
          @return_type = Object.const_get("#{arg}")
          next
        rescue NameError
        end
        if arg.start_with?("-")
          if arg.start_with?("--")
            @long = arg
          else
            @short = arg
          end
          next
        end
        @description = arg
      end
    end

### Public Instance Methods

eql?(other) click to toggle source

Public: Check equivalence of two Options based on equivalence of their

    instance variables

Returns true if all the instance variables are equal, false otherwise

    # File lib/mercenary/option.rb, line 71
    def eql?(other)
      return false unless self.class.eql?(other.class)
      instance_variables.map do |var|
        instance_variable_get(var).eql?(other.instance_variable_get(var))
      end.all?
    end

for_option_parser() click to toggle source

Public: Fetch the array containing the info OptionParser is interested in

Returns the array which OptionParser#on wants

    # File lib/mercenary/option.rb, line 36
    def for_option_parser
      [short, long, return_type, description].flatten.reject{ |o| o.to_s.empty? }
    end

formatted_switches() click to toggle source

Public: Build a beautifully-formatted string representation of the switches

Returns a formatted string representation of the switches

    # File lib/mercenary/option.rb, line 51
    def formatted_switches
      [
        switches.first.rjust(10),
        switches.last.ljust(13)
      ].join(", ").gsub(/ , /, '   ').gsub(/,   /, '    ')
    end

hash() click to toggle source

Public: Hash based on the hash value of instance variables

Returns a Fixnum which is unique to this Option based on the instance variables

    # File lib/mercenary/option.rb, line 61
    def hash
      instance_variables.map do |var|
        instance_variable_get(var).hash
      end.reduce(:^)
    end

switches() click to toggle source

Public: Fetch an array of switches, including the short and long versions

Returns an array of two strings. An empty string represents no switch in that position.

    # File lib/mercenary/option.rb, line 82
    def switches
      [short, long].map(&:to_s)
    end

to_s() click to toggle source

Public: Build a string representation of this option including the

    switches and description

Returns a string representation of this option

    # File lib/mercenary/option.rb, line 44
    def to_s
      "#{formatted_switches}  #{description}"
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.