opt-simple 0.7.3 → 0.7.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/opt_simple.rb +11 -5
- data/test/test_usage.rb +15 -6
- metadata +5 -17
data/lib/opt_simple.rb
CHANGED
@@ -12,7 +12,9 @@ There are three methods to define command line parameters:
|
|
12
12
|
argument - a mandatory command line parameter with one or more arguments
|
13
13
|
|
14
14
|
Inside the blocks in flag, option, and argument a shortcut function called 'set_opt'
|
15
|
-
can be used to set an option that will be returned in the options hash.
|
15
|
+
can be used to set an option that will be returned in the options hash. The 'accumulate_opt'
|
16
|
+
method can be used in the option and argument blocks to create a list of values, and in
|
17
|
+
the flag block to increment a counter (with verbosity being the classic example).
|
16
18
|
|
17
19
|
The number of arguments are determined by the 'arity' of the block.
|
18
20
|
|
@@ -79,7 +81,7 @@ class OptSimple
|
|
79
81
|
end
|
80
82
|
|
81
83
|
mandatory_check = mandatory_opts.map {|m| m.switches}
|
82
|
-
|
84
|
+
|
83
85
|
if(loc = @args.index('--'))
|
84
86
|
#remove the '--', but don't include it w/ positional arguments
|
85
87
|
@positional_arguments += @args.slice!(loc..-1)[1..-1]
|
@@ -96,9 +98,9 @@ class OptSimple
|
|
96
98
|
|
97
99
|
# now actually parse the args, and call all the stored up blocks from the options
|
98
100
|
@parameters.each do | parm |
|
99
|
-
mandatory_check.delete(parm.switches)
|
100
101
|
intersection = @args & parm.switches
|
101
102
|
unless intersection.empty?
|
103
|
+
mandatory_check.delete(parm.switches)
|
102
104
|
|
103
105
|
arg_locations = []
|
104
106
|
@args.each_with_index {|arg,i| arg_locations << i if intersection.include?(arg) }
|
@@ -116,7 +118,7 @@ class OptSimple
|
|
116
118
|
chunks.each do | pieces |
|
117
119
|
if pieces.length < parm.block.arity or
|
118
120
|
pieces.any? {|p| p.start_with?('-')}
|
119
|
-
raise OptSimple::
|
121
|
+
raise OptSimple::ParameterUsageError.new "Not enough args following #{intersection}",self
|
120
122
|
end
|
121
123
|
|
122
124
|
begin
|
@@ -420,9 +422,13 @@ class OptSimple
|
|
420
422
|
# is used on the command line
|
421
423
|
class InvalidOption < Error;end
|
422
424
|
|
423
|
-
# An exception thrown
|
425
|
+
# An exception thrown if a mandatory parameter is not
|
424
426
|
# used on the command line
|
425
427
|
class MissingArgument < Error;end
|
426
428
|
|
429
|
+
# An exception thrown if a parameter isn't followed by enough arguments
|
430
|
+
# on the command line
|
431
|
+
class ParameterUsageError < Error;end
|
432
|
+
|
427
433
|
end
|
428
434
|
|
data/test/test_usage.rb
CHANGED
@@ -8,21 +8,30 @@ class TestHelpStatement < Test::Unit::TestCase
|
|
8
8
|
assert_raise(OptSimple::Error) { OptSimple.new.parse_opts! {option %w[-a --awesome]; flag '-awesome' } }
|
9
9
|
end
|
10
10
|
|
11
|
-
must "
|
12
|
-
os = OptSimple.new({}
|
13
|
-
assert_raise(OptSimple::
|
11
|
+
must "throw parameter missing exception when the argument method is used but the parm isn't specified" do
|
12
|
+
os = OptSimple.new({})
|
13
|
+
assert_raise(OptSimple::MissingArgument) do
|
14
|
+
os.parse_opts! do
|
15
|
+
argument '-a'
|
16
|
+
end
|
17
|
+
end
|
14
18
|
end
|
15
19
|
|
16
|
-
must "
|
20
|
+
must "throw missing parameter usage error when a parameter isn't followed by enough arguments" do
|
17
21
|
os = OptSimple.new({},['-a'])
|
18
|
-
assert_raise(OptSimple::
|
22
|
+
assert_raise(OptSimple::ParameterUsageError) do
|
19
23
|
os.parse_opts! do
|
20
|
-
|
24
|
+
argument '-a' do |arg|
|
21
25
|
nil
|
22
26
|
end
|
23
27
|
end
|
24
28
|
end
|
25
29
|
end
|
30
|
+
|
31
|
+
must "raise error when unknown option is given" do
|
32
|
+
os = OptSimple.new({},['-not-specified'])
|
33
|
+
assert_raise(OptSimple::InvalidOption) { os.parse_opts! { option %w[-a --awesome] } }
|
34
|
+
end
|
26
35
|
|
27
36
|
must "handle arguments with equals and commas" do
|
28
37
|
os = OptSimple.new({},['-a=2','--foo=4,5'])
|
metadata
CHANGED
@@ -1,13 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opt-simple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 7
|
9
|
-
- 3
|
10
|
-
version: 0.7.3
|
4
|
+
version: 0.7.4
|
11
5
|
platform: ruby
|
12
6
|
authors:
|
13
7
|
- Ethan Stryker
|
@@ -15,7 +9,7 @@ autorequire:
|
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
11
|
|
18
|
-
date: 2011-03-
|
12
|
+
date: 2011-03-13 00:00:00 +00:00
|
19
13
|
default_executable:
|
20
14
|
dependencies: []
|
21
15
|
|
@@ -46,27 +40,21 @@ rdoc_options: []
|
|
46
40
|
require_paths:
|
47
41
|
- lib
|
48
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
43
|
requirements:
|
51
44
|
- - ">="
|
52
45
|
- !ruby/object:Gem::Version
|
53
|
-
hash: 3
|
54
|
-
segments:
|
55
|
-
- 0
|
56
46
|
version: "0"
|
47
|
+
version:
|
57
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
49
|
requirements:
|
60
50
|
- - ">="
|
61
51
|
- !ruby/object:Gem::Version
|
62
|
-
hash: 3
|
63
|
-
segments:
|
64
|
-
- 0
|
65
52
|
version: "0"
|
53
|
+
version:
|
66
54
|
requirements: []
|
67
55
|
|
68
56
|
rubyforge_project: opt-simple
|
69
|
-
rubygems_version: 1.
|
57
|
+
rubygems_version: 1.3.5
|
70
58
|
signing_key:
|
71
59
|
specification_version: 3
|
72
60
|
summary: A simple and elegant command line option parser.
|