Class: ALib::AbstractMain::Param

Inherits:
String
  • Object
show all
Includes:
Util::Casting
Defined in:
lib/alib-0.5.1/main.rb

Overview

param dsl and class

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Casting

#bool_cast, #float_cast, #float_list, #int_cast, #int_list, #pathname_cast, #re_cast, #string_cast, #string_list

Methods included from Util::Exporter

#export, included

Constructor Details

#initialize(name, this = nil, &block) ⇒ Param

Returns a new instance of Param.



750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/alib-0.5.1/main.rb', line 750

def initialize name, this = nil, &block
#--{{{
  if name.is_a?(Array)
    a, b, ignored = name
    a, b = a.to_s, b.to_s
    name = a.size > b.size ? a : b
    st = a.size > b.size ? b : a
    short st
  end

  name = name.to_s
  @this = this

  re = %r/\s*=\s*([^=]+)\s*$/
  m, @rhs = re.match(name).to_a
  name.gsub! re, '' if @rhs
  name.gsub! %r/^\s*--/, ''

  super(name.to_s.gsub(%r/^\s*--/,''))
  instance_eval &block if block
#--}}}
end

Class Method Details

.pattr(name) ⇒ Object



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/alib-0.5.1/main.rb', line 707

def self.pattr name
#--{{{
  module_eval <<-code
    def #{ name } *_a, &b
      _a.push lambda{ instance_eval &b } if b
      _a.size == 0 ? (defined? @#{ name } and @#{ name }) : (@#{ name }=_a.shift)
    end
    def #{ name }?
      defined?(@#{ name }) and @#{ name }
    end
    def #{ name }= val
      #{ name }( val )
    end
  code
#--}}}
end

Instance Method Details

#argument(what = :required) ⇒ Object Also known as: arg

–}}}



811
812
813
814
815
816
817
818
819
820
821
822
# File 'lib/alib-0.5.1/main.rb', line 811

def argument what = :required
#--{{{
  case what.to_s
    when /opt/i
      send "optional_argument", true
    when /req/i
      send "required_argument", true
    else
      raise "what <#{ what }>"
  end
#--}}}
end

#cast(&b) ⇒ Object



824
825
826
827
828
829
# File 'lib/alib-0.5.1/main.rb', line 824

def cast &b
#--{{{
  b and @cast = b
  cast?  ? @cast : nil
#--}}}
end

#cast?Boolean

–}}}

Returns:

  • (Boolean)


830
831
832
833
834
# File 'lib/alib-0.5.1/main.rb', line 830

def cast?
#--{{{
  defined? @cast
#--}}}
end

#longObject

–}}}



772
773
774
775
776
# File 'lib/alib-0.5.1/main.rb', line 772

def long
#--{{{
  @long ||= "--#{ self }"
#--}}}
end

#param(name) ⇒ Object

–}}}



835
836
837
838
839
# File 'lib/alib-0.5.1/main.rb', line 835

def param name # call up!
#--{{{
  @this.param name
#--}}}
end

#to_aObject

–}}}



777
778
779
780
781
# File 'lib/alib-0.5.1/main.rb', line 777

def to_a
#--{{{
  [ long, short, description ].compact
#--}}}
end

#to_optionObject

–}}}



782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# File 'lib/alib-0.5.1/main.rb', line 782

def to_option
#--{{{
  optspec = []

  if long
    s = "#{ long }"
    if required_argument
      s << "=#{ self }"
    elsif optional_argument
      s << "=[#{ self }]"
    elsif rhs
      s << "=#{ rhs }" unless s[%r/=/]
    end
    optspec << s
  end

  if short
    s = "#{ short }".strip.gsub(%r/^-*/,'-')
    optspec << s
  end

  if description 
    s = "#{ description }"
    optspec << s
  end

  optspec
#--}}}
end

#value(this = self) ⇒ Object

–}}}



840
841
842
843
844
845
846
847
848
# File 'lib/alib-0.5.1/main.rb', line 840

def value this = self
#--{{{
  if defined? @default
    @default.respond_to?('call') ? @default.call : @default
  else
    nil
  end
#--}}}
end