getopt-declare 1.27 → 1.28

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.txt CHANGED
@@ -1,3 +1,14 @@
1
+ == 1.28
2
+ * Fixed a minor issue where help lines were not being compacted
3
+ properly.
4
+ * Fixed a minor inconsistency in pvtype regex in usage description.
5
+ * Fixed GNU style parameter matching to not get confused with
6
+ multiple spaces before parameters.
7
+ * Sped up matching of default rules a tad due to use of symbols
8
+ instead of strings.
9
+ * Fixed required matches not working for some cases due to change
10
+ in 1.27.
11
+
1
12
  == 1.27
2
13
  * Fixed a bug with the sorting of arguments, which could sometimes
3
14
  lead to strange parsing.
data/Manifest.txt CHANGED
@@ -38,3 +38,4 @@ test/test_cmdline_regex.rb
38
38
  test/test_demo_csv.rb
39
39
  test/test_cluster_singles.rb
40
40
  test/test_cmdline_array.rb
41
+ test/test_cmdline_require.rb
@@ -4,7 +4,7 @@
4
4
  # Ruby port of Perl's Getopt::Declare, version 1.21,
5
5
  # released May 21, 1999.
6
6
  #
7
- # $Release Version: 1.27 $
7
+ # $Release Version: 1.28 $
8
8
  # $Date: 2007/01/15 10:53:09 $
9
9
  # by Gonzalo Garramu�o
10
10
  #
@@ -70,14 +70,14 @@ module Getopt
70
70
  # Main Class
71
71
  class Declare
72
72
 
73
- VERSION = '1.27'
73
+ VERSION = '1.28'
74
74
 
75
75
  # For debugging, use [debug] and it will output the ruby code as .CODE.rb
76
76
  @@debug = false
77
77
 
78
78
  # Main separator used to distinguish arguments in Getopt/Declare spec.
79
79
  # By default, one or more tabs or 3 spaces or more.
80
- @@separator = '(?:\t+| {3})'
80
+ @@separator = '(?:\t+| {3}[^<])'
81
81
 
82
82
  # Class used to handle the beginning of options
83
83
  class StartOpt
@@ -126,54 +126,60 @@ module Getopt
126
126
  # (re)set standard types
127
127
  def ScalarArg._reset_stdtype
128
128
  @@stdtype = {
129
- ':i' => { 'pattern' => '(?:(?:%T[+-]?)%D+)' },
130
- ':n' => { 'pattern' => '(?:(?:%T[+-]?)(?:%D+(?:%T\.%D*)?' +
129
+ ':i' => { :pattern => '(?:(?:%T[+-]?)%D+)' },
130
+ ':n' => { :pattern => '(?:(?:%T[+-]?)(?:%D+(?:%T\.%D*)?' +
131
131
  '(?:%T[eE](?:[+-])?%D+)?|%T\.%D+(?:%T[eE](?:[+-])?%D+)?))',
132
132
  },
133
- ':s' => { 'pattern' => '(?:%T(?:\S|\0))+(?=\s|\0|\z)' },
134
- ':qs' => { 'pattern' => %q{"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|(?:%T(?:\S|\0))+} },
135
- ':id' => { 'pattern' => '%T[a-zA-Z_](?:%T\w)*(?=\s|\0|\z)' },
136
- ':d' => { 'pattern' => '(?:%T(?:\S|\0))+',
137
- 'action' => %q%
133
+ ':s' => { :pattern => '(?:%T(?:\S|\0))+(?=\s|\0|\z)' },
134
+ ':qs' => { :pattern => %q{"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|(?:%T(?:\S|\0))+} },
135
+ ':id' => { :pattern => '%T[a-zA-Z_](?:%T\w)*(?=\s|\0|\z)' },
136
+ ':d' => {
137
+ :pattern => '(?:%T(?:\S|\0))+',
138
+ :action => %q%
138
139
  reject( (_VAL_.nil? || !test(?d, _VAL_) ),
139
140
  "in parameter '#{_PARAM_}' (\"#{_VAL_}\" is not a directory)")%
140
141
  },
141
- ':if' => { 'pattern' => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
142
- 'action' => %q%
142
+ ':if' => {
143
+ :pattern => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
144
+ :action => %q%
143
145
  reject( (_VAL_.nil? || _VAL_ != "-" && !test(?r, _VAL_) ),
144
146
  "in parameter '#{_PARAM_}' (file \"#{_VAL_}\" is not readable)")%
145
147
  },
146
- ':of' => { 'pattern' => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
147
- 'action' => %q%
148
+ ':of' => {
149
+ :pattern => '%F(?:%T(?:\S|\0))+(?=\s|\0|\z)',
150
+ :action => %q%
148
151
  reject( (_VAL_.nil? || _VAL_ != "-" &&
149
152
  test(?r, _VAL_) && !test(?w, _VAL_)),
150
153
  "in parameter '#{_PARAM_}' (file \"#{_VAL_}\" is not writable)")%
151
154
  },
152
- '' => { 'pattern' => ':s', 'ind' => 1 },
155
+ '' => { :pattern => ':s', :ind => 1 },
153
156
 
154
- ':+i' => { 'pattern' => ':i',
155
- 'action' => %q%reject( _VAL_ <= 0,
157
+ ':+i' => {
158
+ :pattern => ':i',
159
+ :action => %q%reject( _VAL_ <= 0,
156
160
  "in parameter '#{_PARAM_}' (#{_VAL_} must be an integer greater than zero)")%,
157
- 'ind' => 1
161
+ :ind => 1
158
162
  },
159
163
 
160
- ':+n' => { 'pattern' => ':n',
161
- 'action' => %q%reject( _VAL_ <= 0.0,
164
+ ':+n' => {
165
+ :pattern => ':n',
166
+ :action => %q%reject( _VAL_ <= 0.0,
162
167
  "in parameter '#{_PARAM_}' (#{_VAL_} must be a number greater than zero)")%,
163
- 'ind' => 1
168
+ :ind => 1
164
169
  },
165
170
 
166
- ':0+i' => { 'pattern' => ':i',
167
- 'action' => %q%reject( _VAL_ < 0,
171
+ ':0+i' => {
172
+ :pattern => ':i',
173
+ :action => %q%reject( _VAL_ < 0,
168
174
  "in parameter '#{_PARAM_}' (#{_VAL_} must be an positive integer)")%,
169
- 'ind' => 1
175
+ :ind => 1
170
176
  },
171
177
 
172
- ':0+n' => {
173
- 'pattern' => ':n',
174
- 'action' => %q%reject( _VAL_ < 0,
178
+ ':0+n' => {
179
+ :pattern => ':n',
180
+ :action => %q%reject( _VAL_ < 0,
175
181
  "in parameter '#{_PARAM_}' (#{_VAL_} must be an positive number)")%,
176
- 'ind' => 1
182
+ :ind => 1
177
183
  },
178
184
  }
179
185
 
@@ -184,12 +190,12 @@ module Getopt
184
190
  # pattern or nil
185
191
  def ScalarArg.stdtype(name)
186
192
  seen = {}
187
- while (!seen[name] && @@stdtype[name] && @@stdtype[name]['ind'])
188
- seen[name] = 1; name = @@stdtype[name]['pattern']
193
+ while (!seen[name] && @@stdtype[name] && @@stdtype[name][:ind])
194
+ seen[name] = 1; name = @@stdtype[name][:pattern]
189
195
  end
190
196
 
191
197
  return nil if seen[name] || !@@stdtype[name]
192
- @@stdtype[name]['pattern']
198
+ @@stdtype[name][:pattern]
193
199
  end
194
200
 
195
201
  def stdtype(name)
@@ -201,16 +207,16 @@ module Getopt
201
207
  def ScalarArg.stdactions(name)
202
208
  seen = {}
203
209
  actions = []
204
- while (!seen[name] && @@stdtype[name] && @@stdtype[name]['ind'])
210
+ while (!seen[name] && @@stdtype[name] && @@stdtype[name][:ind])
205
211
  seen[name] = 1
206
- if @@stdtype[name]['action']
207
- actions.push( @@stdtype[name]['action'] )
212
+ if @@stdtype[name][:action]
213
+ actions.push( @@stdtype[name][:action] )
208
214
  end
209
- name = @@stdtype[name]['pattern']
215
+ name = @@stdtype[name][:pattern]
210
216
  end
211
217
 
212
- if @@stdtype[name] && @@stdtype[name]['action']
213
- actions.push( @@stdtype[name]['action'] )
218
+ if @@stdtype[name] && @@stdtype[name][:action]
219
+ actions.push( @@stdtype[name][:action] )
214
220
  end
215
221
 
216
222
  return actions
@@ -226,10 +232,10 @@ module Getopt
226
232
  end
227
233
 
228
234
  @@stdtype[typeid] = {}
229
- @@stdtype[typeid]['pattern'] = "(?:#{pattern})" if pattern && !ref
230
- @@stdtype[typeid]['pattern'] = ":#{pattern}" if pattern && ref
231
- @@stdtype[typeid]['action'] = action if action
232
- @@stdtype[typeid]['ind'] = ref
235
+ @@stdtype[typeid][:pattern] = "(?:#{pattern})" if pattern && !ref
236
+ @@stdtype[typeid][:pattern] = ":#{pattern}" if pattern && ref
237
+ @@stdtype[typeid][:action] = action if action
238
+ @@stdtype[typeid][:ind] = ref
233
239
 
234
240
  end
235
241
 
@@ -304,7 +310,7 @@ EOS
304
310
 
305
311
  # Based on parameter type, default conversion to apply
306
312
  def conversion
307
- pat = @@stdtype[@type] ? @@stdtype[@type]['pattern'] : ''
313
+ pat = @@stdtype[@type] ? @@stdtype[@type][:pattern] : ''
308
314
  [ @type, pat ].each { |t|
309
315
  case t
310
316
  when /^\:0?(\+)?i$/
@@ -518,13 +524,13 @@ EOS
518
524
 
519
525
  attr_accessor :flag, :args, :actions, :ditto, :nocase
520
526
  attr_accessor :required, :id, :repeatable, :desc
521
- attr_writer :requires
527
+ attr_accessor :requires
522
528
 
523
529
 
524
530
  #
525
- def _enfound(original)
526
- expr = original.gsub(/((?:&&|\|\|)?\s*(?:[!(]\s*)*)([^ \t\n|&\)]+)/x,
527
- '\1_FOUND_[\'\2\']')
531
+ def found_requires
532
+ expr = @requires.gsub(/((?:&&|\|\|)?\s*(?:[!(]\s*)*)([^ \t\n|&\)]+)/x,
533
+ '\1_FOUND_[\'\2\']')
528
534
 
529
535
  if !valid_syntax?( expr )
530
536
  raise "Error: bad condition in [requires: #{original}]\n"
@@ -532,10 +538,6 @@ EOS
532
538
  expr
533
539
  end
534
540
 
535
- def requires
536
- return nil unless @requires
537
- _enfound(@requires)
538
- end
539
541
 
540
542
  # Constructor
541
543
  def initialize(spec, desc, dittoflag)
@@ -635,9 +637,6 @@ EOS
635
637
  nocasei = ((Getopt::Declare::nocase || @nocase) ? 'i' : '')
636
638
 
637
639
  code << " catch(:paramout) do\n while "
638
- if @requires
639
- code << requires << ' and '
640
- end
641
640
  code += !@repeatable? "!_FOUND_['" + self.foundid + "']" : "true"
642
641
 
643
642
  if (flag && (clump==1 && flag !~ /\A[^a-z0-9]+[a-z0-9]\Z/i ||
@@ -645,12 +644,12 @@ EOS
645
644
  code << ' and !_lastprefix'
646
645
  end
647
646
 
648
- code << '
647
+ code <<'
649
648
  begin
650
649
  catch(:param) do
651
650
  _pos = _nextpos if _args
652
651
  _PUNCT_ = {}
653
- '
652
+ '
654
653
 
655
654
  if flag != ''
656
655
  # This boundary is to handle -- option, so that if user uses
@@ -883,7 +882,7 @@ EOS
883
882
  _exclude(mutex, arg.name, ("#$1".split(' ')))
884
883
  end
885
884
 
886
- if desc =~ /.*\[\s*requires:\s*(.*?)\]/i
885
+ if desc =~ /.*\[\s*requires:\s*(.*?)\s*\]/i
887
886
  arg.requires = "#$1"
888
887
  end
889
888
 
@@ -1102,7 +1101,7 @@ EOS
1102
1101
 
1103
1102
  # Check for GNU spec line like: -d, --debug
1104
1103
  arg = nil
1105
- if spec =~ /(-[\w_\d]+),\s+(--[\w_\d]+)(\s+.*)?/
1104
+ if spec =~ /(-[\w_\d]+),\s+(--?[\w_\d]+)(\s+.*)?/
1106
1105
  specs = ["#$1#$3", "#$2#$3"]
1107
1106
  specs.each { |spec|
1108
1107
  arg = Arg.new(spec,desc,ditto)
@@ -1318,7 +1317,7 @@ EOS
1318
1317
  lastdesc = nil
1319
1318
  usage = ''
1320
1319
 
1321
- while t.length() > 0
1320
+ while !t.empty?
1322
1321
 
1323
1322
  # COMMENT:
1324
1323
  t.sub!(/\A[ \t]*#.*\n/,".") and next
@@ -1326,7 +1325,7 @@ EOS
1326
1325
  # TYPE DIRECTIVE:
1327
1326
  se = DelimScanner::new( t )
1328
1327
 
1329
- if t =~ /\A\s*\[pvtype:/
1328
+ if t =~ /\A\s*\[\s*pvtype:/
1330
1329
  if action = se.extractBracketed("[")
1331
1330
  t.sub!(Regexp::quote( action ).to_re,'')
1332
1331
  t.sub!(/\A[ \t]*\n/,"")
@@ -1389,7 +1388,7 @@ EOS
1389
1388
 
1390
1389
 
1391
1390
  # OTHERWISE, DECORATION
1392
- if t.sub!(/((?:(?!\[pvtype:).)*)(\n|(?=\[pvtype:))/,"")
1391
+ if t.sub!(/((?:(?!\[\s*pvtype:).)*)(\n|(?=\[\s*pvtype:))/,"")
1393
1392
  desc = "#$1"+("#$2"||'')
1394
1393
  #desc.gsub!(/^(\s*\[.*?\])+\s*\n/m,'')
1395
1394
  #desc.gsub!(/\[.*?\]/,'') # eliminates anything in brackets
@@ -1431,7 +1430,7 @@ EOS
1431
1430
 
1432
1431
  header << "Options:\n" unless decfirst && decfirst == 1
1433
1432
 
1434
- usage.sub!(/[\s\n]+$/, '')
1433
+ usage.sub!(/[\s\n]+\Z/m, '')
1435
1434
 
1436
1435
  if opt.empty?
1437
1436
  return header + usage + "\n"
@@ -1613,7 +1612,7 @@ end
1613
1612
  next unless arg.requires
1614
1613
 
1615
1614
  code << %q%
1616
- if _FOUND_['% + arg.name + %q%'] && !(% + arg.requires +
1615
+ if _FOUND_['% + arg.name + %q%'] && !(% + arg.found_requires +
1617
1616
  %q%)
1618
1617
  $stderr.puts "Error#{@source}: parameter '% + arg.name + %q%' can only be specified with '% + arg.requires + %q%'"
1619
1618
  _errors += 1
@@ -1735,9 +1734,3 @@ end
1735
1734
  end # class Declare
1736
1735
 
1737
1736
  end # module Getopt
1738
-
1739
-
1740
-
1741
-
1742
- __END__
1743
-
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Put script description here.
4
+ #
5
+ # Author::
6
+ # Copyright::
7
+ # License:: Ruby
8
+ #
9
+
10
+ require 'Getopt/Declare'
11
+
12
+
13
+ require 'test/unit'
14
+ require "Getopt/Declare"
15
+
16
+ # to avoid getopt from exiting...
17
+ def exit(args)
18
+ end
19
+
20
+
21
+ class TC_Require < Test::Unit::TestCase
22
+
23
+ def setup
24
+ @args = Getopt::Declare.new(<<EOF, :build)
25
+ -i <iterations:+i> iteration count
26
+ -q add new task [ requires: -i ]
27
+ EOF
28
+ end
29
+
30
+ def test_q_without_i
31
+ begin
32
+ @args.parse('-q')
33
+ assert_equal( nil, "Parsing of -q did not fail" )
34
+ rescue
35
+ end
36
+ end
37
+
38
+ def test_i_without_q
39
+ begin
40
+ @args.parse('-i 5')
41
+ rescue
42
+ end
43
+ assert_equal( 5, @args['-i'] )
44
+ end
45
+
46
+ def test_i_with_q
47
+ begin
48
+ @args.parse('-i 5 -q')
49
+ rescue
50
+ end
51
+ assert_equal( 5, @args['-i'] )
52
+ assert_equal( '-q', @args['-q'] )
53
+ end
54
+
55
+ end
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.27"
7
- date: 2007-06-23 00:00:00 -03:00
6
+ version: "1.28"
7
+ date: 2007-07-09 00:00:00 -03:00
8
8
  summary: Getopt-Declare is a command-line argument parser.
9
9
  require_paths:
10
10
  - lib
@@ -69,6 +69,7 @@ files:
69
69
  - test/test_demo_csv.rb
70
70
  - test/test_cluster_singles.rb
71
71
  - test/test_cmdline_array.rb
72
+ - test/test_cmdline_require.rb
72
73
  test_files:
73
74
  - test/test_cmdline_cmdline.rb
74
75
  - test/test_cmdline_basic.rb
@@ -79,6 +80,7 @@ test_files:
79
80
  - test/test_cmdline_pvtype.rb
80
81
  - test/test_cmdline_regex.rb
81
82
  - test/test_demo_csv.rb
83
+ - test/test_cmdline_require.rb
82
84
  - test/test_cluster_singles.rb
83
85
  - test/test_cmdline_array.rb
84
86
  rdoc_options: