cri 2.6.1 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +46 -21
- data/LICENSE +1 -1
- data/NEWS.md +12 -0
- data/README.adoc +1 -1
- data/Rakefile +7 -2
- data/cri.gemspec +5 -7
- data/lib/cri.rb +0 -2
- data/lib/cri/argument_array.rb +0 -4
- data/lib/cri/command.rb +31 -40
- data/lib/cri/command_dsl.rb +28 -18
- data/lib/cri/command_runner.rb +2 -6
- data/lib/cri/commands/basic_help.rb +2 -2
- data/lib/cri/commands/basic_root.rb +1 -1
- data/lib/cri/core_ext.rb +3 -1
- data/lib/cri/core_ext/string.rb +28 -30
- data/lib/cri/help_renderer.rb +105 -23
- data/lib/cri/option_parser.rb +13 -17
- data/lib/cri/platform.rb +1 -5
- data/lib/cri/string_formatter.rb +14 -9
- data/lib/cri/version.rb +1 -3
- data/test/helper.rb +38 -38
- data/test/test_argument_array.rb +7 -7
- data/test/test_base.rb +4 -4
- data/test/test_basic_help.rb +47 -47
- data/test/test_basic_root.rb +10 -10
- data/test/test_command.rb +424 -347
- data/test/test_command_dsl.rb +174 -150
- data/test/test_command_runner.rb +22 -23
- data/test/test_option_parser.rb +212 -224
- data/test/test_string_formatter.rb +97 -82
- metadata +3 -3
data/test/test_command_dsl.rb
CHANGED
@@ -1,187 +1,211 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
3
|
+
module Cri
|
4
|
+
class CommandDSLTestCase < Cri::TestCase
|
5
|
+
def test_create_command
|
6
|
+
# Define
|
7
|
+
dsl = Cri::CommandDSL.new
|
8
|
+
dsl.instance_eval do
|
9
|
+
name 'moo'
|
10
|
+
usage 'dunno whatever'
|
11
|
+
summary 'does stuff'
|
12
|
+
description 'This command does a lot of stuff.'
|
13
|
+
|
14
|
+
option :a, :aaa, 'opt a', :argument => :optional, :multiple => true
|
15
|
+
required :b, :bbb, 'opt b'
|
16
|
+
optional :c, :ccc, 'opt c'
|
17
|
+
flag :d, :ddd, 'opt d'
|
18
|
+
forbidden :e, :eee, 'opt e'
|
19
|
+
flag :f, :fff, 'opt f', :hidden => true
|
20
|
+
|
21
|
+
run do |_opts, _args|
|
22
|
+
$did_it_work = :probably
|
23
|
+
end
|
22
24
|
end
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
25
|
+
command = dsl.command
|
26
|
+
|
27
|
+
# Run
|
28
|
+
$did_it_work = :sadly_not
|
29
|
+
command.run(%w( -a x -b y -c -d -e ))
|
30
|
+
assert_equal :probably, $did_it_work
|
31
|
+
|
32
|
+
# Check
|
33
|
+
assert_equal 'moo', command.name
|
34
|
+
assert_equal 'dunno whatever', command.usage
|
35
|
+
assert_equal 'does stuff', command.summary
|
36
|
+
assert_equal 'This command does a lot of stuff.', command.description
|
37
|
+
|
38
|
+
# Check options
|
39
|
+
expected_option_definitions = Set.new([
|
40
|
+
{ :short => 'a', :long => 'aaa', :desc => 'opt a', :argument => :optional, :multiple => true, :hidden => false, :block => nil },
|
41
|
+
{ :short => 'b', :long => 'bbb', :desc => 'opt b', :argument => :required, :multiple => false, :hidden => false, :block => nil },
|
42
|
+
{ :short => 'c', :long => 'ccc', :desc => 'opt c', :argument => :optional, :multiple => false, :hidden => false, :block => nil },
|
43
|
+
{ :short => 'd', :long => 'ddd', :desc => 'opt d', :argument => :forbidden, :multiple => false, :hidden => false, :block => nil },
|
44
|
+
{ :short => 'e', :long => 'eee', :desc => 'opt e', :argument => :forbidden, :multiple => false, :hidden => false, :block => nil },
|
45
|
+
{ :short => 'f', :long => 'fff', :desc => 'opt f', :argument => :forbidden, :multiple => false, :hidden => true, :block => nil },
|
44
46
|
])
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
actual_option_definitions = Set.new(command.option_definitions)
|
48
|
+
assert_equal expected_option_definitions, actual_option_definitions
|
49
|
+
end
|
48
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
51
|
+
def test_optional_options
|
52
|
+
# Define
|
53
|
+
dsl = Cri::CommandDSL.new
|
54
|
+
dsl.instance_eval do
|
55
|
+
name 'moo'
|
56
|
+
usage 'dunno whatever'
|
57
|
+
summary 'does stuff'
|
58
|
+
description 'This command does a lot of stuff.'
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
+
flag :s, nil, 'short'
|
61
|
+
flag nil, :long, 'long'
|
60
62
|
|
61
|
-
|
62
|
-
|
63
|
+
run do |_opts, _args|
|
64
|
+
$did_it_work = :probably
|
65
|
+
end
|
63
66
|
end
|
64
|
-
|
65
|
-
command = dsl.command
|
66
|
-
|
67
|
-
# Run
|
68
|
-
$did_it_work = :sadly_not
|
69
|
-
command.run(%w( -s --long ))
|
70
|
-
assert_equal :probably, $did_it_work
|
71
|
-
|
72
|
-
# Check options
|
73
|
-
expected_option_definitions = Set.new([
|
74
|
-
{ :short => 's', :long => nil, :desc => 'short', :argument => :forbidden, :multiple => false, :block => nil },
|
75
|
-
{ :short => nil, :long => 'long', :desc => 'long', :argument => :forbidden, :multiple => false, :block => nil }
|
76
|
-
])
|
77
|
-
actual_option_definitions = Set.new(command.option_definitions)
|
78
|
-
assert_equal expected_option_definitions, actual_option_definitions
|
79
|
-
end
|
67
|
+
command = dsl.command
|
80
68
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
flag :f, :flag, 'sample flag option', :multiple => true
|
86
|
-
required :r, :required, 'sample required option', :multiple => true
|
87
|
-
optional :o, :optional, 'sample optional option', :multiple => true
|
69
|
+
# Run
|
70
|
+
$did_it_work = :sadly_not
|
71
|
+
command.run(%w( -s --long ))
|
72
|
+
assert_equal :probably, $did_it_work
|
88
73
|
|
89
|
-
|
74
|
+
# Check options
|
75
|
+
expected_option_definitions = Set.new([
|
76
|
+
{ :short => 's', :long => nil, :desc => 'short', :argument => :forbidden, :multiple => false, :hidden => false, :block => nil },
|
77
|
+
{ :short => nil, :long => 'long', :desc => 'long', :argument => :forbidden, :multiple => false, :hidden => false, :block => nil },
|
78
|
+
])
|
79
|
+
actual_option_definitions = Set.new(command.option_definitions)
|
80
|
+
assert_equal expected_option_definitions, actual_option_definitions
|
90
81
|
end
|
91
|
-
command = dsl.command
|
92
|
-
|
93
|
-
# Check options
|
94
|
-
expected_option_definitions = Set.new([
|
95
|
-
{ :short => 'f', :long => 'flag', :desc => 'sample flag option', :argument => :forbidden, :multiple => true, :block => nil },
|
96
|
-
{ :short => 'r', :long => 'required', :desc => 'sample required option', :argument => :required, :multiple => true, :block => nil },
|
97
|
-
{ :short => 'o', :long => 'optional', :desc => 'sample optional option', :argument => :optional, :multiple => true, :block => nil },
|
98
|
-
])
|
99
|
-
actual_option_definitions = Set.new(command.option_definitions)
|
100
|
-
assert_equal expected_option_definitions, actual_option_definitions
|
101
|
-
end
|
102
82
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
assert_raises ArgumentError do
|
83
|
+
def test_multiple
|
84
|
+
# Define
|
85
|
+
dsl = Cri::CommandDSL.new
|
107
86
|
dsl.instance_eval do
|
108
|
-
|
87
|
+
flag :f, :flag, 'flag', :multiple => true
|
88
|
+
required :r, :required, 'req', :multiple => true
|
89
|
+
optional :o, :optional, 'opt', :multiple => true
|
90
|
+
|
91
|
+
run { |_opts, _args| }
|
109
92
|
end
|
93
|
+
command = dsl.command
|
94
|
+
|
95
|
+
# Check options
|
96
|
+
expected_option_definitions = Set.new([
|
97
|
+
{ :short => 'f', :long => 'flag', :desc => 'flag', :argument => :forbidden, :multiple => true, :hidden => false, :block => nil },
|
98
|
+
{ :short => 'r', :long => 'required', :desc => 'req', :argument => :required, :multiple => true, :hidden => false, :block => nil },
|
99
|
+
{ :short => 'o', :long => 'optional', :desc => 'opt', :argument => :optional, :multiple => true, :hidden => false, :block => nil },
|
100
|
+
])
|
101
|
+
actual_option_definitions = Set.new(command.option_definitions)
|
102
|
+
assert_equal expected_option_definitions, actual_option_definitions
|
110
103
|
end
|
111
|
-
|
104
|
+
|
105
|
+
def test_hidden
|
106
|
+
# Define
|
107
|
+
dsl = Cri::CommandDSL.new
|
112
108
|
dsl.instance_eval do
|
113
|
-
flag
|
109
|
+
flag :f, :flag, 'flag', :hidden => true
|
110
|
+
required :r, :required, 'req', :hidden => true
|
111
|
+
optional :o, :optional, 'opt', :hidden => true
|
112
|
+
|
113
|
+
run { |_opts, _args| }
|
114
114
|
end
|
115
|
+
command = dsl.command
|
116
|
+
|
117
|
+
# Check options
|
118
|
+
expected_option_definitions = Set.new([
|
119
|
+
{ :short => 'f', :long => 'flag', :desc => 'flag', :argument => :forbidden, :multiple => false, :hidden => true, :block => nil },
|
120
|
+
{ :short => 'r', :long => 'required', :desc => 'req', :argument => :required, :multiple => false, :hidden => true, :block => nil },
|
121
|
+
{ :short => 'o', :long => 'optional', :desc => 'opt', :argument => :optional, :multiple => false, :hidden => true, :block => nil },
|
122
|
+
])
|
123
|
+
actual_option_definitions = Set.new(command.option_definitions)
|
124
|
+
assert_equal expected_option_definitions, actual_option_definitions
|
115
125
|
end
|
116
|
-
|
117
|
-
|
118
|
-
|
126
|
+
|
127
|
+
def test_required_short_and_long
|
128
|
+
# Define
|
129
|
+
dsl = Cri::CommandDSL.new
|
130
|
+
assert_raises ArgumentError do
|
131
|
+
dsl.instance_eval do
|
132
|
+
option nil, nil, 'meh'
|
133
|
+
end
|
119
134
|
end
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
135
|
+
assert_raises ArgumentError do
|
136
|
+
dsl.instance_eval do
|
137
|
+
flag nil, nil, 'meh'
|
138
|
+
end
|
139
|
+
end
|
140
|
+
assert_raises ArgumentError do
|
141
|
+
dsl.instance_eval do
|
142
|
+
required nil, nil, 'meh'
|
143
|
+
end
|
144
|
+
end
|
145
|
+
assert_raises ArgumentError do
|
146
|
+
dsl.instance_eval do
|
147
|
+
optional nil, nil, 'meh'
|
148
|
+
end
|
124
149
|
end
|
125
150
|
end
|
126
|
-
end
|
127
151
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
152
|
+
def test_subcommand
|
153
|
+
# Define
|
154
|
+
dsl = Cri::CommandDSL.new
|
155
|
+
dsl.instance_eval do
|
156
|
+
name 'super'
|
157
|
+
subcommand do |c|
|
158
|
+
c.name 'sub'
|
159
|
+
end
|
135
160
|
end
|
161
|
+
command = dsl.command
|
162
|
+
|
163
|
+
# Check
|
164
|
+
assert_equal 'super', command.name
|
165
|
+
assert_equal 1, command.subcommands.size
|
166
|
+
assert_equal 'sub', command.subcommands.to_a[0].name
|
136
167
|
end
|
137
|
-
command = dsl.command
|
138
168
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
169
|
+
def test_aliases
|
170
|
+
# Define
|
171
|
+
dsl = Cri::CommandDSL.new
|
172
|
+
dsl.instance_eval do
|
173
|
+
aliases :moo, :aah
|
174
|
+
end
|
175
|
+
command = dsl.command
|
144
176
|
|
145
|
-
|
146
|
-
|
147
|
-
dsl = Cri::CommandDSL.new
|
148
|
-
dsl.instance_eval do
|
149
|
-
aliases :moo, :aah
|
177
|
+
# Check
|
178
|
+
assert_equal %w( aah moo ), command.aliases.sort
|
150
179
|
end
|
151
|
-
command = dsl.command
|
152
|
-
|
153
|
-
# Check
|
154
|
-
assert_equal %w( aah moo ), command.aliases.sort
|
155
|
-
end
|
156
180
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
181
|
+
def test_run_arity
|
182
|
+
dsl = Cri::CommandDSL.new
|
183
|
+
assert_raises ArgumentError do
|
184
|
+
dsl.instance_eval do
|
185
|
+
run do |_a, _b, _c, _d, _e|
|
186
|
+
end
|
162
187
|
end
|
163
188
|
end
|
164
189
|
end
|
165
|
-
end
|
166
190
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
191
|
+
def test_runner
|
192
|
+
# Define
|
193
|
+
dsl = Cri::CommandDSL.new
|
194
|
+
dsl.instance_eval <<-EOS
|
195
|
+
class Cri::CommandDSLTestCaseCommandRunner < Cri::CommandRunner
|
196
|
+
def run
|
197
|
+
$did_it_work = arguments[0]
|
198
|
+
end
|
174
199
|
end
|
175
|
-
end
|
176
200
|
|
177
|
-
|
178
|
-
EOS
|
179
|
-
|
201
|
+
runner Cri::CommandDSLTestCaseCommandRunner
|
202
|
+
EOS
|
203
|
+
command = dsl.command
|
180
204
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
205
|
+
# Check
|
206
|
+
$did_it_work = false
|
207
|
+
command.run(%w( certainly ))
|
208
|
+
assert_equal 'certainly', $did_it_work
|
209
|
+
end
|
185
210
|
end
|
186
|
-
|
187
211
|
end
|
data/test/test_command_runner.rb
CHANGED
@@ -1,32 +1,31 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def test_initialize
|
14
|
-
|
15
|
-
runner = Cri::CommandRunner.new(@options, @arguments, @command)
|
3
|
+
module Cri
|
4
|
+
class CommandRunnerTestCase < Cri::TestCase
|
5
|
+
def setup
|
6
|
+
super
|
7
|
+
|
8
|
+
@options = { :vehicle => 'pig' }
|
9
|
+
@arguments = %w( baby_monkey )
|
10
|
+
@command = Cri::Command.new
|
11
|
+
end
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
assert_equal @command, runner.command
|
20
|
-
end
|
13
|
+
def test_initialize
|
14
|
+
runner = Cri::CommandRunner.new(@options, @arguments, @command)
|
21
15
|
|
22
|
-
|
23
|
-
|
24
|
-
|
16
|
+
assert_equal @options, runner.options
|
17
|
+
assert_equal @arguments, runner.arguments
|
18
|
+
assert_equal @command, runner.command
|
25
19
|
end
|
26
20
|
|
27
|
-
|
28
|
-
Cri::
|
21
|
+
def test_call_run
|
22
|
+
assert_raises(Cri::NotImplementedError) do
|
23
|
+
Cri::CommandRunner.new(@options, @arguments, @command).call
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_raises(Cri::NotImplementedError) do
|
27
|
+
Cri::CommandRunner.new(@options, @arguments, @command).run
|
28
|
+
end
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
32
31
|
end
|
data/test/test_option_parser.rb
CHANGED
@@ -1,294 +1,282 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
|
3
|
+
module Cri
|
4
|
+
class OptionParserTestCase < Cri::TestCase
|
5
|
+
def test_parse_without_options
|
6
|
+
input = %w( foo bar baz )
|
7
|
+
definitions = []
|
4
8
|
|
5
|
-
def test_parse_without_options
|
6
|
-
input = %w( foo bar baz )
|
7
|
-
definitions = []
|
8
|
-
|
9
|
-
parser = Cri::OptionParser.parse(input, definitions)
|
10
|
-
|
11
|
-
assert_equal({}, parser.options)
|
12
|
-
assert_equal([ 'foo', 'bar', 'baz' ], parser.arguments)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_parse_with_invalid_option
|
16
|
-
input = %w( foo -x )
|
17
|
-
definitions = []
|
18
|
-
|
19
|
-
result = nil
|
20
|
-
|
21
|
-
assert_raises(Cri::OptionParser::IllegalOptionError) do
|
22
9
|
parser = Cri::OptionParser.parse(input, definitions)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_parse_with_unused_options
|
27
|
-
input = %w( foo )
|
28
|
-
definitions = [
|
29
|
-
{ :long => 'aaa', :short => 'a', :argument => :forbidden }
|
30
|
-
]
|
31
10
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_parse_with_long_valueless_option
|
38
|
-
input = %w( foo --aaa bar )
|
39
|
-
definitions = [
|
40
|
-
{ :long => 'aaa', :short => 'a', :argument => :forbidden }
|
41
|
-
]
|
11
|
+
assert_equal({}, parser.options)
|
12
|
+
assert_equal(%w(foo bar baz), parser.arguments)
|
13
|
+
end
|
42
14
|
|
43
|
-
|
15
|
+
def test_parse_with_invalid_option
|
16
|
+
input = %w( foo -x )
|
17
|
+
definitions = []
|
44
18
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
def test_parse_with_long_valueful_option
|
50
|
-
input = %w( foo --aaa xxx bar )
|
51
|
-
definitions = [
|
52
|
-
{ :long => 'aaa', :short => 'a', :argument => :required }
|
53
|
-
]
|
19
|
+
assert_raises(Cri::OptionParser::IllegalOptionError) do
|
20
|
+
Cri::OptionParser.parse(input, definitions)
|
21
|
+
end
|
22
|
+
end
|
54
23
|
|
55
|
-
|
24
|
+
def test_parse_with_unused_options
|
25
|
+
input = %w( foo )
|
26
|
+
definitions = [
|
27
|
+
{ :long => 'aaa', :short => 'a', :argument => :forbidden },
|
28
|
+
]
|
56
29
|
|
57
|
-
|
58
|
-
assert_equal([ 'foo', 'bar' ], parser.arguments)
|
59
|
-
end
|
30
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
60
31
|
|
61
|
-
|
62
|
-
|
63
|
-
definitions = [
|
64
|
-
{ :long => 'aaa', :short => 'a', :argument => :required }
|
65
|
-
]
|
32
|
+
assert(!parser.options[:aaa])
|
33
|
+
end
|
66
34
|
|
67
|
-
|
35
|
+
def test_parse_with_long_valueless_option
|
36
|
+
input = %w( foo --aaa bar )
|
37
|
+
definitions = [
|
38
|
+
{ :long => 'aaa', :short => 'a', :argument => :forbidden },
|
39
|
+
]
|
68
40
|
|
69
|
-
|
70
|
-
assert_equal([ 'foo', 'bar' ], parser.arguments)
|
71
|
-
end
|
41
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
72
42
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
{ :long => 'aaa', :short => 'a', :argument => :required }
|
77
|
-
]
|
43
|
+
assert(parser.options[:aaa])
|
44
|
+
assert_equal(%w(foo bar), parser.arguments)
|
45
|
+
end
|
78
46
|
|
79
|
-
|
47
|
+
def test_parse_with_long_valueful_option
|
48
|
+
input = %w( foo --aaa xxx bar )
|
49
|
+
definitions = [
|
50
|
+
{ :long => 'aaa', :short => 'a', :argument => :required },
|
51
|
+
]
|
80
52
|
|
81
|
-
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
82
53
|
parser = Cri::OptionParser.parse(input, definitions)
|
83
|
-
end
|
84
|
-
end
|
85
54
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
{ :long => 'all', :short => 'a', :argument => :required },
|
90
|
-
{ :long => 'port', :short => 'p', :argument => :required }
|
91
|
-
]
|
55
|
+
assert_equal({ :aaa => 'xxx' }, parser.options)
|
56
|
+
assert_equal(%w(foo bar), parser.arguments)
|
57
|
+
end
|
92
58
|
|
93
|
-
|
59
|
+
def test_parse_with_long_valueful_equalsign_option
|
60
|
+
input = %w( foo --aaa=xxx bar )
|
61
|
+
definitions = [
|
62
|
+
{ :long => 'aaa', :short => 'a', :argument => :required },
|
63
|
+
]
|
94
64
|
|
95
|
-
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
96
65
|
parser = Cri::OptionParser.parse(input, definitions)
|
66
|
+
|
67
|
+
assert_equal({ :aaa => 'xxx' }, parser.options)
|
68
|
+
assert_equal(%w(foo bar), parser.arguments)
|
97
69
|
end
|
98
|
-
end
|
99
70
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
71
|
+
def test_parse_with_long_valueful_option_with_missing_value
|
72
|
+
input = %w( foo --aaa )
|
73
|
+
definitions = [
|
74
|
+
{ :long => 'aaa', :short => 'a', :argument => :required },
|
75
|
+
]
|
105
76
|
|
106
|
-
|
77
|
+
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
78
|
+
Cri::OptionParser.parse(input, definitions)
|
79
|
+
end
|
80
|
+
end
|
107
81
|
|
108
|
-
|
109
|
-
|
110
|
-
|
82
|
+
def test_parse_with_two_long_valueful_options
|
83
|
+
input = %w( foo --all --port 2 )
|
84
|
+
definitions = [
|
85
|
+
{ :long => 'all', :short => 'a', :argument => :required },
|
86
|
+
{ :long => 'port', :short => 'p', :argument => :required },
|
87
|
+
]
|
111
88
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
]
|
89
|
+
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
90
|
+
Cri::OptionParser.parse(input, definitions)
|
91
|
+
end
|
92
|
+
end
|
117
93
|
|
118
|
-
|
94
|
+
def test_parse_with_long_valueless_option_with_optional_value
|
95
|
+
input = %w( foo --aaa )
|
96
|
+
definitions = [
|
97
|
+
{ :long => 'aaa', :short => 'a', :argument => :optional },
|
98
|
+
]
|
119
99
|
|
120
|
-
|
121
|
-
assert_equal([ 'foo' ], parser.arguments)
|
122
|
-
end
|
100
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
123
101
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
{ :long => 'aaa', :short => 'a', :argument => :optional },
|
128
|
-
{ :long => 'bbb', :short => 'b', :argument => :forbidden },
|
129
|
-
{ :long => 'ccc', :short => 'c', :argument => :forbidden }
|
130
|
-
]
|
102
|
+
assert(parser.options[:aaa])
|
103
|
+
assert_equal(['foo'], parser.arguments)
|
104
|
+
end
|
131
105
|
|
132
|
-
|
106
|
+
def test_parse_with_long_valueful_option_with_optional_value
|
107
|
+
input = %w( foo --aaa xxx )
|
108
|
+
definitions = [
|
109
|
+
{ :long => 'aaa', :short => 'a', :argument => :optional },
|
110
|
+
]
|
133
111
|
|
134
|
-
|
135
|
-
assert(parser.options[:bbb])
|
136
|
-
assert(parser.options[:ccc])
|
137
|
-
assert_equal([ 'foo' ], parser.arguments)
|
138
|
-
end
|
112
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
139
113
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
{ :long => 'aaa', :short => 'a', :argument => :forbidden }
|
144
|
-
]
|
114
|
+
assert_equal({ :aaa => 'xxx' }, parser.options)
|
115
|
+
assert_equal(['foo'], parser.arguments)
|
116
|
+
end
|
145
117
|
|
146
|
-
|
118
|
+
def test_parse_with_long_valueless_option_with_optional_value_and_more_options
|
119
|
+
input = %w( foo --aaa -b -c )
|
120
|
+
definitions = [
|
121
|
+
{ :long => 'aaa', :short => 'a', :argument => :optional },
|
122
|
+
{ :long => 'bbb', :short => 'b', :argument => :forbidden },
|
123
|
+
{ :long => 'ccc', :short => 'c', :argument => :forbidden },
|
124
|
+
]
|
147
125
|
|
148
|
-
|
149
|
-
assert_equal([ 'foo', 'bar' ], parser.arguments)
|
150
|
-
end
|
126
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
151
127
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
128
|
+
assert(parser.options[:aaa])
|
129
|
+
assert(parser.options[:bbb])
|
130
|
+
assert(parser.options[:ccc])
|
131
|
+
assert_equal(['foo'], parser.arguments)
|
132
|
+
end
|
157
133
|
|
158
|
-
|
134
|
+
def test_parse_with_short_valueless_options
|
135
|
+
input = %w( foo -a bar )
|
136
|
+
definitions = [
|
137
|
+
{ :long => 'aaa', :short => 'a', :argument => :forbidden },
|
138
|
+
]
|
159
139
|
|
160
|
-
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
161
140
|
parser = Cri::OptionParser.parse(input, definitions)
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
def test_parse_with_short_combined_valueless_options
|
166
|
-
input = %w( foo -abc bar )
|
167
|
-
definitions = [
|
168
|
-
{ :long => 'aaa', :short => 'a', :argument => :forbidden },
|
169
|
-
{ :long => 'bbb', :short => 'b', :argument => :forbidden },
|
170
|
-
{ :long => 'ccc', :short => 'c', :argument => :forbidden }
|
171
|
-
]
|
172
141
|
|
173
|
-
|
142
|
+
assert(parser.options[:aaa])
|
143
|
+
assert_equal(%w(foo bar), parser.arguments)
|
144
|
+
end
|
174
145
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
146
|
+
def test_parse_with_short_valueful_option_with_missing_value
|
147
|
+
input = %w( foo -a )
|
148
|
+
definitions = [
|
149
|
+
{ :long => 'aaa', :short => 'a', :argument => :required },
|
150
|
+
]
|
180
151
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
{ :long => 'bbb', :short => 'b', :argument => :forbidden },
|
186
|
-
{ :long => 'ccc', :short => 'c', :argument => :forbidden }
|
187
|
-
]
|
152
|
+
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
153
|
+
Cri::OptionParser.parse(input, definitions)
|
154
|
+
end
|
155
|
+
end
|
188
156
|
|
189
|
-
|
157
|
+
def test_parse_with_short_combined_valueless_options
|
158
|
+
input = %w( foo -abc bar )
|
159
|
+
definitions = [
|
160
|
+
{ :long => 'aaa', :short => 'a', :argument => :forbidden },
|
161
|
+
{ :long => 'bbb', :short => 'b', :argument => :forbidden },
|
162
|
+
{ :long => 'ccc', :short => 'c', :argument => :forbidden },
|
163
|
+
]
|
190
164
|
|
191
|
-
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
192
165
|
parser = Cri::OptionParser.parse(input, definitions)
|
166
|
+
|
167
|
+
assert(parser.options[:aaa])
|
168
|
+
assert(parser.options[:bbb])
|
169
|
+
assert(parser.options[:ccc])
|
170
|
+
assert_equal(%w(foo bar), parser.arguments)
|
193
171
|
end
|
194
|
-
end
|
195
172
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
173
|
+
def test_parse_with_short_combined_valueful_options_with_missing_value
|
174
|
+
input = %w( foo -abc bar )
|
175
|
+
definitions = [
|
176
|
+
{ :long => 'aaa', :short => 'a', :argument => :required },
|
177
|
+
{ :long => 'bbb', :short => 'b', :argument => :forbidden },
|
178
|
+
{ :long => 'ccc', :short => 'c', :argument => :forbidden },
|
179
|
+
]
|
180
|
+
|
181
|
+
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
182
|
+
Cri::OptionParser.parse(input, definitions)
|
183
|
+
end
|
184
|
+
end
|
202
185
|
|
203
|
-
|
186
|
+
def test_parse_with_two_short_valueful_options
|
187
|
+
input = %w( foo -a -p 2 )
|
188
|
+
definitions = [
|
189
|
+
{ :long => 'all', :short => 'a', :argument => :required },
|
190
|
+
{ :long => 'port', :short => 'p', :argument => :required },
|
191
|
+
]
|
204
192
|
|
205
|
-
|
206
|
-
|
193
|
+
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
194
|
+
Cri::OptionParser.parse(input, definitions)
|
195
|
+
end
|
207
196
|
end
|
208
|
-
end
|
209
197
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
198
|
+
def test_parse_with_short_valueless_option_with_optional_value
|
199
|
+
input = %w( foo -a )
|
200
|
+
definitions = [
|
201
|
+
{ :long => 'aaa', :short => 'a', :argument => :optional },
|
202
|
+
]
|
215
203
|
|
216
|
-
|
204
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
217
205
|
|
218
|
-
|
219
|
-
|
220
|
-
|
206
|
+
assert(parser.options[:aaa])
|
207
|
+
assert_equal(['foo'], parser.arguments)
|
208
|
+
end
|
221
209
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
210
|
+
def test_parse_with_short_valueful_option_with_optional_value
|
211
|
+
input = %w( foo -a xxx )
|
212
|
+
definitions = [
|
213
|
+
{ :long => 'aaa', :short => 'a', :argument => :optional },
|
214
|
+
]
|
227
215
|
|
228
|
-
|
216
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
229
217
|
|
230
|
-
|
231
|
-
|
232
|
-
|
218
|
+
assert_equal({ :aaa => 'xxx' }, parser.options)
|
219
|
+
assert_equal(['foo'], parser.arguments)
|
220
|
+
end
|
233
221
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
222
|
+
def test_parse_with_short_valueless_option_with_optional_value_and_more_options
|
223
|
+
input = %w( foo -a -b -c )
|
224
|
+
definitions = [
|
225
|
+
{ :long => 'aaa', :short => 'a', :argument => :optional },
|
226
|
+
{ :long => 'bbb', :short => 'b', :argument => :forbidden },
|
227
|
+
{ :long => 'ccc', :short => 'c', :argument => :forbidden },
|
228
|
+
]
|
241
229
|
|
242
|
-
|
230
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
243
231
|
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
232
|
+
assert(parser.options[:aaa])
|
233
|
+
assert(parser.options[:bbb])
|
234
|
+
assert(parser.options[:ccc])
|
235
|
+
assert_equal(['foo'], parser.arguments)
|
236
|
+
end
|
249
237
|
|
250
|
-
|
251
|
-
|
252
|
-
|
238
|
+
def test_parse_with_single_hyphen
|
239
|
+
input = %w( foo - bar )
|
240
|
+
definitions = []
|
253
241
|
|
254
|
-
|
242
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
255
243
|
|
256
|
-
|
257
|
-
|
258
|
-
|
244
|
+
assert_equal({}, parser.options)
|
245
|
+
assert_equal(['foo', '-', 'bar'], parser.arguments)
|
246
|
+
end
|
259
247
|
|
260
|
-
|
261
|
-
|
262
|
-
|
248
|
+
def test_parse_with_end_marker
|
249
|
+
input = %w( foo bar -- -x --yyy -abc )
|
250
|
+
definitions = []
|
263
251
|
|
264
|
-
|
252
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
265
253
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
254
|
+
assert_equal({}, parser.options)
|
255
|
+
assert_equal(['foo', 'bar', '-x', '--yyy', '-abc'], parser.arguments)
|
256
|
+
assert_equal(['foo', 'bar', '--', '-x', '--yyy', '-abc'], parser.arguments.raw)
|
257
|
+
end
|
270
258
|
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
259
|
+
def test_parse_with_end_marker_between_option_key_and_value
|
260
|
+
input = %w( foo --aaa -- zzz )
|
261
|
+
definitions = [
|
262
|
+
{ :long => 'aaa', :short => 'a', :argument => :required },
|
263
|
+
]
|
276
264
|
|
277
|
-
|
278
|
-
|
265
|
+
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
266
|
+
Cri::OptionParser.parse(input, definitions)
|
267
|
+
end
|
279
268
|
end
|
280
|
-
end
|
281
269
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
270
|
+
def test_parse_with_multiple_options
|
271
|
+
input = %w( foo -o test -o test2 -v -v -v)
|
272
|
+
definitions = [
|
273
|
+
{ :long => 'long', :short => 'o', :argument => :required, :multiple => true },
|
274
|
+
{ :long => 'verbose', :short => 'v', :multiple => true },
|
275
|
+
]
|
276
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
289
277
|
|
290
|
-
|
291
|
-
|
278
|
+
assert_equal(%w(test test2), parser.options[:long])
|
279
|
+
assert_equal(3, parser.options[:verbose].size)
|
280
|
+
end
|
292
281
|
end
|
293
|
-
|
294
282
|
end
|