class Rzo::Trollop::Option

The option for each flag

Constants

FLAG_TYPES

The set of values that indicate a flag option when passed as the :type parameter of opt.

MULTI_ARG_TYPES

The set of values that indicate a multiple-parameter option (i.e., that takes multiple space-separated values on the commandline) when passed as the :type parameter of opt.

SINGLE_ARG_TYPES

The set of values that indicate a single-parameter (normal) option when passed as the :type parameter of opt.

A value of io corresponds to a readable IO resource, including a filename, URI, or the strings 'stdin' or '-'.

TYPES

The complete set of legal values for the :type parameter of opt.

Attributes

name[RW]
opts[RW]

Public Class Methods

create(name, desc="", opts={}) click to toggle source
# File lib/rzo/trollop.rb, line 803
def self.create(name, desc="", opts={})
  new(name, desc, opts)
end
new(name, desc="", opts={}, &b) click to toggle source
# File lib/rzo/trollop.rb, line 665
def initialize(name, desc="", opts={}, &b)
  ## fill in :type
  opts[:type] = # normalize
    case opts[:type]
    when :boolean, :bool then :flag
    when :integer        then :int
    when :integers       then :ints
    when :double         then :float
    when :doubles        then :floats
    when Class
      case opts[:type].name
      when 'TrueClass',
           'FalseClass'  then :flag
      when 'String'      then :string
      when 'Integer'     then :int
      when 'Float'       then :float
      when 'IO'          then :io
      when 'Date'        then :date
      else
        raise ArgumentError, "unsupported argument type '#{opts[:type].class.name}'"
      end
    when nil             then nil
    else
      raise ArgumentError, "unsupported argument type '#{opts[:type]}'" unless TYPES.include?(opts[:type])
      opts[:type]
    end

  ## for options with :multi => true, an array default doesn't imply
  ## a multi-valued argument. for that you have to specify a :type
  ## as well. (this is how we disambiguate an ambiguous situation;
  ## see the docs for Parser#opt for details.)
  disambiguated_default = if opts[:multi] && opts[:default].kind_of?(Array) && !opts[:type]
    opts[:default].first
  else
    opts[:default]
  end

  type_from_default =
    case disambiguated_default
    when Integer     then :int
    when Numeric     then :float
    when TrueClass,
         FalseClass  then :flag
    when String      then :string
    when IO          then :io
    when Date        then :date
    when Array
      if opts[:default].empty?
        if opts[:type]
          raise ArgumentError, "multiple argument type must be plural" unless MULTI_ARG_TYPES.include?(opts[:type])
          nil
        else
          raise ArgumentError, "multiple argument type cannot be deduced from an empty array for '#{opts[:default][0].class.name}'"
        end
      else
        case opts[:default][0]    # the first element determines the types
        when Integer then :ints
        when Numeric then :floats
        when String  then :strings
        when IO      then :ios
        when Date    then :dates
        else
          raise ArgumentError, "unsupported multiple argument type '#{opts[:default][0].class.name}'"
        end
      end
    when nil         then nil
    else
      raise ArgumentError, "unsupported argument type '#{opts[:default].class.name}'"
    end

  raise ArgumentError, ":type specification and default type don't match (default type is #{type_from_default})" if opts[:type] && type_from_default && opts[:type] != type_from_default

  opts[:type] = opts[:type] || type_from_default || :flag

  ## fill in :long
  opts[:long] = opts[:long] ? opts[:long].to_s : name.to_s.gsub("_", "-")
  opts[:long] = case opts[:long]
    when /^--([^-].*)$/ then $1
    when /^[^-]/        then opts[:long]
    else                     raise ArgumentError, "invalid long option name #{opts[:long].inspect}"
  end

  ## fill in :short
  opts[:short] = opts[:short].to_s if opts[:short] && opts[:short] != :none
  opts[:short] = case opts[:short]
    when /^-(.)$/          then $1
    when nil, :none, /^.$/ then opts[:short]
    else                   raise ArgumentError, "invalid short option name '#{opts[:short].inspect}'"
  end

  if opts[:short]
    raise ArgumentError, "a short option name can't be a number or a dash" if opts[:short] =~ Trollop::Parser::INVALID_SHORT_ARG_REGEX
  end

  ## fill in :default for flags
  opts[:default] = false if opts[:type] == :flag && opts[:default].nil?

  ## autobox :default for :multi (multi-occurrence) arguments
  opts[:default] = [opts[:default]] if opts[:default] && opts[:multi] && !opts[:default].kind_of?(Array)

  ## fill in :multi
  opts[:multi] ||= false

  self.name = name
  self.opts = opts
end

Public Instance Methods

array_default?() click to toggle source

? def multi_default ; opts.default || opts.multi && [] ; end

# File lib/rzo/trollop.rb, line 791
def array_default? ; opts[:default].kind_of?(Array) ; end
callback() click to toggle source
# File lib/rzo/trollop.rb, line 798
def callback ; opts[:callback] ; end
default() click to toggle source
# File lib/rzo/trollop.rb, line 789
def default ; opts[:default] ; end
desc() click to toggle source
# File lib/rzo/trollop.rb, line 799
def desc ; opts[:desc] ; end
flag?() click to toggle source
# File lib/rzo/trollop.rb, line 777
def flag? ; type == :flag ; end
key?(name) click to toggle source
# File lib/rzo/trollop.rb, line 772
def key?(name)
  opts.key?(name)
end
long() click to toggle source
# File lib/rzo/trollop.rb, line 797
def long ; opts[:long] ; end
multi() click to toggle source
# File lib/rzo/trollop.rb, line 782
def multi ; opts[:multi] ; end
Also aliased as: multi?
multi?()
Alias for: multi
multi_arg?() click to toggle source
# File lib/rzo/trollop.rb, line 785
def multi_arg?
  MULTI_ARG_TYPES.include?(type)
end
required?() click to toggle source
# File lib/rzo/trollop.rb, line 801
def required? ; opts[:required] ; end
short() click to toggle source
# File lib/rzo/trollop.rb, line 793
def short ; opts[:short] ; end
short=(val) click to toggle source

not thrilled about this

# File lib/rzo/trollop.rb, line 796
def short=(val) ; opts[:short] = val ; end
short?() click to toggle source
# File lib/rzo/trollop.rb, line 794
def short? ; short && short != :none ; end
single_arg?() click to toggle source
# File lib/rzo/trollop.rb, line 778
def single_arg?
  SINGLE_ARG_TYPES.include?(type)
end
type() click to toggle source
# File lib/rzo/trollop.rb, line 776
def type ; opts[:type] ; end