opt-simple 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -55,13 +55,13 @@ It is recommended to install OptSimple using RubyGems:
55
55
  puts ARGV
56
56
 
57
57
 
58
- === An example that provides error checking on ARGV, using all default behavior
58
+ === An example that provides error checking on ARGV, using all default behavior and how to specify a 'metavar'
59
59
 
60
60
  require 'opt_simple'
61
61
 
62
62
  options,arguments = OptSimple.new.parse_opts! do
63
- argument "-i","inFile"
64
- option %w[-p --pattern --glob-pattern], "glob pattern"
63
+ argument "-i","inFile","FILE"
64
+ option %w[-p --pattern --glob-pattern], "glob pattern","PATTERN"
65
65
  flag "-v","Verbose"
66
66
  flag "-whatever"
67
67
  end
data/lib/opt_simple.rb CHANGED
@@ -70,7 +70,7 @@ class OptSimple
70
70
  # actually get parsed.
71
71
 
72
72
  # add the help option at the end
73
- option %w[-h --help] ,"(for this help message)"
73
+ flag %w[-h --help] ,"(for this help message)"
74
74
 
75
75
  # first look for a call for help
76
76
  unless (%w[-h --help] & @args).empty?
@@ -85,6 +85,15 @@ class OptSimple
85
85
  @positional_arguments += @args.slice!(loc..-1)[1..-1]
86
86
  end
87
87
 
88
+ # Handle the case where a user specifies --foo=bar, or --foo=bar,baz
89
+ equal_args = @args.find_all {|arg| arg.include?('=') }
90
+ @args.delete_if {|arg| arg.include?('=') }
91
+ equal_args.each do | e |
92
+ switch,list = e.split('=')
93
+ @args << switch
94
+ list.split(',').each {|val| @args << val }
95
+ end
96
+
88
97
  # now actually parse the args, and call all the stored up blocks from the options
89
98
  @parameters.each do | parm |
90
99
  intersection = @args & parm.switches
@@ -174,24 +183,26 @@ class OptSimple
174
183
 
175
184
  # Registers an optional parameter, with one or more argument following it.
176
185
  # 'switches' can be a String or an Array of Strings, and specifies the switches expected on the CL.
177
- # 'help' provides a description of the parameter
186
+ # 'help' provides a description of the parameter, and 'metavar' will be used in the
187
+ # usage statement.
178
188
  # and an optional block can do parameter validation/transformation.
179
189
  # If no block is given, then the strings specified (after the
180
190
  # leading '-'s removed) will be used as keys in the options hash
181
191
  # and the values set to the arg following the switch in the args array.
182
- def option(switches,help="",&block)
183
- add_parameter(Option.new(switches,help,&block))
192
+ def option(switches,help="",metavar="ARG",&block)
193
+ add_parameter(Option.new(switches,help,metavar,&block))
184
194
  end
185
195
 
186
196
  # Registers a mandatory parameter, with one or more argument following it.
187
197
  # 'switches' can be a String or an Array of Strings, and specifies the switches expected on the CL.
188
- # 'help' provides a description of the parameter
198
+ # 'help' provides a description of the parameter, and 'metavar' will be used in the
199
+ # usage statement.
189
200
  # and an optional block can do parameter validation/transformation.
190
201
  # If no block is given, then the strings specified (after the
191
202
  # leading '-'s removed) will be used as keys in the options hash
192
203
  # and the values set to the arg following the switch the args array.
193
- def argument(switches,help="",&block)
194
- add_parameter(Argument.new(switches,help,&block))
204
+ def argument(switches,help="",metavar="ARG",&block)
205
+ add_parameter(Argument.new(switches,help,metavar,&block))
195
206
  end
196
207
 
197
208
  # A shortcut for raising an OptSimple::Error exception
@@ -232,8 +243,8 @@ class OptSimple
232
243
  # You probably don't want to call this method.
233
244
  # A lower level function that adds a Flag, Option, or Argument,
234
245
  def add_parameter(parm)
235
- total_switch_len = parm.switches.join(', ').length
236
- @longest_switch_len = total_switch_len if total_switch_len > @longest_switch_len
246
+
247
+ @longest_switch_len = parm.switch_len if parm.switch_len > @longest_switch_len
237
248
 
238
249
  parm.names.each do | n |
239
250
  if @param_names.has_key?(n)
@@ -252,7 +263,7 @@ class OptSimple
252
263
  # Option and Argument are derived. Provides documentation methods,
253
264
  # an 'error method', and the 'set_opt' utility funciton.
254
265
  class Parameter
255
- attr_reader :switches,:param_options,:block
266
+ attr_reader :switches,:param_options,:metavar,:block
256
267
 
257
268
  # 'switches' can be a String or an Array of Strings, and specifies the switches expected on the CL.
258
269
  # 'help' provides a description of the parameter
@@ -275,8 +286,11 @@ class OptSimple
275
286
  @names ||= switches.map {|s| s.sub(/^-+/,'')}
276
287
  end
277
288
 
278
- # a single line that will be put in the overall usage string
279
- def help_str(switch_len)
289
+ def switch_len
290
+ @switches.join(', ').length
291
+ end
292
+
293
+ def switch_str
280
294
  short_parms = @switches.find_all {|st| st.start_with?('-') and st.length == 2 and st[1] != '-'}
281
295
  long_parms = @switches.find_all {|st| st.start_with?('--') or (st.start_with?('-') and st.length > 2)}
282
296
  other_parms = @switches.find_all {|st| not st.start_with?('-')}
@@ -284,8 +298,13 @@ class OptSimple
284
298
  sh_str = short_parms.empty? ? " " * 4 : short_parms.first
285
299
  long_str = long_parms.join(', ') + other_parms.join(', ')
286
300
  sh_str << ', ' unless sh_str =~/^\s+$/ or long_str.empty?
287
-
288
- " %-#{switch_len}s" % (sh_str + long_str) + " \t#{@help}"
301
+
302
+ sh_str + long_str
303
+ end
304
+
305
+ # a single line that will be put in the overall usage string
306
+ def help_str(switch_len)
307
+ " %-#{switch_len}s" % self.switch_str + " \t#{@help}"
289
308
  end
290
309
 
291
310
  # A shortcut for raising an OptSimple::Error exception
@@ -324,23 +343,36 @@ class OptSimple
324
343
 
325
344
  # An optional parameter, with one or more argument following it.
326
345
  # 'switches' can be a String or an Array of Strings, and specifies the switches expected on the CL.
327
- # 'help' provides a description of the parameter
346
+ # 'help' provides a description of the parameter, and 'metavar' will be used in the
347
+ # usage statement.
328
348
  # and an optional block can do parameter validation/transformation.
329
349
  # If no block is given, then the strings specified (after the
330
350
  # leading '-'s removed) will be used as keys in the options hash
331
351
  # and the values set to the arg following the switch in the args array.
332
352
  class Option < Parameter
333
- def initialize(switches,help="",&block)
353
+ attr_reader = :metavar
354
+ def initialize(switches,help="",metavar="ARG",&block)
334
355
  super(switches,help,&block)
356
+ @metavar = metavar
335
357
  unless block_given?
336
358
  @block = Proc.new {|arg| names.each {|n| @param_options[n] = arg}}
337
359
  end
338
360
  end
361
+
362
+ def switch_len
363
+ metavar_space = @metavar.empty? ? 0 : @metavar.length + 1
364
+ super + metavar_space
365
+ end
366
+
367
+ def switch_str
368
+ super + " #{@metavar}"
369
+ end
339
370
  end
340
371
 
341
372
  # A mandatory parameter, with one or more argument following it.
342
373
  # 'switches' can be a String or an Array of Strings, and specifies the switches expected on the CL.
343
- # 'help' provides a description of the parameter
374
+ # 'help' provides a description of the parameter, and 'metavar' will be used in the
375
+ # usage statement.
344
376
  # and an optional block can do parameter validation/transformation.
345
377
  # If no block is given, then the strings specified (after the
346
378
  # leading '-'s removed) will be used as keys in the options hash
data/test/test_help.rb CHANGED
@@ -42,4 +42,10 @@ class TestHelpStatement < Test::Unit::TestCase
42
42
  os.summary "My summary"
43
43
  assert os.to_s.match(Regexp.new("My summary",Regexp::MULTILINE))
44
44
  end
45
+
46
+ must "add metavars to help statement" do
47
+ os = OptSimple.new({},['-h'])
48
+ os.add_parameter(OptSimple::Option.new '-a',"some help","THING")
49
+ assert os.to_s.match(Regexp.new("\-a.*THING.*some help",Regexp::MULTILINE))
50
+ end
45
51
  end
data/test/test_usage.rb CHANGED
@@ -24,5 +24,23 @@ class TestHelpStatement < Test::Unit::TestCase
24
24
  end
25
25
  end
26
26
 
27
+ must "handle arguments with equals and commas" do
28
+ os = OptSimple.new({},['-a=2','--foo=4,5'])
29
+ x = nil
30
+ y = nil
31
+ opts, args = os.parse_opts! do
32
+ option '-a' do |arg|
33
+ set_opt arg.to_i
34
+ end
35
+ option "--foo" do |a,b|
36
+ x = a.to_i
37
+ y = b.to_i
38
+ end
39
+ end
40
+
41
+ assert_equal opts['a'],2
42
+ assert_equal x,4
43
+ assert_equal y,5
44
+ end
27
45
  end
28
46
 
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opt-simple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ hash: 7
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 7
9
+ - 2
10
+ version: 0.7.2
5
11
  platform: ruby
6
12
  authors:
7
13
  - Ethan Stryker
@@ -9,7 +15,7 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2011-03-04 00:00:00 +00:00
18
+ date: 2011-03-06 00:00:00 +00:00
13
19
  default_executable:
14
20
  dependencies: []
15
21
 
@@ -40,21 +46,27 @@ rdoc_options: []
40
46
  require_paths:
41
47
  - lib
42
48
  required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
43
50
  requirements:
44
51
  - - ">="
45
52
  - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
46
56
  version: "0"
47
- version:
48
57
  required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
49
59
  requirements:
50
60
  - - ">="
51
61
  - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
52
65
  version: "0"
53
- version:
54
66
  requirements: []
55
67
 
56
68
  rubyforge_project: opt-simple
57
- rubygems_version: 1.3.5
69
+ rubygems_version: 1.6.1
58
70
  signing_key:
59
71
  specification_version: 3
60
72
  summary: A simple and elegant command line option parser.