Class: Getopt::Declare::Arg

Inherits:
Object
  • Object
show all
Defined in:
lib/Getopt/Declare.rb

Overview

Class used to handle other arguments (flags, etc)

Constant Summary collapse

Helpcmd =
%w( -help --help -Help --Help -HELP --HELP -h -H )
Versioncmd =
%w( -version --version -Version --Version
-VERSION --VERSION -v -V )
@@nextid =
0
@@helpcmdH =
{}
@@versioncmdH =
{}
@@flags =
[]
@@posflagpat =
nil
@@negflagpat =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec, desc, dittoflag) ⇒ Arg

Constructor



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/Getopt/Declare.rb', line 544

def initialize(spec, desc, dittoflag)
  first = 1


  @@nextid += 1
  @flag 	= ''
  @foundid  = nil
  @args	= []
  @actions  = []
  @ditto	= dittoflag
  @required = false
  @requires = nil
  @id       = @@nextid
  @desc	= spec.dup
  @items	= 0
  @nocase   = false

  @desc.sub!(/\A\s*(.*?)\s*\Z/,'\1')

  while spec && spec != ''
	begin

	  # OPTIONAL
	  if spec.sub!( /\A(\s*)\[/, '\1' )
 @args.push( StartOpt.new )
 next
	  elsif spec.sub!(/\A\s*\]/,"")
 @args.push( EndOpt.new )
 next
	  end

	  # ARG

	  se  = DelimScanner::new( spec )
	  tmp = se.scanBracketed('<>')

	  arg = nows = nil
	  arg, spec, nows = tmp[:match], tmp[:suffix], tmp[:prefix] if tmp


	  if arg
 arg =~ /\A(\s*)(<)([a-zA-Z]\w*)(:[^>]+|)>/ or
   raise "Error: bad Getopt::Declare parameter variable specification near '#{arg}'\n"

        # NAME,TYPE,NOW
 details = [ "#$3", "#$4", !first && !(nows.length>0) ]

 if spec && spec.sub!( /\A\.\.\./, "")	# ARRAY ARG
   @args.push( ArrayArg.new(*details) )
 else  # SCALAR ARG
   @args.push( ScalarArg.new(*details) )
 end
 @items += 1
 next

 # PUNCTUATION
	  elsif spec.sub!( /\A(\s*)((\\.|[^\] \t\n\[<])+)/, '' )
 ows, punct = $1, $2
 punct.gsub!( /\\(?!\\)(.)/, '\1' )

 if first
          spec  =~ /\A(\S+)/
          @foundid = "#{punct}#{$1}"
   @flag = punct
   @@flags.push( punct )
 else
   @args.push( Punctuator.new(punct, !(ows.size > 0)) )
   @items += 1
 end

	  else 
 break
	  end # if arg/spec.sub
	ensure
	  first = nil
	end
  end # while

  @@helpcmdH.delete(@flag)    if @@helpcmdH.key?(@flag)
  @@versioncmdH.delete(@flag) if @@versioncmdH.key?(@flag)
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



526
527
528
# File 'lib/Getopt/Declare.rb', line 526

def actions
  @actions
end

#argsObject

Returns the value of attribute args.



526
527
528
# File 'lib/Getopt/Declare.rb', line 526

def args
  @args
end

#descObject

Returns the value of attribute desc.



527
528
529
# File 'lib/Getopt/Declare.rb', line 527

def desc
  @desc
end

#dittoObject

Returns the value of attribute ditto.



526
527
528
# File 'lib/Getopt/Declare.rb', line 526

def ditto
  @ditto
end

#flagObject

Returns the value of attribute flag.



526
527
528
# File 'lib/Getopt/Declare.rb', line 526

def flag
  @flag
end

#idObject

Returns the value of attribute id.



527
528
529
# File 'lib/Getopt/Declare.rb', line 527

def id
  @id
end

#nocaseObject

Returns the value of attribute nocase.



526
527
528
# File 'lib/Getopt/Declare.rb', line 526

def nocase
  @nocase
end

#repeatableObject

Returns the value of attribute repeatable.



527
528
529
# File 'lib/Getopt/Declare.rb', line 527

def repeatable
  @repeatable
end

#requiredObject

Returns the value of attribute required.



527
528
529
# File 'lib/Getopt/Declare.rb', line 527

def required
  @required
end

#requiresObject

Returns the value of attribute requires.



528
529
530
# File 'lib/Getopt/Declare.rb', line 528

def requires
  @requires
end

Class Method Details

.besthelpObject



471
472
473
# File 'lib/Getopt/Declare.rb', line 471

def Arg.besthelp
  for i in Helpcmd; return i if @@helpcmdH[i]; end
end

.bestversionObject



486
487
488
# File 'lib/Getopt/Declare.rb', line 486

def Arg.bestversion
  for i in Versioncmd; return i if @@versioncmdH[i]; end
end

.clearObject



499
500
501
502
503
504
# File 'lib/Getopt/Declare.rb', line 499

def Arg.clear
  @@flags = []
  @@nextid = 0
  @@posflagpat  = nil
  @@negflagpath = nil
end

.helppatObject

Create regex of help flags based on help shortcuts left



476
477
478
# File 'lib/Getopt/Declare.rb', line 476

def Arg.helppat
  @@helpcmdH.keys.join('|')
end

.negflagpat(*t) ⇒ Object

Return string with regex that avoids all flags in declaration



507
508
509
510
511
512
513
514
# File 'lib/Getopt/Declare.rb', line 507

def Arg.negflagpat(*t)
  if !@@negflagpat && @@flags
	@@negflagpat = ( @@flags.map { |i| 
 "(?!" + Regexp::quote(i) + ")" } ).join('')
  else
	@@negflagpat
  end
end

.posflagpat(*t) ⇒ Object

Return string with regex that matches any of the flags in declaration



517
518
519
520
521
522
523
524
# File 'lib/Getopt/Declare.rb', line 517

def Arg.posflagpat(*t)
  if !@@posflagpat && @@flags
	@@posflagpat = '(?:' + ( @@flags.map { |i| 
  Regexp::quote(i) } ).join('|') + ')'
  else
	@@posflagpat
  end
end

.versionpatObject

Create regex of version flags based on help shortcuts left



491
492
493
# File 'lib/Getopt/Declare.rb', line 491

def Arg.versionpat
  @@versioncmdH.keys.join('|')
end

Instance Method Details

#code(*t) ⇒ Object

Return String with code to parse this argument (ie. flag)



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
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
753
754
755
756
757
758
759
760
761
762
# File 'lib/Getopt/Declare.rb', line 629

def code(*t)
  owner = t[0]
  mod   = t[1]


  code = "\n"
  flag = @flag
  clump = owner.clump
  i = 0
  nocasei = ((Getopt::Declare::nocase || @nocase) ? 'i' : '')

  code << "          catch(:paramout) do\n            while "
  code += !@repeatable? "!_FOUND_['" + self.foundid + "']" : "true"

  if (flag && (clump==1 && flag !~ /\A[^a-z0-9]+[a-z0-9]\Z/i ||
 (clump<3 && @args.size > 0 )))
	code << ' and !_lastprefix'
  end

  code << '
          begin
            catch(:param) do
              _pos = _nextpos if _args
              _PUNCT_ = {}
          '

  if flag != ''
    # This boundary is to handle -- option, so that if user uses
    # --foo and --foo is not a flag, it does not become
    # --  and unused: 'foo', but an error saying flag '--foo' not defined.
    boundary = ''
    boundary = '(\s+|\Z)' if flag =~ /^(--|-|\+|\+\+)$/

	code << '
              _args && _pos = gindex( _args, /\G[\s|\0]*' + 
	  Regexp::quote(flag) + boundary + '/' + nocasei + ", _pos) or throw(:paramout) 
              unless @_errormsg
  @_errormsg = %q|incorrect specification of '" + flag + "' parameter|
              end
"
  elsif ( ScalarArg::stdtype(@args[0].type)||'') !~ /\%F/
	code << "\n                  throw(:paramout) if @_errormsg\n"
  end


  code << "\n                  _PARAM_ = '" + self.name + "'\n"


  trailer = []
  i = @args.size-1
  while i > 0
	trailer[i-1] = @args[i].trailer
	trailer[i-1] = trailer[i] unless trailer[i-1]
	i -= 1
  end # while i

  if @args
	code << "\n"+'                 _args && _pos = gindex( _args, /\G'

	@args.each_with_index { |arg, j|
	  code << arg.ows(arg.matcher(trailer[j]))
	}

	code << '/x' + nocasei + ", _pos ) or throw(:paramout)\n"
  end # if @args

  @args.each_with_index { |arg, j|
	code << arg.code(j,mod)	#, $flag ????
  }

  if flag
	mutexlist = owner.mutex[flag] ? 
	(  owner.mutex[flag].map {|j| "'#{j}'"} ).join(',') : ''

	code << "
              if _invalid.has_key?('#{flag}')
                @_errormsg = %q|parameter '#{flag}' not allowed with parameter '| + _invalid['#{flag}'] + %q|'|
                throw(:paramout)
              else
  for i in [#{mutexlist}]
      _invalid[i] = '#{flag}'
                end
              end  #if/then

"
  end



  for action in @actions
	#action.sub!( /(\s*\{)/, '\1 module '+mod )  # @TODO
	code << "\n                  " + action + "\n"
  end

  if flag && @items==0
	code << "\n                  @cache['#{flag}'] = '#{flag}'\n"
    if @ditto
      code << "\n                  @cache['#{@ditto.flag}'] = '#{flag}'\n"
    end
  end

  if @items > 1
	code << "                  @cache['#{self.name}'] = {} unless @cache['#{self.name}'].kind_of?(Hash)\n"
    if @ditto
      code << "\n                  @cache['#{@ditto.name}'] = {} unless @cache['#{@ditto.name}'].kind_of?(Hash)\n"
    end
  end

  for subarg in @args
	code << subarg.cachecode(self.name,@items)
    if ditto
	code << subarg.cachecode(@ditto.name,@items)
    end
  end

  if flag =~ /\A([^a-z0-9]+)/i
	code << '                  _lastprefix = "'+ Regexp::quote("#$1") + '"' + "\n"
  else
	code << "                  _lastprefix = nil\n"
  end

  code << "
              _FOUND_['"+ self.foundid + "'] = 1
              throw :arg if _pos > 0
_nextpos = _args.size
              throw :alldone
            end  # catch(:param)
   end  # begin
        end # while
      end # catch(:paramout)
"

  code
end

#found_requiresObject



532
533
534
535
536
537
538
539
540
# File 'lib/Getopt/Declare.rb', line 532

def found_requires
  expr = @requires.gsub(/((?:&&|\|\|)?\s*(?:[!(]\s*)*)([^ \t\n|&\)]+)/x,
                        '\1_FOUND_[\'\2\']')
  
  if !valid_syntax?( expr )
    raise "Error: bad condition in [requires: #{original}]\n"
  end
  expr
end

#foundidObject

Return foundid of argument, which can be flag’s name or variable’s name



774
775
776
# File 'lib/Getopt/Declare.rb', line 774

def foundid
  return @foundid || self.name
end

#nameObject

Return name of argument, which can be flag’s name or variable’s name



765
766
767
768
769
770
771
# File 'lib/Getopt/Declare.rb', line 765

def name
  return @flag unless @flag.empty?
  for i in @args
    return "<#{i.name}>" if i.respond_to?(:name)
  end
  raise "Unknown flag name for parameter #{self.desc}"
end