getopt-declare 1.09.7 → 1.12

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.txt ADDED
@@ -0,0 +1,10 @@
1
+
2
+ 1.12 - Fixed bug with :+i, :+n, :0+i options not being cast appropiately
3
+ when used in pvttypes.
4
+ Added [debug] option to argument description to turn on debugging.
5
+
6
+ NOTE: there is a known bug when using [] within pvttype regex
7
+ definitions. Problem seems to be related to DelimScanner/strscan.
8
+
9
+ 1.11 - Fixed a pretty bug that was parsing -flags inappropiately and leading
10
+ to slower parsing.
@@ -0,0 +1,22 @@
1
+ require "rubygems"
2
+
3
+ spec = Gem::Specification.new do |spec|
4
+ spec.name = "getopt-declare"
5
+ spec.version = '1.12'
6
+ spec.author = "Gonzalo Garramuno"
7
+ spec.email = 'GGarramuno@aol.com'
8
+ spec.homepage = 'http://www.rubyforge.org/projects/getoptdeclare/'
9
+ spec.summary = 'Getopt-Declare is a command-line argument parser.'
10
+ spec.require_path = "lib"
11
+ spec.autorequire = "Getopt/Declare"
12
+ spec.files = ["lib/Getopt/Declare.rb", "lib/Getopt/DelimScanner.rb"]
13
+ spec.description = <<-EOF
14
+ Comprehensive and easy to use command-line parser library using
15
+ regular expressions. Port of Conway's Perl Getopt-Declare.
16
+ EOF
17
+ spec.extra_rdoc_files = ["Declare.rdoc", "HISTORY.txt", "getoptdeclare.gemspec"]
18
+ spec.has_rdoc = false
19
+ spec.test_files = Dir.glob('samples/*.rb')
20
+ spec.rubyforge_project = 'getoptdeclare'
21
+ spec.required_ruby_version = '>= 1.6.8'
22
+ end
@@ -1,11 +1,11 @@
1
1
  #
2
2
  # Getopt::Declare - Declaratively Expressed Command-Line Arguments via Regular Expressions
3
3
  #
4
- # Ruby port of Perl's Getopt::Declare, version 1.09,
4
+ # Ruby port of Perl's Getopt::Declare, version 1.12,
5
5
  # released May 21, 1999.
6
6
  #
7
- # $Release Version: 1.10 $
8
- # $Date: 2004/01/25 07:02:48 $
7
+ # $Release Version: 1.12 $
8
+ # $Date: 2004/01/15 10:53:09 $
9
9
  # by Gonzalo Garramu�o
10
10
  #
11
11
  # For detailed instructions, see Declare.rdoc file
@@ -61,7 +61,7 @@ end
61
61
 
62
62
  # Regex for removing bracket directives
63
63
  BracketDirectives =
64
- /\[(?:ditto|tight|repeatable|required|mutex:.*|implies:.*|excludes:.*|requires:.*)\]/
64
+ /\[(?:ditto|tight|strict|repeatable|debug|required|mutex:.*|implies:.*|excludes:.*|requires:.*)\]/
65
65
 
66
66
 
67
67
  module Getopt
@@ -69,10 +69,10 @@ module Getopt
69
69
  # Main Class
70
70
  class Declare
71
71
 
72
- VERSION = '1.09.7'
72
+ VERSION = '1.12'
73
73
 
74
- # For debugging, set to 1 and it will output the ruby code as .CODE.rb
75
- @@Declare_debug = 0
74
+ # For debugging, use [debug] and it will output the ruby code as .CODE.rb
75
+ @@debug = nil
76
76
 
77
77
  # Class used to handle the beginning of options
78
78
  class StartOpt
@@ -97,7 +97,7 @@ module Getopt
97
97
  nil
98
98
  end
99
99
 
100
- # Helps build regex that matches parameters of flags
100
+ # Helps build regex that matches parameters of flags
101
101
  def ows(g)
102
102
  g
103
103
  end
@@ -123,7 +123,7 @@ module Getopt
123
123
  @@stdtype = {
124
124
  ':i' => { 'pattern' => '(?:(?:%T[+-]?)%D+)' },
125
125
  ':n' => { 'pattern' => '(?:(?:%T[+-]?)(?:%D+(?:%T\.%D*)?' +
126
- '(?:%T[eE]%D+)?|%T\.%D+(?:%T[eE]%D+)?))'
126
+ '(?:%T[eE]%D+)?|%T\.%D+(?:%T[eE]%D+)?))',
127
127
  },
128
128
  ':s' => { 'pattern' => '(?:%T(?:\S|\0))+' },
129
129
  ':qs' => { 'pattern' => %q#"(?:\\"|[^"])*"|'(?:\\'|[^"])*|(?:%T(?:\S|\0))+#
@@ -154,7 +154,7 @@ module Getopt
154
154
  },
155
155
 
156
156
  ':+n' => { 'pattern' => ':n',
157
- 'action' => %q%reject( _VAL_ <= 0.0),
157
+ 'action' => %q%reject( _VAL_ <= 0.0,
158
158
  "in parameter '#{_PARAM_}' (#{_VAL_} must be a number greater than zero)")%,
159
159
  'ind' => 1
160
160
  },
@@ -178,7 +178,7 @@ module Getopt
178
178
 
179
179
  # Given a standard type name, return the corresponding regex
180
180
  # pattern or nil
181
- def stdtype(name)
181
+ def ScalarArg.stdtype(name)
182
182
  seen = {}
183
183
  while (!seen[name] && @@stdtype[name] && @@stdtype[name]['ind'])
184
184
  seen[name] = 1; name = @@stdtype[name]['pattern']
@@ -188,6 +188,10 @@ module Getopt
188
188
  @@stdtype[name]['pattern']
189
189
  end
190
190
 
191
+ def stdtype(name)
192
+ ScalarArg.stdtype(name)
193
+ end
194
+
191
195
 
192
196
  # Given the name of a type, return its corresponding action(s)
193
197
  def ScalarArg.stdactions(name)
@@ -264,7 +268,6 @@ module Getopt
264
268
 
265
269
  # Return string with code to process parameter
266
270
  def code(*t)
267
-
268
271
  if t[0]
269
272
  pos1 = t[0].to_s()
270
273
  else
@@ -272,14 +275,12 @@ module Getopt
272
275
  end
273
276
 
274
277
  c = conversion
275
- if !c.empty?
276
- c = "\n _VAL_ = _VAL_#{c}\n"
277
- end
278
+ c = "\n _VAL_ = _VAL_#{c}" if not c.empty?
278
279
 
279
280
  code = <<-EOS
280
281
  _VAR_ = %q|<#{@name}>|
281
282
  _VAL_ = @@m[#{pos1}]
282
- _VAL_.tr!("\\0"," ") if _VAL_#{c}
283
+ _VAL_.tr!("\\0"," ")#{c}
283
284
  EOS
284
285
 
285
286
  actions = Getopt::Declare::ScalarArg::stdactions(@type)
@@ -299,14 +300,16 @@ EOS
299
300
 
300
301
  # Based on parameter type, default conversion to apply
301
302
  def conversion
302
- case @type
303
- when /^\:(0)?(\+)?i$/
304
- '.to_i'
305
- when /^\:(0)?(\+)?n$/
306
- '.to_f'
307
- else
308
- ''
309
- end
303
+ pat = @@stdtype[@type] ? @@stdtype[@type]['pattern'] : ''
304
+ [ @type, pat ].each { |t|
305
+ case t
306
+ when /^\:0?(\+)?i$/
307
+ return '.to_i'
308
+ when /^\:0?(\+)?n$/
309
+ return '.to_f'
310
+ end
311
+ }
312
+ return ''
310
313
  end
311
314
 
312
315
  # Return string with code to cache argument in Getopt::Declare's cache
@@ -362,7 +365,7 @@ EOS
362
365
 
363
366
  # Handle conversion to proper type
364
367
  c = conversion
365
- if !c.empty?
368
+ if not c.empty?
366
369
  code << " #{@name}.map! { |i| i#{c} }\n"
367
370
  end
368
371
 
@@ -622,16 +625,22 @@ EOS
622
625
  _PUNCT_ = {}
623
626
  '
624
627
 
625
- if flag
628
+ if flag != ''
629
+ # This boundary is to handle -- option, so that if user uses
630
+ # --foo and --foo is not a flag, it does not become
631
+ # -- and unused: 'foo', but an error saying flag '--foo' not defined.
632
+ boundary = ''
633
+ boundary = '(\s+|\Z)' if flag =~ /^(--|-|\+|\+\+)$/
634
+
626
635
  code << '
627
636
  _args && _pos = gindex( _args, /\G(?:\s|\0)*' +
628
- Regexp::quote(flag) + '/' + nocase + ", _pos) or throw(:paramout)
637
+ Regexp::quote(flag) + boundary + '/' + nocase + ", _pos) or throw(:paramout)
629
638
  unless @_errormsg
630
639
  @_errormsg = %q|incorrect specification of '" + flag + "' parameter|
631
640
  end
632
641
  "
633
- elsif ( ScalarArg::stdtype(@args[0]['type'])||'') !~ /\%F/
634
- code << %q%\n throw(:paramout) if @_errormsg\n%
642
+ elsif ( ScalarArg::stdtype(@args[0].type)||'') !~ /\%F/
643
+ code << "\n throw(:paramout) if @_errormsg\n"
635
644
  end
636
645
 
637
646
 
@@ -907,8 +916,8 @@ EOS
907
916
  action = action[1..-2]
908
917
 
909
918
  raise "Error: bad type directive (expected closing ']' but found " +
910
- "'#$1' instead): [pvtype: $name " + (pat ? "/$#{pat}/" : '') +
911
- " $#{action} #$1#$2....\n" if desc =~ /\A\s*([^\] \t\n])(\S*)/
919
+ "'#$1' instead): [pvtype: #{name} " + (pat ? "/#{pat}/" : '') +
920
+ " action:#{action} #$1#$2....\n" if desc =~ /\A\s*([^\] \t\n])(\S*)/
912
921
 
913
922
 
914
923
  Getopt::Declare::ScalarArg::addtype(name,pat,action,ind=~/:/)
@@ -993,6 +1002,7 @@ EOS
993
1002
  # COMMENT:
994
1003
  i.sub!(/\A[ \t]*#.*\n/,"") and next
995
1004
 
1005
+
996
1006
  # TYPE DIRECTIVE:
997
1007
  se = DelimScanner::new( i )
998
1008
 
@@ -1012,7 +1022,6 @@ EOS
1012
1022
  '{' => '}',
1013
1023
  }
1014
1024
 
1015
-
1016
1025
  _action = se.extractCodeblock(codeblockDelimiters)
1017
1026
  if _action
1018
1027
  i.sub!( Regexp::quote(_action ).to_re, "" )
@@ -1036,6 +1045,7 @@ EOS
1036
1045
  "\t(did you forget a closing '}'?)\n"
1037
1046
  end
1038
1047
 
1048
+
1039
1049
  # ARG + DESC:
1040
1050
  if i.sub!(/\A(.*?\S.*?)(\t.*\n)/,"")
1041
1051
  spec = "#$1"
@@ -1062,6 +1072,7 @@ EOS
1062
1072
  _infer(decorator, nil, _mutex)
1063
1073
 
1064
1074
  _all_repeatable = true if decorator =~ /\[repeatable\]/
1075
+ @@debug = true if decorator =~ /\[debug\]/
1065
1076
 
1066
1077
  end # while i.length
1067
1078
 
@@ -1107,7 +1118,7 @@ EOS
1107
1118
  @caller = caller()
1108
1119
 
1109
1120
  # VESTIGAL DEBUGGING CODE
1110
- if @@Declare_debug > 0
1121
+ if @@debug
1111
1122
  f = File.new(".CODE.rb","w") and
1112
1123
  f.puts( code() ) and
1113
1124
  f.close()
@@ -1193,7 +1204,7 @@ EOS
1193
1204
  err = eval( code(@caller) )
1194
1205
  if $@
1195
1206
  # oops, something wrong... exit
1196
- puts "#{$!} #{$@}"
1207
+ puts "#{$!}: #{$@.inspect}"
1197
1208
  exit(1)
1198
1209
  end
1199
1210
  if !err
@@ -1393,9 +1404,11 @@ begin
1393
1404
  end
1394
1405
  end
1395
1406
 
1396
- def finish(i)
1397
- if !i || i[0]
1398
- @_finished = 1
1407
+ def finish(*i)
1408
+ if i.size
1409
+ @_finished = i
1410
+ else
1411
+ @_finished = true
1399
1412
  end
1400
1413
  end
1401
1414
 
@@ -1526,7 +1539,7 @@ end
1526
1539
  code << %q%
1527
1540
  #################### Add unused arguments
1528
1541
  if _args && _nextpos > 0 && _args.length() > 0
1529
- @unused.push( _args[_nextpos..-1].split(' ' ) )
1542
+ @unused.replace( @unused + _args[_nextpos..-1].split(' ') )
1530
1543
  end
1531
1544
 
1532
1545
  for i in @unused
@@ -1564,7 +1577,8 @@ unless _errors > 0
1564
1577
  begin
1565
1578
  i.call()
1566
1579
  rescue => e
1567
- raise "Error: action in Getopt::Declare specification produced:\n" + e
1580
+ STDERR.puts "Action in Getopt::Declare specification produced:\n#{e}"
1581
+ _errors += 1
1568
1582
  end
1569
1583
  end
1570
1584
  end
@@ -22,7 +22,7 @@
22
22
  #
23
23
  # == Version
24
24
  #
25
- # $Id: DelimScanner.rb,v 1.1.1.1 2004/01/25 07:02:48 gga Exp $
25
+ # $Id: DelimScanner.rb,v 1.2 2003/01/12 20:56:51 deveiant Exp $
26
26
  #
27
27
  # == History
28
28
  #
@@ -73,8 +73,8 @@ class DelimScanner
73
73
 
74
74
 
75
75
  ### Class constants
76
- Version = /([\d\.]+)/.match( %q{$Revision: 1.1.1.1 $} )[1]
77
- Rcsid = %q$Id: DelimScanner.rb,v 1.1.1.1 2004/01/25 07:02:48 gga Exp $
76
+ Version = /([\d\.]+)/.match( %q{$Revision: 1.2 $} )[1]
77
+ Rcsid = %q$Id: DelimScanner.rb,v 1.2 2003/01/12 20:56:51 deveiant Exp $
78
78
 
79
79
  # Pattern to match a valid XML name
80
80
  XmlName = '[a-zA-Z_:][a-zA-Z0-9:.-]*'
@@ -1,25 +1,26 @@
1
- #! /usr/bin/ruby
2
-
3
- require "Getopt/Declare"
4
-
5
- args = Getopt::Declare.new( %q{
6
-
7
- -v <value> [etc] One or more values
8
- <infile> Input file [required]
9
- -o <outfiles>... Output files
10
- } )
11
-
12
- if args['-v']
13
- print "Using value: ", args['-v']['<value>']
14
- print " (etcetera)" if args['-v']['etc']
15
- print "\n"
16
- end
17
-
18
- infile = File.new( args['<infile>'] )
19
- data = infile
20
-
21
- for outfile in args['-o']
22
- #outfile = File.new(outfile,'w')
23
- print "processed ",outfile
24
- #outfile.close
25
- end
1
+ #!/usr/bin/env ruby
2
+
3
+ require "Getopt/Declare"
4
+
5
+ args = Getopt::Declare.new( %q{
6
+
7
+ -v <value> [etc] One or more values
8
+ <infile:if> Input file [required]
9
+ -o <outfiles>... Output files
10
+ } )
11
+
12
+ if args['-v']
13
+ print "Using value: ", args['-v']['<value>']
14
+ print " (etcetera)" if args['-v']['etc']
15
+ print "\n"
16
+ end
17
+
18
+
19
+ infile = File.new( args['<infile>'] )
20
+ data = infile
21
+
22
+ for outfile in args['-o']
23
+ #outfile = File.new(outfile,'w')
24
+ print "processed ",outfile
25
+ #outfile.close
26
+ end
@@ -1,31 +1,31 @@
1
- #!/usr/bin/ruby
2
-
3
- require "Getopt/Declare"
4
-
5
- args = Getopt::Declare.new(<<'EOPARAM')
6
-
7
- ============================================================
8
- Required parameter:
9
-
10
- -in <infile> Input file [required]
11
-
12
- ------------------------------------------------------------
13
-
14
- Optional parameters:
15
-
16
- (The first two are mutually exclusive) [mutex: -r -p]
17
-
18
- -r[and[om]] Output in random order
19
- -p[erm[ute]] Output all permutations
20
-
21
- ---------------------------------------------------
22
-
23
- -out <outfile> Optional output file
24
-
25
- ------------------------------------------------------------
26
- Note: this program is known to run very slowly of files with
27
- long individual lines.
28
- ============================================================
29
- EOPARAM
30
-
31
- print args.inspect
1
+ #!/bin/env ruby
2
+
3
+ require "Getopt/Declare"
4
+
5
+ args = Getopt::Declare.new(<<'EOPARAM')
6
+
7
+ ============================================================
8
+ Required parameter:
9
+
10
+ -in <infile> Input file [required]
11
+
12
+ ------------------------------------------------------------
13
+
14
+ Optional parameters:
15
+
16
+ (The first two are mutually exclusive) [mutex: -r -p]
17
+
18
+ -r[and[om]] Output in random order
19
+ -p[erm[ute]] Output all permutations
20
+
21
+ ---------------------------------------------------
22
+
23
+ -out <outfile> Optional output file
24
+
25
+ ------------------------------------------------------------
26
+ Note: this program is known to run very slowly of files with
27
+ long individual lines.
28
+ ============================================================
29
+ EOPARAM
30
+
31
+ print args.inspect
@@ -1,31 +1,31 @@
1
- #!/usr/bin/ruby
2
-
3
- require "Getopt/Declare"
4
-
5
- args = Getopt::Declare.new(<<'EOPARAM', ['-BUILD'])
6
-
7
- ============================================================
8
- Required parameter:
9
-
10
- -in <infile> Input file [required]
11
-
12
- ------------------------------------------------------------
13
-
14
- Optional parameters:
15
-
16
- (The first two are mutually exclusive) [mutex: -r -p]
17
-
18
- -r[and[om]] Output in random order
19
- -p[erm[ute]] Output all permutations
20
-
21
- ---------------------------------------------------
22
-
23
- -out <outfile> Optional output file
24
-
25
- ------------------------------------------------------------
26
- Note: this program is known to run very slowly of files with
27
- long individual lines.
28
- ============================================================
29
- EOPARAM
30
-
31
- puts args.code()
1
+ #!/bin/env ruby
2
+
3
+ require "Getopt/Declare"
4
+
5
+ args = Getopt::Declare.new(<<'EOPARAM', ['-BUILD'])
6
+
7
+ ============================================================
8
+ Required parameter:
9
+
10
+ -in <infile> Input file [required]
11
+
12
+ ------------------------------------------------------------
13
+
14
+ Optional parameters:
15
+
16
+ (The first two are mutually exclusive) [mutex: -r -p]
17
+
18
+ -r[and[om]] Output in random order
19
+ -p[erm[ute]] Output all permutations
20
+
21
+ ---------------------------------------------------
22
+
23
+ -out <outfile> Optional output file
24
+
25
+ ------------------------------------------------------------
26
+ Note: this program is known to run very slowly of files with
27
+ long individual lines.
28
+ ============================================================
29
+ EOPARAM
30
+
31
+ puts args.code()
@@ -1,23 +1,23 @@
1
- #! /usr/bin/ruby
2
-
3
- require "Getopt/Declare"
4
-
5
-
6
- # With the next specification, the <tt>-rev</tt> and/or <tt>-rand</tt> flags
7
- # can be specified _after_ the list of files, but still affect the processing
8
- # of those files. Moreover, if the command-line parsing fails for some reason
9
- # (perhaps due to an unrecognized argument), the deferred processing will
10
- # not be performed.
11
- args = Getopt::Declare.new( %q{
12
-
13
- <files>... Files to be processed
14
- { defer { files.each { |i|
15
- puts i, " ",$ordered } } }
16
-
17
- -rev[erse] Process in reverse order
18
- { $ordered = -1; }
19
-
20
- -rand[om] Process in random order
21
- { $ordered = 0; }
22
- } )
23
-
1
+ #!/bin/env ruby
2
+
3
+ require "Getopt/Declare"
4
+
5
+
6
+ # With the next specification, the <tt>-rev</tt> and/or <tt>-rand</tt> flags
7
+ # can be specified _after_ the list of files, but still affect the processing
8
+ # of those files. Moreover, if the command-line parsing fails for some reason
9
+ # (perhaps due to an unrecognized argument), the deferred processing will
10
+ # not be performed.
11
+ args = Getopt::Declare.new( %q{
12
+
13
+ <files>... Files to be processed
14
+ { defer { files.each { |i|
15
+ puts i, " ",$ordered } } }
16
+
17
+ -rev[erse] Process in reverse order
18
+ { $ordered = -1; }
19
+
20
+ -rand[om] Process in random order
21
+ { $ordered = 0; }
22
+ } )
23
+
@@ -1,38 +1,38 @@
1
- #!/usr/bin/ruby
2
-
3
- require "Getopt/Declare"
4
-
5
- args = Getopt::Declare.new(<<'EOPARAM', ['-BUILD'])
6
-
7
- ============================================================
8
- Required parameter:
9
-
10
- -in <infile> Input file [required]
11
-
12
- ------------------------------------------------------------
13
-
14
- Optional parameters:
15
-
16
- (The first two are mutually exclusive) [mutex: -r -p]
17
-
18
- -r[and[om]] Output in random order
19
- -p[erm[ute]] Output all permutations
20
-
21
- ---------------------------------------------------
22
-
23
- -out <outfile> Optional output file
24
-
25
- ------------------------------------------------------------
26
- Note: this program is known to run very slowly of files with
27
- long individual lines.
28
- ============================================================
29
- EOPARAM
30
-
31
-
32
- # TRY STANDARD CONFIG FILES
33
- args.parse(['-CONFIG'])
34
- #args.parse('/usr/local/config/.demo_rc')
35
-
36
- #args.parse() or raise "cannot parse";
37
-
38
- print args.inspect
1
+ #!/bin/env ruby
2
+
3
+ require "Getopt/Declare"
4
+
5
+ args = Getopt::Declare.new(<<'EOPARAM', ['-BUILD'])
6
+
7
+ ============================================================
8
+ Required parameter:
9
+
10
+ -in <infile> Input file [required]
11
+
12
+ ------------------------------------------------------------
13
+
14
+ Optional parameters:
15
+
16
+ (The first two are mutually exclusive) [mutex: -r -p]
17
+
18
+ -r[and[om]] Output in random order
19
+ -p[erm[ute]] Output all permutations
20
+
21
+ ---------------------------------------------------
22
+
23
+ -out <outfile> Optional output file
24
+
25
+ ------------------------------------------------------------
26
+ Note: this program is known to run very slowly of files with
27
+ long individual lines.
28
+ ============================================================
29
+ EOPARAM
30
+
31
+
32
+ # TRY STANDARD CONFIG FILES
33
+ args.parse(['-CONFIG'])
34
+ #args.parse('/usr/local/config/.demo_rc')
35
+
36
+ #args.parse() or raise "cannot parse";
37
+
38
+ print args.inspect
@@ -0,0 +1,14 @@
1
+ #!/bin/env ruby
2
+
3
+ require "Getopt/Declare"
4
+
5
+ argspec = %q{
6
+ [strict]
7
+ --verbose Print verbose info
8
+ -- Traditional argument list terminator
9
+ { finish }
10
+ }
11
+
12
+ args = Getopt::Declare.new(argspec)
13
+
14
+ p args