opt-simple 0.7.2 → 0.7.3
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 +36 -1
- data/lib/opt_simple.rb +48 -21
- data/test/test_usage.rb +35 -0
- metadata +4 -4
data/README
CHANGED
@@ -15,7 +15,9 @@ There are three methods to define command line parameters:
|
|
15
15
|
argument - a mandatory command line parameter with one or more arguments
|
16
16
|
|
17
17
|
Inside the blocks in flag, option, and argument a shortcut function called 'set_opt'
|
18
|
-
can be used to set an option that will be returned in the options hash.
|
18
|
+
can be used to set an option that will be returned in the options hash. The 'accumulate_opt'
|
19
|
+
method can be used in the option and argument blocks to create a list of values, and in
|
20
|
+
the flag block to increment a counter (with verbosity being the classic example).
|
19
21
|
|
20
22
|
The number of arguments are determined by the 'arity' of the block.
|
21
23
|
|
@@ -103,6 +105,39 @@ It is recommended to install OptSimple using RubyGems:
|
|
103
105
|
puts "ARGV"
|
104
106
|
puts ARGV
|
105
107
|
|
108
|
+
=== An example that shows how to use 'accumulate_opt' on an option to create a list, and on a flag to increment a counter
|
109
|
+
|
110
|
+
require 'opt_simple'
|
111
|
+
|
112
|
+
verbosity = 0
|
113
|
+
|
114
|
+
options,arguments = OptSimple.new.parse_opts! do
|
115
|
+
option %w[-i --infile], "Infile, multiple allowed", "INFILE" do | arg |
|
116
|
+
accumulate_opt arg
|
117
|
+
end
|
118
|
+
|
119
|
+
flag %w[-v --verbose],"Verbosity. the more you set, the more we give" do
|
120
|
+
verbosity += 1
|
121
|
+
end
|
122
|
+
|
123
|
+
flag %w[-m --more-cow-bell], "I've got a fever" do
|
124
|
+
accumulate_opt
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
puts "Options"
|
129
|
+
puts options.inspect
|
130
|
+
|
131
|
+
puts "Arguments"
|
132
|
+
puts arguments.inspect
|
133
|
+
|
134
|
+
puts "Verbosity"
|
135
|
+
puts verbosity
|
136
|
+
|
137
|
+
puts "ARGV"
|
138
|
+
puts ARGV
|
139
|
+
|
140
|
+
|
106
141
|
=== An example that shows that you can easily set your defaults in normal Ruby variables and provide your own help.
|
107
142
|
|
108
143
|
require 'opt_simple'
|
data/lib/opt_simple.rb
CHANGED
@@ -96,25 +96,36 @@ class OptSimple
|
|
96
96
|
|
97
97
|
# now actually parse the args, and call all the stored up blocks from the options
|
98
98
|
@parameters.each do | parm |
|
99
|
-
|
99
|
+
mandatory_check.delete(parm.switches)
|
100
|
+
intersection = @args & parm.switches
|
100
101
|
unless intersection.empty?
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
102
|
+
|
103
|
+
arg_locations = []
|
104
|
+
@args.each_with_index {|arg,i| arg_locations << i if intersection.include?(arg) }
|
105
|
+
|
106
|
+
# we want to process the args in order to provide predictable behavior
|
107
|
+
# in the case of switch duplication.
|
108
|
+
# We do pull them out in reverse so that the slicing removes pieces from the end
|
109
|
+
# of @args, so we don't disrupt the other locations, but put them into 'chunks'
|
110
|
+
# backwards to restore their order.
|
111
|
+
chunks = []
|
112
|
+
arg_locations.sort.reverse.each do |loc|
|
113
|
+
chunks.unshift @args.slice!(loc .. loc + parm.block.arity)[1..-1]
|
108
114
|
end
|
109
|
-
|
110
|
-
mandatory_check.delete(parm.switches)
|
111
115
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
+
chunks.each do | pieces |
|
117
|
+
if pieces.length < parm.block.arity or
|
118
|
+
pieces.any? {|p| p.start_with?('-')}
|
119
|
+
raise OptSimple::MissingArgument.new "Not enough args following #{intersection}",self
|
120
|
+
end
|
121
|
+
|
122
|
+
begin
|
123
|
+
parm.instance_exec(*pieces,&parm.block)
|
124
|
+
rescue OptSimple::Error => e
|
125
|
+
raise OptSimple::Error.new e.message,self
|
126
|
+
end
|
116
127
|
end
|
117
|
-
|
128
|
+
|
118
129
|
@options.merge!(parm.param_options)
|
119
130
|
end
|
120
131
|
end
|
@@ -286,11 +297,11 @@ class OptSimple
|
|
286
297
|
@names ||= switches.map {|s| s.sub(/^-+/,'')}
|
287
298
|
end
|
288
299
|
|
289
|
-
def switch_len
|
300
|
+
def switch_len #:nodoc:
|
290
301
|
@switches.join(', ').length
|
291
302
|
end
|
292
303
|
|
293
|
-
def switch_str
|
304
|
+
def switch_str #:nodoc:
|
294
305
|
short_parms = @switches.find_all {|st| st.start_with?('-') and st.length == 2 and st[1] != '-'}
|
295
306
|
long_parms = @switches.find_all {|st| st.start_with?('--') or (st.start_with?('-') and st.length > 2)}
|
296
307
|
other_parms = @switches.find_all {|st| not st.start_with?('-')}
|
@@ -335,10 +346,18 @@ class OptSimple
|
|
335
346
|
class Flag < Parameter
|
336
347
|
def initialize(switches,help="",&block)
|
337
348
|
super(switches,help,&block)
|
338
|
-
|
349
|
+
if block_given?
|
350
|
+
names.each {|n| @param_options[n] = 0 }
|
351
|
+
else
|
339
352
|
@block = Proc.new { names.each {|n| @param_options[n] = true}}
|
340
353
|
end
|
341
354
|
end
|
355
|
+
|
356
|
+
# increment the parameter by one every time it is seen on the CL
|
357
|
+
def accumulate_opt
|
358
|
+
names.each {|n| @param_options[n] += 1}
|
359
|
+
end
|
360
|
+
|
342
361
|
end
|
343
362
|
|
344
363
|
# An optional parameter, with one or more argument following it.
|
@@ -354,19 +373,27 @@ class OptSimple
|
|
354
373
|
def initialize(switches,help="",metavar="ARG",&block)
|
355
374
|
super(switches,help,&block)
|
356
375
|
@metavar = metavar
|
357
|
-
|
376
|
+
if block_given?
|
377
|
+
names.each {|n| @param_options[n] = []}
|
378
|
+
else
|
358
379
|
@block = Proc.new {|arg| names.each {|n| @param_options[n] = arg}}
|
359
380
|
end
|
360
381
|
end
|
361
382
|
|
362
|
-
def switch_len
|
383
|
+
def switch_len #:nodoc:
|
363
384
|
metavar_space = @metavar.empty? ? 0 : @metavar.length + 1
|
364
385
|
super + metavar_space
|
365
386
|
end
|
366
387
|
|
367
|
-
def switch_str
|
388
|
+
def switch_str #:nodoc:
|
368
389
|
super + " #{@metavar}"
|
369
390
|
end
|
391
|
+
|
392
|
+
# append val to the parameter list
|
393
|
+
def accumulate_opt(val)
|
394
|
+
names.each {|n| @param_options[n] << val}
|
395
|
+
end
|
396
|
+
|
370
397
|
end
|
371
398
|
|
372
399
|
# A mandatory parameter, with one or more argument following it.
|
data/test/test_usage.rb
CHANGED
@@ -42,5 +42,40 @@ class TestHelpStatement < Test::Unit::TestCase
|
|
42
42
|
assert_equal x,4
|
43
43
|
assert_equal y,5
|
44
44
|
end
|
45
|
+
|
46
|
+
must "accumulate lists of args when asked" do
|
47
|
+
os = OptSimple.new({},%w[-i foo.bar --infile bar.in])
|
48
|
+
o,a = os.parse_opts! do
|
49
|
+
option %w[-i --infile], "Infile, multiple allowed", "INFILE" do | arg |
|
50
|
+
accumulate_opt arg
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
assert_equal o['i'],%w[foo.bar bar.in]
|
55
|
+
assert_equal o['infile'],%w[foo.bar bar.in]
|
56
|
+
end
|
57
|
+
|
58
|
+
must "accumulate numbers of flags set when asked" do
|
59
|
+
os = OptSimple.new({},%w[-v -v --verbose -v])
|
60
|
+
o,a = os.parse_opts! do
|
61
|
+
flag %w[-v --verbose],"Verbosity. the more you set, the more we give" do
|
62
|
+
accumulate_opt
|
63
|
+
end
|
64
|
+
end
|
65
|
+
assert_equal o['v'],4
|
66
|
+
assert_equal o['verbose'],4
|
67
|
+
end
|
68
|
+
|
69
|
+
must "set last arg when duplicated when accumulate opt isn't used" do
|
70
|
+
os = OptSimple.new({},%w[-i foo.bar --infile bar.in -i baz])
|
71
|
+
o,a = os.parse_opts! do
|
72
|
+
option %w[-i --infile], "Infile, multiple allowed", "INFILE" do | arg |
|
73
|
+
set_opt arg
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
assert_equal o['i'],'baz'
|
78
|
+
assert_equal o['infile'],'baz'
|
79
|
+
end
|
45
80
|
end
|
46
81
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opt-simple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 5
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 0.7.
|
9
|
+
- 3
|
10
|
+
version: 0.7.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ethan Stryker
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-12 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|