Class: Getopt::Declare

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

Overview

Main Class

Defined Under Namespace

Classes: Arg, ArrayArg, EndOpt, Punctuator, ScalarArg, StartOpt

Constant Summary collapse

VERSION =
'1.32'
@@debug =

For debugging, use [debug] and it will output the ruby code as .CODE.rb

false
@@separator =

Main separator used to distinguish arguments in Getopt/Declare spec. By default, one or more tabs or 3 spaces or more.

'(?:\t+| {3}[^<])'
@@m =
[]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ Declare

Constructor



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
# File 'lib/Getopt/Declare.rb', line 997

def initialize(*opts)
  @cache = nil

  Getopt::Declare::Arg::clear

  # HANDLE SHORT-CIRCUITS
  return if opts.size==2 && (!opts[1] || opts[1] == '-SKIP') 

  grammar, source = opts

  if grammar.nil?
    raise "Error: No grammar description provided."
  end

  ### REMOVED PREDEF GRAMMAR AS IT WAS NOT DOCUMENTED NOR 
  ### WORKING IN PERL'S Declare.pm VERSION.

  # PRESERVE ESCAPED '['s
  grammar.gsub!(/\\\[/,"\255")

  # MAKE SURE GRAMMAR ENDS WITH A NEWLINE.
  grammar.sub!(/([^\n])\Z/,'\1'+"\n")

  @usage   = grammar.dup

  # SET-UP
  i = grammar
  _args = []
  _mutex = {}
  _strict = false
  _all_repeatable = false
  _lastdesc = nil
  arg = nil
  Getopt::Declare::nocase = false
  Getopt::Declare::ScalarArg::_reset_stdtype


  # CONSTRUCT GRAMMAR
  while i.length > 0

    # COMMENT:
    i.sub!(/\A[ \t]*#.*\n/,"") and next


    # TYPE DIRECTIVE:
    se  = DelimScanner::new( i )

    if i =~ /\A\s*\[\s*pvtype:/ 
	_action = se.extractBracketed("[")
	if _action
 i.sub!( Regexp::quote( _action ).to_re, "" )   ### @GGA: added
 i.sub!(/\A[ \t]*\n/,"")                        ### @GGA: added
 _action.sub!(/.*?\[\s*pvtype:\s*/,"")
 _typedef(_action)
 next
	end # if
    end

    # ACTION  
    codeblockDelimiters = {
	'{'     => '}',
    }

    _action = se.extractCodeblock(codeblockDelimiters)
    if _action
	i.sub!( Regexp::quote(_action ).to_re, "" )
	i.sub!(/\A[ \t]*\n/,"")
	_action = _action[1..-2]

	if !valid_syntax?( _action )
        raise "Error: bad action in Getopt::Declare specification:" +
   "\n\n#{_action}\n\n\n"
	end

	if _args.length == 0
 raise "Error: unattached action in Getopt::Declare specification:\n#{_action}\n" +
       "\t(did you forget the tab after the preceding parameter specification?)\n"
	end

	_args.last.actions.push( _action )
	next
    elsif i =~ /\A(\s*[{].*)/
	raise "Error: incomplete action in Getopt::Declare specification:\n$1.....\n" +
     "\t(did you forget a closing '}'?)\n"
    end


    # ARG + DESC:
    if i.sub!(re_argument,"")
	spec = "#$1".strip
	desc = "#$2"
	_strict ||= desc =~ /\[\s*strict\s*\]/

      while i.sub!(re_more_desc,"")
        desc += "#$1"
      end
	
	ditto = nil
      if _lastdesc and desc.sub!(/\A\s*\[\s*ditto\s*\]/,_lastdesc)
        ditto = arg
      else
        _lastdesc = desc
      end

      # Check for GNU spec line like:  -d, --debug
      arg = nil
      if spec =~ /(-[\w]+),\s+(--?[\w]+)(\s+.*)?/
        specs = ["#$1#$3", "#$2#$3"]
        specs.each { |sp|
          arg = Arg.new(sp,desc,ditto)
          _args.push( arg )
          _infer(desc, arg, _mutex)
          ditto = arg
        }
      else
        arg = Arg.new(spec,desc,ditto)
        _args.push( arg )
        _infer(desc, arg, _mutex)
      end


	next
    end

    # OTHERWISE: DECORATION
    i.sub!(/((?:(?!\[\s*pvtype:).)*)(\n|(?=\[\s*pvtype:))/,"")
    decorator = "#$1"
    _strict ||= decorator =~ /\[\s*strict\s*\]/
    _infer(decorator, nil, _mutex)

    _all_repeatable = true if decorator =~ /\[\s*repeatable\s*\]/
    @@debug = true if decorator =~ /\[\s*debug\s*\]/

  end # while i.length



  _lastactions = nil
  for i in _args
    if _lastactions && i.ditto && i.actions.size == 0
	i.actions = _lastactions
    else
	_lastactions = i.actions
    end

    if _all_repeatable
	i.repeatable = 1
    end
  end

  # Sort flags based on criteria described in docs
  # Sadly, this cannot be reduced to sort_by
   _args = _args.sort() { |a,b|
    cond1 = ( b.flag.size <=> a.flag.size )
    cond2 = ( b.flag == a.flag and ( b.args.size <=> a.args.size ) )
    cond3 = ( a.id <=> b.id )
    cond1 = nil if cond1 == 0
    cond2 = nil if cond2 == 0
    cond1 or cond2 or cond3
  }

  # Handle clump
  clump = (@usage =~ /\[\s*cluster:\s*none\s*\]/i)     ? 0 :
    (@usage =~ /\[\s*cluster:\s*singles?\s*\]/i) ? 1 :
    (@usage =~ /\[\s*cluster:\s*flags?\s*\]/i)   ? 2 :
    (@usage =~ /\[\s*cluster:\s*any\s*\]/i)      ? 3 :
    (@usage =~ /\[\s*cluster:(.*)\s*\]/i)  	 ? "r" : 3
  raise "Error: unknown clustering mode: [cluster:#$1]\n" if clump == "r"

  # CONSTRUCT OBJECT ITSELF
  @args    = _args
  @mutex   = _mutex
  @helppat = Arg::helppat()
  @verspat = Arg::versionpat()

  @strict  = _strict
  @clump   = clump
  @source  = ''
  @tight   = @usage =~ /\[\s*tight\s*\]/i
  @caller  = caller()

  # VESTIGAL DEBUGGING CODE
  if @@debug
    f = File.new(".CODE.rb","w") and
	f.puts( code() ) and
	f.close() 
  end

  # DO THE PARSE (IF APPROPRIATE)
  if opts.size == 2
    return nil unless parse(source)
  else
    return nil unless parse()
  end

end

Class Attribute Details

.nocaseObject

Returns the value of attribute nocase.



785
786
787
# File 'lib/Getopt/Declare.rb', line 785

def nocase
  @nocase
end

Instance Attribute Details

#clumpObject (readonly)

Returns the value of attribute clump.



1735
1736
1737
# File 'lib/Getopt/Declare.rb', line 1735

def clump
  @clump
end

#helppatObject (readonly)

Returns the value of attribute helppat.



1732
1733
1734
# File 'lib/Getopt/Declare.rb', line 1732

def helppat
  @helppat
end

#mutexObject (readonly)

Returns the value of attribute mutex.



1731
1732
1733
# File 'lib/Getopt/Declare.rb', line 1731

def mutex
  @mutex
end

#sourceObject (readonly)

Returns the value of attribute source.



1736
1737
1738
# File 'lib/Getopt/Declare.rb', line 1736

def source
  @source
end

#strictObject (readonly)

Returns the value of attribute strict.



1734
1735
1736
# File 'lib/Getopt/Declare.rb', line 1734

def strict
  @strict
end

#unusedObject

Returns the value of attribute unused.



1453
1454
1455
# File 'lib/Getopt/Declare.rb', line 1453

def unused
  @unused
end

#verspatObject (readonly)

Returns the value of attribute verspat.



1733
1734
1735
# File 'lib/Getopt/Declare.rb', line 1733

def verspat
  @verspat
end

Instance Method Details

#[](name) ⇒ Object

Operator to easily return cache of Getopt::Declare



1717
1718
1719
1720
1721
1722
1723
# File 'lib/Getopt/Declare.rb', line 1717

def [](name)
  if @cache
    return @cache[name]
  else
    return nil
  end
end

#[]=(name, val) ⇒ Object

Operator to easily create new value in of Getopt::Declare



1711
1712
1713
1714
# File 'lib/Getopt/Declare.rb', line 1711

def []=(name,val)
  @cache = {} unless @cache
  @cache[name] = val
end

#code(*t) ⇒ Object

Main method to generate code to be evaluated for parsing.



1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
# File 'lib/Getopt/Declare.rb', line 1465

def code(*t)
  package = t[0] || ''
  code = %q%


@_deferred = []
@_errormsg = nil
@_finished = nil

begin

begin
  undef :defer
  undef :reject
  undef :finish
rescue
end

def defer(&i)
  @_deferred.push( i )
end

def reject(*i)
  if !i || i[0]
    @_errormsg = i[1] if i[1]
    throw :paramout
  end
end

def finish(*i)
  if i.size
    @_finished = i
  else
    @_finished = true
  end
end

@unused = []
@cache  = {}
_FOUND_ = {}
_errors = 0
_invalid = {}
_lastprefix = nil

_pos     = 0   # current position to match from
_nextpos = 0   # next position to match from

catch(:alldone) do 
  while !@_finished
    begin
	catch(:arg) do
 @_errormsg = nil

 # This is used for clustering of flags
 while _lastprefix
   substr = _args[_nextpos..-1]
   substr =~ /^(?!\s|\0|\Z)% +
Getopt::Declare::Arg::negflagpat() + %q%/ or
     begin 
_lastprefix=nil
break
     end
   "#{_lastprefix}#{substr}" =~ /^(% +
Getopt::Declare::Arg::posflagpat() + %q%)/ or
     begin 
_lastprefix=nil
break
     end
   _args = _args[0.._nextpos-1] + _lastprefix + _args[_nextpos..-1]
   break
 end #  while _lastprefix

 % + '' + %q%
 _pos = _nextpos if _args

 usage(0) if _args && gindex(_args,/\G(% + @helppat + %q%)(\s|\0|\Z)/,_pos)
        version(0) if _args && _args =~ /\G(% + @verspat + %q%)(\s|\0|\Z)/
    %

 for arg in @args
   code << arg.code(self,package)
 end

 code << %q%

	if _lastprefix
         _pos = _nextpos + _lastprefix.length()
  _lastprefix = nil
  next
      end

 _pos = _nextpos

 _args && _pos = gindex( _args, /\G[\s|\0]*(\S+)/, _pos ) or throw(:alldone)

 if @_errormsg
           $stderr.puts( "Error#{source}: #{@_errormsg}\n" )
        else
           @unused.push( @@m[0] )
        end

 _errors += 1 if @_errormsg

      end  # catch(:arg)

    ensure  # begin
      _pos = 0 if _pos.nil?
	_nextpos = _pos if _args
	if _args and _args.index( /\G(\s|\0)*\Z/, _pos )
 _args = _get_nextline.call(self) if !@_finished
        throw(:alldone) unless _args
        _pos = _nextpos = 0
        _lastprefix = ''
	end   # if
    end   # begin/ensure
  end   # while @_finished
end   # catch(:alldone)
end  # begin

%


   ################################
   # Check for required arguments #
   ################################
 for arg in @args
   next unless arg.required

   code << %q%unless _FOUND_['% + arg.name + %q%'] %

            if @mutex[arg.name]
              for m in @mutex[arg.name]
                code << %q# or _FOUND_['# + m + %q#']#
              end
            end

   code << %q%
 $stderr.puts "Error#{@source}: required parameter '% + arg.name + %q%' not found."
 _errors += 1
end
%

 end

   ########################################
   # Check for arguments requiring others #
   ########################################

 for arg in @args
   next unless arg.requires

   code << %q%
if _FOUND_['% + arg.name + %q%'] && !(% + arg.found_requires +
     %q%)
 $stderr.puts "Error#{@source}: parameter '% + arg.name + %q%' can only be specified with '% + arg.requires + %q%'"
 _FOUND_['% + arg.name + %q%'] = nil
 @args['% + arg.name + %q%'] = nil
 _errors += 1
end
          %
 end

 code << %q%
#################### Add unused arguments
if _args && _nextpos > 0 && _args.length() > 0
  @unused.replace( @unused + _args[_nextpos..-1].split(' ') )
end

for i in @unused
  i.tr!( "\0", " " )
end

%

        if @strict
   code << %q%
#################### Handle strict flag
unless _nextpos < ( _args ? _args.length : 0 )
for i in @unused
  $stderr.puts "Error#{@source}: unrecognizable argument ('#{i}')"
  _errors += 1
end
end
%
 end

        code << %q%
#################### Print help hint
if _errors > 0 && [email protected]?
$stderr.puts "\n(try '#$0 % + Getopt::Declare::Arg::besthelp + %q%' for more information)"
end

## cannot just assign unused to ARGV in ruby
unless @source != ''
ARGV.clear
@unused.map { |j| ARGV.push(j) }
end

unless _errors > 0
for i in @_deferred
  begin
    i.call
  rescue => e
    STDERR.puts "Action in Getopt::Declare specification produced:\n#{e}"
    _errors += 1
  end
end
end

!(_errors>0)  # return true or false (false for errors)

%
	return code
end

#each(&t) ⇒ Object

Iterator for Getopt::Declare (travels thru all cache keys)



1706
1707
1708
# File 'lib/Getopt/Declare.rb', line 1706

def each(&t)
  @cache.each(&t)
end

#inspectObject

Inspect cache (not the declare object)



1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
# File 'lib/Getopt/Declare.rb', line 1682

def inspect
  return nil if !@cache 
  t = ''

  @cache.each { |a,b|
    t << a + " => "
    case b
    when Hash
	t << "{"
	i = []
	b.each { |c,d|
 i.push( " '#{c}' => " + d.inspect )
	}
	t << i.join(',')
	t << " }"
    else
	t << b.inspect
    end
    t << "\n"
  }
  t << "Unused: " + unused.join(', ')
end

#parse(*opts) ⇒ Object

Parse the parameter description and in some cases, optionally eval it, too.



1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
# File 'lib/Getopt/Declare.rb', line 1197

def parse(*opts)
  source = opts[0]
  _args = nil
  _get_nextline = proc { nil }

  if source
    case source
    when Method
	_get_nextline = source
	_args = _get_nextline.call(self)
	source = '[METHOD]'
    when Proc
	_get_nextline = source
	_args = _get_nextline.call(self)
	source = '[PROC]'
    when IO
	if source.fileno > 0 && source.tty?
 _get_nextline = method(:_get_nextline)
 _args = $stdin.readline
 source = '<STDIN>'
	else
 _args = source.readlines.join(' ')
 _args.tr!('\t\n',' ')
	end
    when :build, :skip
      return 0
    when Array
	if source.length() == 1 && !source[0] ||
   source[0] == "-BUILD" ||
   source[0] == "-SKIP"
 return 0
	elsif source.length() == 2 && source[0] == "-ARGV"
 if !source[1] or !source[1] === Array
   raise 'Error: parse(["-ARGV"]) not passed an array as second parameter.'
 end
 _args  = source[1].map { |i| i.tr( " \t\n", "\0\0\0" ) }.join(' ')
 source = '<ARRAY>'
	elsif source.length() == 1 && source[0] == "-STDIN"
 _get_nextline = method(:_get_nextline)
 _args = $stdin.readline
 source = '<STDIN>'
	elsif source.length() == 1 && source[0] == '-CONFIG'
 progname = "#{$0}rc"
 progname.sub!(%r#.*/#,'')
 home = ENV['HOME'] || ''
 _args, source = _load_sources( _get_nextline,
			[ home+"/.#{progname}",
			  ".#{progname}" ] )
	else
 # Bunch of files to load passed to parse()
 _args, source = _load_sources( _get_nextline, source )
	end
    when String  # else/case LITERAL STRING TO PARSE
	_args = source.dup
	source = source[0,7] + '...' if source && source.length() > 7
	source = "\"#{source[0..9]}\""
    else
      raise "Unknown source type for Getopt::Declare::parse"
    end  # case
    return 0 unless _args
    source = " (in #{source})"
  else
    _args  = ARGV.map { |i| i.tr( " \t\n", "\0\0\0" ) }.join(' ')
    source = ''
  end

  @source = source
  begin
    err = eval( code(@caller) )
    if $@
	# oops, something wrong... exit
	puts "#{$!}: #{$@.inspect}"
	exit(1)
    end
    if !err
	exit(1)
    end
  rescue
    raise
  end


  true
end

#sizeObject

Operator to return number of flags set



1726
1727
1728
1729
# File 'lib/Getopt/Declare.rb', line 1726

def size
  return 0 unless @cache
  return @cache.keys.size
end

#type(*t) ⇒ Object



1282
1283
1284
# File 'lib/Getopt/Declare.rb', line 1282

def type(*t)
  Getopt::Declare::ScalarArg::addtype(t)
end

#usage(*opt) ⇒ Object

Print out usage information



1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
# File 'lib/Getopt/Declare.rb', line 1311

def usage(*opt)

  t = @usage

  lastflag = nil
  lastdesc = nil
  usage = ''

  while !t.empty?

    # COMMENT:
    t.sub!(/\A[ \t]*#.*\n/,".") and next

    # TYPE DIRECTIVE:
    se  = DelimScanner::new( t )

    if t =~ /\A\s*\[\s*pvtype:/
	if action = se.extractBracketed("[")
 t.sub!(Regexp::quote( action ).to_re,'')
 t.sub!(/\A[ \t]*\n/,"")  
 next
	end
    end

    # ACTION
    codeblockDelimiters = {
	'{'     => '}'
    }
    se  = DelimScanner::new( t )
    if action = se.extractCodeblock(codeblockDelimiters)
	t.sub!(Regexp::quote( action ).to_re,'')
	t.sub!(/\A[ \t]*\n/,"")
	decfirst = 0 unless !decfirst.nil?
	next
    end


    # ARG + DESC:
    if t.sub!(re_argument,"")
	decfirst = 0 unless !decfirst.nil?
	spec = "#$1".expand_tabs!()
	desc = "#$2".expand_tabs!()

	while t.gsub!(re_more_desc, '')
 desc += "#$1".expand_tabs!
	end

	next if desc =~ /\[\s*undocumented\s*\]/i

	uoff = 0
	spec.gsub!(/(<[a-zA-Z]\w*):([^>]+)>/e) { |i|
 uoff += 1 + "#$2".length() and "#$1>"
      }
	spec.gsub!(/\t/,"=")

	ditto = desc =~ /\A\s*\[ditto\]/
	desc.gsub!(/^\s*\[.*?\]\s*\n/m,"")
	desc.gsub!(BracketDirectives,'')
	#desc.gsub!(/\[.*?\]/,"")

	
	if ditto
 desc = (lastdesc ? _ditto(lastflag,lastdesc,desc) : "" )
	elsif desc =~ /\A\s*\Z/
 next
	else
 lastdesc = desc
	end

	spec =~ /\A\s*(\S+)/ and lastflag = "#$1"
      
      desc.sub!(/\s+\Z/, "\n")
	usage += spec + ' ' * uoff + desc
	next
    end

    

    # OTHERWISE, DECORATION
    if t.sub!(/((?:(?!\[\s*pvtype:).)*)(\n|(?=\[\s*pvtype:))/,"")
	desc = "#$1"+("#$2"||'')
	#desc.gsub!(/^(\s*\[.*?\])+\s*\n/m,'')
	#desc.gsub!(/\[.*?\]/,'')  # eliminates anything in brackets
	if @tight || desc !~ /\A\s*\Z/
 desc.gsub!(BracketDirectives,'')
 next if desc =~ /\A\s*\Z/
	end
	decfirst = 1 unless !decfirst.nil? or desc =~ /\A\s*\Z/
	usage += desc
    end

  end  #while

  required = ''
  
  for arg in @args
    required += ' ' + arg.desc + ' '  if arg.required
  end
    
  usage.gsub!(/\255/,"[/") # REINSTATE ESCAPED '['s
    
  required.gsub!(/<([a-zA-Z]\w*):[^>]+>/,'<\1>')
  required.rstrip!
  
  helpcmd = Getopt::Declare::Arg::besthelp
  versioncmd = Getopt::Declare::Arg::bestversion
  
    
  header = ''
  unless @source.nil?
    header << version()
    prog = "#{$0}"
    prog.sub!(%r#.*/#,'')
    header <<  "Usage: #{prog} [options]#{required}\n"
    header <<  "       #{prog} #{helpcmd}\n" if helpcmd
    header <<  "       #{prog} #{versioncmd}\n" if versioncmd
    header <<  "\n" unless decfirst && decfirst == 1 && usage =~ /\A[ \t]*\n/
  end
  
  header << "Options:\n" unless decfirst && decfirst == 1
  
  usage.sub!(/[\s]+\Z/m, '')

  pager = $stdout

  #begin
  #  eval('require "IO/Pager";')
  #  pager = IO::Pager.new()
  #rescue
  #end

  if opt.empty?
    pager.puts "#{header}#{usage}"
    return 0
    ### usage
  end

  #usage.sub!(/\A[\s\n]+/m, '')
  pager.puts "#{header}#{usage}"
  exit(opt[0]) if opt[0]
end

#usedObject

Return list of used parameters (after parsing)



1457
1458
1459
1460
# File 'lib/Getopt/Declare.rb', line 1457

def used
  used = @cache.keys
  return used.join(' ')
end

#version(*t) ⇒ Object

Print out version information and maybe exit



1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
# File 'lib/Getopt/Declare.rb', line 1287

def version(*t)
  prog = "#{$0}"
  begin
    filedate = File.stat( prog ).mtime.localtime()
  rescue
    filedate = 'Unknown date'
  end
  prog.sub!(%r#.*/#,'')
  r = ''
  if defined?($VERSION)
    r << "\n#{prog}: version #{$VERSION}  (#{filedate})\n\n"
  else
    r << "\n#{prog}: version dated #{filedate}\n\n"
  end

  if t.empty?
    return r
  else
    puts r
    exit t[0]
  end 
end