getopt-declare 1.22 → 1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/HISTORY.txt CHANGED
@@ -1,3 +1,21 @@
1
+ 1.23 - * Added all samples as Ruby tests, which lead to the discovery
2
+ of several bugs.
3
+ * Fixed silly bug with foundid that would make some parsing not
4
+ work (required parameters).
5
+ * Fixed a silly bug when parsing from a string file, which would
6
+ modify the string in place
7
+ * Fixed a bug in that would make it not parse Rh+ and Rh- in
8
+ cmdline_pvttype demo and other potential regexes.
9
+ * Removed 3 warnings from parsing multiple times.
10
+ * Fixed a bug with cluster: singles not working.
11
+ * Fixed a stack trace bug with optional single text arguments.
12
+ * Improved handling of optional integer/float arguments, leaving
13
+ them as nil instead of 0 as before.
14
+ * Added 'used' function to allow returning the flags that are used.
15
+ * Made CSV demo work again, albeit I am not too happy with the regex.
16
+ * Made Getopt::Declare part of firebrigade.
17
+ http://firebrigade.seattlerb.org/
18
+
1
19
  1.22 - * Fixed some nasty bugs introduced in 1.21, which would cause stack
2
20
  traces.
3
21
  * Fixed the use of /bin/env in some demos instead of /usr/bin/env.
data/Rakefile CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'rake/gempackagetask'
4
4
  require 'rake/rdoctask'
5
+ require 'rake/testtask'
5
6
 
6
7
  rdoc_files = ["Declare.rdoc", 'HISTORY.txt'] + Dir.glob('lib/*/*.rb')
7
8
 
@@ -10,7 +11,7 @@ rdoc_files = ["Declare.rdoc", 'HISTORY.txt'] + Dir.glob('lib/*/*.rb')
10
11
  #
11
12
  spec = Gem::Specification.new do |spec|
12
13
  spec.name = "getopt-declare"
13
- spec.version = '1.22'
14
+ spec.version = '1.23'
14
15
  spec.author = "Gonzalo Garramuno"
15
16
  spec.email = 'ggarra@advancedsl.com.ar, GGarramuno@aol.com'
16
17
  spec.homepage = 'http://www.rubyforge.org/projects/getoptdeclare/'
@@ -62,7 +63,40 @@ task 'upload-docs' => ['rdoc'] do
62
63
  end
63
64
 
64
65
 
66
+ desc 'Zip directory as an archive.'
67
+ task 'zip' => ['rdoc'] do
68
+ dir = File.basename(Dir.pwd)
69
+ zipfile = spec.name + "-" + spec.version.to_s + ".zip"
70
+ Dir.chdir('..')
71
+ File.unlink(zipfile) if File.exists?(zipfile)
72
+ sh "zip -9rv #{zipfile} #{dir} -x '*~' -x '#*#' -x '*docs/*' -x '*pkg/*' -x '.*'"
73
+ Dir.chdir(dir)
74
+ end
75
+
76
+ desc 'Run unit tests'
77
+ Rake::TestTask.new do |t|
78
+ t.libs << 'test'
79
+ t.test_files = FileList['test/*.rb']
80
+ t.verbose = true
81
+ t.warning = true
82
+ # t.loader = :testrb
83
+ # p t.methods - Object.methods
84
+ end
85
+
86
+ desc 'upload gem'
87
+ task 'upload-gem' => ['test', 'gem'] do
88
+ end
89
+
90
+ desc 'upload zip'
91
+ task 'upload-zip' => ['zip'] do
92
+ end
93
+
94
+ desc 'Upload gem, docs and zip file'
95
+ task 'upload' => [ 'upload-gem', 'upload-zip', 'upload-docs' ]
96
+
97
+
98
+
65
99
  #
66
100
  # Main task
67
101
  #
68
- task :default => ['gem', 'upload-docs']
102
+ task :default => ['upload']
@@ -131,7 +131,7 @@ module Getopt
131
131
  '(?:%T[eE](?:[+-])?%D+)?|%T\.%D+(?:%T[eE](?:[+-])?%D+)?))',
132
132
  },
133
133
  ':s' => { 'pattern' => '(?:%T(?:\S|\0))+(?=\s|\0|\z)' },
134
- ':qs' => { 'pattern' => %q{"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|(?:%T(?:\S|\0))+(?=\s|\0|\z)} },
134
+ ':qs' => { 'pattern' => %q{"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|(?:%T(?:\S|\0))+} },
135
135
  ':id' => { 'pattern' => '%T[a-zA-Z_](?:%T\w)*(?=\s|\0|\z)' },
136
136
  ':d' => { 'pattern' => '(?:%T(?:\S|\0))+',
137
137
  'action' => %q%
@@ -279,12 +279,12 @@ module Getopt
279
279
  end
280
280
 
281
281
  c = conversion
282
- c = "\n _VAL_ = _VAL_#{c}" if c
282
+ c = "\n _VAL_ = _VAL_#{c} if _VAL_" if c
283
283
 
284
284
  code = <<-EOS
285
285
  _VAR_ = %q|<#{@name}>|
286
286
  _VAL_ = @@m[#{pos1}]
287
- _VAL_.tr!("\\0"," ")#{c}
287
+ _VAL_.tr!("\\0"," ") if _VAL_#{c}
288
288
  EOS
289
289
 
290
290
  actions = Getopt::Declare::ScalarArg::stdactions(@type)
@@ -489,6 +489,13 @@ EOS
489
489
  @@posflagpat = nil
490
490
  @@negflagpat = nil
491
491
 
492
+ def Arg.clear
493
+ @@flags = []
494
+ @@nextid = 0
495
+ @@posflagpat = nil
496
+ @@negflagpath = nil
497
+ end
498
+
492
499
  # Return string with regex that avoids all flags in declaration
493
500
  def Arg.negflagpat(*t)
494
501
  if !@@negflagpat && @@flags
@@ -601,7 +608,6 @@ EOS
601
608
 
602
609
  # Return String with code to parse this argument (ie. flag)
603
610
  def code(*t)
604
-
605
611
  owner = t[0]
606
612
  mod = t[1]
607
613
 
@@ -616,7 +622,7 @@ EOS
616
622
  code += !@repeatable? " while !_FOUND_['" + self.foundid + "']" :
617
623
  " while 1"
618
624
  if (flag && (clump==1 && flag !~ /\A[^a-z0-9]+[a-z0-9]\Z/i ||
619
- (clump<3 && @args )))
625
+ (clump<3 && @args.size > 0 )))
620
626
  code << " && !_lastprefix"
621
627
  end
622
628
 
@@ -657,16 +663,13 @@ EOS
657
663
  i -= 1
658
664
  end # while i
659
665
 
660
-
661
666
  if @args
662
- code << "\n"+' _args && _pos = gindex( _args, /\G'
667
+ code << "\n"+' _args && _pos = gindex( _args, /\G'
663
668
 
664
669
  @args.each_with_index { |arg, i|
665
670
  code << arg.ows(arg.matcher(trailer[i]))
666
671
  }
667
672
 
668
- code << '\b'
669
-
670
673
  code << '/x' + nocasei + ", _pos ) or throw(:paramout)\n"
671
674
  end # if @args
672
675
 
@@ -741,12 +744,16 @@ EOS
741
744
 
742
745
  # Return name of argument, which can be flag's name or variable's name
743
746
  def name
744
- return ( @flag.empty? ? "<#{@args[0].name}>" : @flag )
747
+ return @flag unless @flag.empty?
748
+ for i in @args
749
+ return "<#{i.name}>" if i.respond_to?(:name)
750
+ end
751
+ raise "Unknown flag name for parameter #{self.desc}"
745
752
  end
746
753
 
747
754
  # Return foundid of argument, which can be flag's name or variable's name
748
755
  def foundid
749
- return @foundid || '<#{@args[0].name}>'
756
+ return @foundid || "<#{self.name}>"
750
757
  end
751
758
 
752
759
  end # Arg
@@ -989,6 +996,9 @@ EOS
989
996
 
990
997
  # Constructor
991
998
  def initialize(*opts)
999
+ @cache = nil
1000
+
1001
+ Getopt::Declare::Arg::clear
992
1002
 
993
1003
  # HANDLE SHORT-CIRCUITS
994
1004
  return if opts.size==2 && (!opts[1] || opts[1] == '-SKIP')
@@ -1017,6 +1027,7 @@ EOS
1017
1027
  _strict = false
1018
1028
  _all_repeatable = false
1019
1029
  _lastdesc = nil
1030
+ arg = nil
1020
1031
  Getopt::Declare::nocase = false
1021
1032
  Getopt::Declare::ScalarArg::_reset_stdtype
1022
1033
 
@@ -1134,7 +1145,13 @@ EOS
1134
1145
  end
1135
1146
 
1136
1147
  # Sort flags based on criteria described in docs
1137
- _args = _args.sort_by { |a| [a.flag.size, a.flag, a.args.size, a.id] }
1148
+ # Sadly, this cannot be reduced to sort_by
1149
+ _args = _args.sort() { |a,b|
1150
+ ( b.flag.length() <=> a.flag.length() ) or
1151
+ ( b.flag == a.flag and ( b.args.length() <=> a.args.length() ) ) or
1152
+ ( a.id <=> b.id )
1153
+ }
1154
+ # _args = _args.sort_by { |a| [a.flag.size, a.flag, a.args.size, a.id] }
1138
1155
 
1139
1156
 
1140
1157
  # Handle clump
@@ -1165,8 +1182,8 @@ EOS
1165
1182
  end
1166
1183
 
1167
1184
  # DO THE PARSE (IF APPROPRIATE)
1168
- if (opts.length() == 2)
1169
- return nil unless parse(opts[1])
1185
+ if opts.size == 2
1186
+ return nil unless parse(source)
1170
1187
  else
1171
1188
  return nil unless parse()
1172
1189
  end
@@ -1178,7 +1195,7 @@ EOS
1178
1195
  # optionally eval it, too.
1179
1196
  def parse(*opts)
1180
1197
  source = opts[0]
1181
- _args = ()
1198
+ _args = nil
1182
1199
  _get_nextline = proc { nil }
1183
1200
 
1184
1201
  if source
@@ -1200,6 +1217,8 @@ EOS
1200
1217
  _args = source.readlines.join(' ')
1201
1218
  _args.tr!('\t\n',' ')
1202
1219
  end
1220
+ when :build, :skip
1221
+ return 0
1203
1222
  when Array
1204
1223
  if source.length() == 1 && !source[0] ||
1205
1224
  source[0] == "-BUILD" ||
@@ -1226,10 +1245,12 @@ EOS
1226
1245
  # Bunch of files to load passed to parse()
1227
1246
  _args, source = _load_sources( _get_nextline, source )
1228
1247
  end
1229
- else # else/case LITERAL STRING TO PARSE
1248
+ when String # else/case LITERAL STRING TO PARSE
1230
1249
  _args = source.dup
1231
- source[7] = '...' if source && source.length() > 7
1250
+ source = source[0,7] + '...' if source && source.length() > 7
1232
1251
  source = "\"#{source[0..9]}\""
1252
+ else
1253
+ raise "Unknown source type for Getopt::Declare::parse"
1233
1254
  end # case
1234
1255
  return 0 unless _args
1235
1256
  source = " (in #{source})"
@@ -1239,7 +1260,6 @@ EOS
1239
1260
  end
1240
1261
 
1241
1262
  @source = source
1242
-
1243
1263
  begin
1244
1264
  err = eval( code(@caller) )
1245
1265
  if $@
@@ -1267,12 +1287,19 @@ EOS
1267
1287
  prog = "#{$0}"
1268
1288
  filedate = File.stat( prog ).mtime.localtime()
1269
1289
  prog.sub!(%r#.*/#,'')
1290
+ r = ''
1270
1291
  if defined?($VERSION)
1271
- puts "\n#{prog}: version #{$VERSION} (#{filedate})\n\n"
1292
+ r << "\n#{prog}: version #{$VERSION} (#{filedate})\n\n"
1272
1293
  else
1273
- puts "\n#{prog}: version dated #{filedate}\n\n"
1294
+ r << "\n#{prog}: version dated #{filedate}\n\n"
1274
1295
  end
1275
- exit t[0] if !t[0].nil?
1296
+
1297
+ if t.empty?
1298
+ return r
1299
+ else
1300
+ puts r
1301
+ exit t[0]
1302
+ end
1276
1303
  end
1277
1304
 
1278
1305
  # Print out usage information
@@ -1346,7 +1373,8 @@ EOS
1346
1373
  end
1347
1374
 
1348
1375
  spec =~ /\A\s*(\S+)/ and lastflag = "#$1"
1349
-
1376
+
1377
+ desc.sub!(/\s+\Z/, "\n")
1350
1378
  usage += spec + ' ' * uoff + desc
1351
1379
  next
1352
1380
  end
@@ -1364,6 +1392,7 @@ EOS
1364
1392
  end
1365
1393
  decfirst = 1 unless !decfirst.nil? or desc =~ /\A\s*\Z/
1366
1394
  usage += desc
1395
+ usage.rstrip!
1367
1396
  end
1368
1397
 
1369
1398
  end #while
@@ -1377,31 +1406,37 @@ EOS
1377
1406
  usage.gsub!(/\255/,"[/") # REINSTATE ESCAPED '['s
1378
1407
 
1379
1408
  required.gsub!(/<([a-zA-Z]\w*):[^>]+>/,'<\1>')
1409
+ required.rstrip!
1380
1410
 
1381
1411
  helpcmd = Getopt::Declare::Arg::besthelp
1382
1412
  versioncmd = Getopt::Declare::Arg::bestversion
1383
1413
 
1384
- pager = $stdout
1385
1414
 
1386
- #begin
1387
- # eval('require "IO/Pager";')
1388
- # pager = IO::Pager.new()
1389
- #rescue
1390
- #end
1391
-
1415
+ header = ''
1392
1416
  unless @source.nil?
1393
- version()
1417
+ header << version()
1394
1418
  prog = "#{$0}"
1395
1419
  prog.sub!(%r#.*/#,'')
1396
- pager.print "Usage: #{prog} [options]#{required}\n"
1397
- pager.print " #{prog} #{helpcmd}\n" if helpcmd
1398
- pager.print " #{prog} #{versioncmd}\n" if versioncmd
1399
- pager.print "\n" unless decfirst && decfirst == 1 && usage =~ /\A[ \t]*\n/
1420
+ header << "Usage: #{prog} [options]#{required}\n"
1421
+ header << " #{prog} #{helpcmd}\n" if helpcmd
1422
+ header << " #{prog} #{versioncmd}\n" if versioncmd
1423
+ header << "\n" unless decfirst && decfirst == 1 && usage =~ /\A[ \t]*\n/
1400
1424
  end
1401
1425
 
1402
- pager.print "Options:\n" unless decfirst && decfirst == 1
1403
- pager.print usage
1426
+ header << "Options:\n" unless decfirst && decfirst == 1
1404
1427
 
1428
+ if opt.empty?
1429
+ return header + usage + "\n"
1430
+ end
1431
+
1432
+ pager = $stdout
1433
+ #begin
1434
+ # eval('require "IO/Pager";')
1435
+ # pager = IO::Pager.new()
1436
+ #rescue
1437
+ #end
1438
+
1439
+ pager.puts "#{header}#{usage}"
1405
1440
  exit(opt[0]) if opt[0]
1406
1441
  end
1407
1442
 
@@ -1410,8 +1445,8 @@ EOS
1410
1445
 
1411
1446
  # Return list of used parameters (after parsing)
1412
1447
  def used
1413
- used = @cache.each_key { |i| [ i, @cache[i] ] }
1414
- return (used.map { |i| flatten(i) } ).join(' ')
1448
+ used = @cache.keys
1449
+ return used.join(' ')
1415
1450
  end
1416
1451
 
1417
1452
  @@m = []
@@ -1428,6 +1463,13 @@ EOS
1428
1463
 
1429
1464
  begin
1430
1465
 
1466
+ begin
1467
+ undef :defer
1468
+ undef :reject
1469
+ undef :finish
1470
+ rescue
1471
+ end
1472
+
1431
1473
  def defer(&i)
1432
1474
  @_deferred.push( i )
1433
1475
  end
@@ -1610,7 +1652,7 @@ end
1610
1652
  unless _errors > 0
1611
1653
  for i in @_deferred
1612
1654
  begin
1613
- i.call()
1655
+ i.call
1614
1656
  rescue => e
1615
1657
  STDERR.puts "Action in Getopt::Declare specification produced:\n#{e}"
1616
1658
  _errors += 1
@@ -1626,7 +1668,7 @@ end
1626
1668
 
1627
1669
 
1628
1670
  # Inspect cache (not the declare object)
1629
- def inspect()
1671
+ def inspect
1630
1672
  return nil if !@cache
1631
1673
  t = ''
1632
1674
 
@@ -29,7 +29,7 @@ args = Getopt::Declare.new(<<EOF)
29
29
 
30
30
  =getrand [<n>] Get a random number
31
31
  (or, optionally, <n> of them)
32
- { n = 1 unless !n.empty? }
32
+ { n = 1 unless n }
33
33
 
34
34
  -- Traditionally indicates end of arguments
35
35
  { finish }
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.1
3
3
  specification_version: 1
4
4
  name: getopt-declare
5
5
  version: !ruby/object:Gem::Version
6
- version: "1.22"
7
- date: 2007-01-21 00:00:00 -03:00
6
+ version: "1.23"
7
+ date: 2007-03-21 00:00:00 -03:00
8
8
  summary: Getopt-Declare is a command-line argument parser.
9
9
  require_paths:
10
10
  - lib