Class: Trollop::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/darksky-ruby/trollop.rb

Overview

The option for each flag

Constant Summary collapse

FLAG_TYPES =

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

[:flag, :bool, :boolean]
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 ‘-’.

[:int, :integer, :string, :double, :float, :io, :date]
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.

[:ints, :integers, :strings, :doubles, :floats, :ios, :dates]
TYPES =

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

FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, desc = "", opts = {}, &b) ⇒ Option

Returns a new instance of Option.

Raises:

  • (ArgumentError)


647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/darksky-ruby/trollop.rb', line 647

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



645
646
647
# File 'lib/darksky-ruby/trollop.rb', line 645

def name
  @name
end

#optsObject

Returns the value of attribute opts.



645
646
647
# File 'lib/darksky-ruby/trollop.rb', line 645

def opts
  @opts
end

Class Method Details

.create(name, desc = "", opts = {}) ⇒ Object



785
786
787
# File 'lib/darksky-ruby/trollop.rb', line 785

def self.create(name, desc="", opts={})
  new(name, desc, opts)
end

Instance Method Details

#array_default?Boolean

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

Returns:

  • (Boolean)


773
# File 'lib/darksky-ruby/trollop.rb', line 773

def array_default? ; opts[:default].kind_of?(Array) ; end

#callbackObject



780
# File 'lib/darksky-ruby/trollop.rb', line 780

def callback ; opts[:callback] ; end

#defaultObject



771
# File 'lib/darksky-ruby/trollop.rb', line 771

def default ; opts[:default] ; end

#descObject



781
# File 'lib/darksky-ruby/trollop.rb', line 781

def desc ; opts[:desc] ; end

#flag?Boolean

Returns:

  • (Boolean)


759
# File 'lib/darksky-ruby/trollop.rb', line 759

def flag? ; type == :flag ; end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


754
755
756
# File 'lib/darksky-ruby/trollop.rb', line 754

def key?(name)
  opts.key?(name)
end

#longObject



779
# File 'lib/darksky-ruby/trollop.rb', line 779

def long ; opts[:long] ; end

#multiObject Also known as: multi?



764
# File 'lib/darksky-ruby/trollop.rb', line 764

def multi ; opts[:multi] ; end

#multi_arg?Boolean

Returns:

  • (Boolean)


767
768
769
# File 'lib/darksky-ruby/trollop.rb', line 767

def multi_arg?
  MULTI_ARG_TYPES.include?(type)
end

#required?Boolean

Returns:

  • (Boolean)


783
# File 'lib/darksky-ruby/trollop.rb', line 783

def required? ; opts[:required] ; end

#shortObject



775
# File 'lib/darksky-ruby/trollop.rb', line 775

def short ; opts[:short] ; end

#short=(val) ⇒ Object

not thrilled about this



778
# File 'lib/darksky-ruby/trollop.rb', line 778

def short=(val) ; opts[:short] = val ; end

#short?Boolean

Returns:

  • (Boolean)


776
# File 'lib/darksky-ruby/trollop.rb', line 776

def short? ; short && short != :none ; end

#single_arg?Boolean

Returns:

  • (Boolean)


760
761
762
# File 'lib/darksky-ruby/trollop.rb', line 760

def single_arg?
  SINGLE_ARG_TYPES.include?(type)
end

#typeObject



758
# File 'lib/darksky-ruby/trollop.rb', line 758

def type ; opts[:type] ; end