opt-simple 0.9.8 → 0.9.9
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/README +40 -4
- data/lib/opt_simple.rb +4 -2
- data/lib/tmp.rb +63 -0
- metadata +3 -2
data/README
CHANGED
|
@@ -39,7 +39,8 @@ It is recommended to install OptSimple using RubyGems:
|
|
|
39
39
|
=== One example that shows most of the behavior you might use
|
|
40
40
|
require 'opt_simple'
|
|
41
41
|
|
|
42
|
-
defaults = {
|
|
42
|
+
defaults = {
|
|
43
|
+
:glob_pattern => '*',
|
|
43
44
|
:num_results => 42,
|
|
44
45
|
:range => [5,10]
|
|
45
46
|
}
|
|
@@ -107,9 +108,9 @@ Which prints out an automatic usage statement:
|
|
|
107
108
|
|
|
108
109
|
-n, -num, --num-values VAL The answer to everything
|
|
109
110
|
|
|
110
|
-
-p, --pattern, --glob-pattern PATTERN glob pattern
|
|
111
|
+
-p, --pattern, --glob-pattern PATTERN glob pattern (default is '*')
|
|
111
112
|
|
|
112
|
-
--range ARG range: min,max (both >0) (default is [5, 10])
|
|
113
|
+
--range ARG range: min,max (both >0) (default is '[5, 10]')
|
|
113
114
|
|
|
114
115
|
-h, --help (for this help message)
|
|
115
116
|
|
|
@@ -252,8 +253,43 @@ Which prints out an automatic usage statement:
|
|
|
252
253
|
puts "Options"
|
|
253
254
|
puts options
|
|
254
255
|
|
|
255
|
-
|
|
256
|
+
=== A totally contrived example showing how to change multiple options within an option specification block
|
|
257
|
+
|
|
258
|
+
require 'opt_simple'
|
|
256
259
|
|
|
260
|
+
allowed_odds = [1,3,5,7]
|
|
261
|
+
allowed_evens = [2,4,6]
|
|
262
|
+
defaults = {
|
|
263
|
+
:even_val => allowed_evens.first,
|
|
264
|
+
:odd_val => allowed_odds.first
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
# Note that b/c we define option [--odd] and [--evens] _after_ the max-out flag,
|
|
268
|
+
# they will override the max-out setting.
|
|
269
|
+
opts = OptSimple.new(defaults).parse_opts! do | opt_obj |
|
|
270
|
+
flag %w[-m --max-out], "Maximize evens and odds." do
|
|
271
|
+
opt_obj.odd_val = allowed_odds.last
|
|
272
|
+
opt_obj.even_val = allowed_evens.last
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
option "--odd-val","Odd val in #{allowed_odds.inspect}","VAL" do | arg |
|
|
276
|
+
val = arg.to_i
|
|
277
|
+
error "Odd val must be in #{allowed_odds.inspect}" unless allowed_odds.include? val
|
|
278
|
+
set_opt val
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
option "--even-val","Even val #{allowed_evens.inspect}","VAL" do | arg |
|
|
282
|
+
val = arg.to_i
|
|
283
|
+
error "Even val must be in #{allowed_evens.inspect}" unless allowed_evens.include? val
|
|
284
|
+
set_opt val
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
puts "Options"
|
|
289
|
+
puts opts
|
|
290
|
+
|
|
291
|
+
== Questions and/or Comments
|
|
292
|
+
|
|
257
293
|
email {Ethan Stryker}[mailto:e.stryker@gmail.com]
|
|
258
294
|
|
|
259
295
|
|
data/lib/opt_simple.rb
CHANGED
|
@@ -274,8 +274,10 @@ class OptSimple
|
|
|
274
274
|
intersection = @defaults.keys & parm.names unless parm.class == Flag
|
|
275
275
|
if intersection.empty?
|
|
276
276
|
help_str << "\n\n"
|
|
277
|
+
elsif @defaults[intersection.first].nil?
|
|
278
|
+
help_str << " (default is 'nil')\n\n"
|
|
277
279
|
else
|
|
278
|
-
help_str << " (default is #{@defaults[intersection.first]})\n\n"
|
|
280
|
+
help_str << " (default is '#{@defaults[intersection.first]}')\n\n"
|
|
279
281
|
end
|
|
280
282
|
end
|
|
281
283
|
help_str << " SUMMARY:\n\n #{@summary}\n\n" unless @summary.empty?
|
|
@@ -424,7 +426,7 @@ class OptSimple
|
|
|
424
426
|
unless block_given?
|
|
425
427
|
@block = Proc.new {|arg| @param_options[names.first] = arg}
|
|
426
428
|
end
|
|
427
|
-
@first_call = true
|
|
429
|
+
@first_call = true # use this so we can accumulate non-defaults
|
|
428
430
|
end
|
|
429
431
|
|
|
430
432
|
def switch_len #:nodoc:
|
data/lib/tmp.rb
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# in parse_opts!
|
|
2
|
+
# now actually parse the args, and call all the stored up blocks from the options
|
|
3
|
+
@parameters.each do | parm |
|
|
4
|
+
intersection = @args & parm.switches
|
|
5
|
+
|
|
6
|
+
unless intersection.empty?
|
|
7
|
+
mandatory_check.delete(parm.switches)
|
|
8
|
+
|
|
9
|
+
arg_locations = []
|
|
10
|
+
@args.each_with_index {|arg,i| arg_locations << i if intersection.include?(arg) }
|
|
11
|
+
# ...
|
|
12
|
+
def flag(switches,help="",&block)
|
|
13
|
+
parm = Flag.new(switches,help,&block)
|
|
14
|
+
parm.set_opt false
|
|
15
|
+
add_parameter parm
|
|
16
|
+
|
|
17
|
+
# add it to the results to make checking flags nicer.
|
|
18
|
+
# use the first name b/c they are all aliased anyways.
|
|
19
|
+
@results.merge! parm.param_options
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def option(switches,help="",metavar="ARG",&block)
|
|
23
|
+
parm = Option.new(switches,help,metavar,&block)
|
|
24
|
+
|
|
25
|
+
# set the defaults, if there are any in the defaults hash
|
|
26
|
+
intersection = parm.names & @defaults.keys
|
|
27
|
+
if intersection.length == 1
|
|
28
|
+
parm.set_opt @defaults[intersection.first]
|
|
29
|
+
@results.merge! parm.param_options
|
|
30
|
+
elsif intersection.length > 1
|
|
31
|
+
raise OptSimple::Error "Clashes in the defaults for #{parm.switches}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
add_parameter(parm)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class Option < Parameter
|
|
38
|
+
attr_reader = :metavar
|
|
39
|
+
def initialize(switches,help="",metavar="ARG",&block)
|
|
40
|
+
super(switches,help,&block)
|
|
41
|
+
@metavar = metavar
|
|
42
|
+
unless block_given?
|
|
43
|
+
@block = Proc.new {|arg| @param_options[names.first] = arg}
|
|
44
|
+
end
|
|
45
|
+
@first_call = true # use this so we can accumulate non-defaults
|
|
46
|
+
end
|
|
47
|
+
def switch_len #:nodoc:
|
|
48
|
+
metavar_space = @metavar.empty? ? 0 : @metavar.length + 1
|
|
49
|
+
super + metavar_space
|
|
50
|
+
end
|
|
51
|
+
def switch_str #:nodoc:
|
|
52
|
+
super + " #{@metavar}"
|
|
53
|
+
end
|
|
54
|
+
# append val to the parameter list
|
|
55
|
+
def accumulate_opt(val)
|
|
56
|
+
if @first_call
|
|
57
|
+
@param_options[names.first] = [val].flatten
|
|
58
|
+
else
|
|
59
|
+
@param_options[names.first] << val
|
|
60
|
+
end
|
|
61
|
+
@first_call = false
|
|
62
|
+
end
|
|
63
|
+
end
|
metadata
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
name: opt-simple
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease:
|
|
5
|
-
version: 0.9.
|
|
5
|
+
version: 0.9.9
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
8
8
|
- Ethan Stryker
|
|
@@ -10,7 +10,7 @@ autorequire:
|
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
12
|
|
|
13
|
-
date: 2011-
|
|
13
|
+
date: 2011-07-09 00:00:00 +01:00
|
|
14
14
|
default_executable:
|
|
15
15
|
dependencies: []
|
|
16
16
|
|
|
@@ -25,6 +25,7 @@ extra_rdoc_files: []
|
|
|
25
25
|
files:
|
|
26
26
|
- lib/test_unit_extensions.rb
|
|
27
27
|
- lib/opt_simple.rb
|
|
28
|
+
- lib/tmp.rb
|
|
28
29
|
- test/test_help.rb
|
|
29
30
|
- test/test_usage.rb
|
|
30
31
|
- test/test_arglist.rb
|