cri 2.8.0 → 2.9.0
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +15 -12
- data/NEWS.md +29 -36
- data/README.adoc +28 -5
- data/Rakefile +2 -2
- data/lib/cri/argument_array.rb +1 -1
- data/lib/cri/command_dsl.rb +6 -0
- data/lib/cri/help_renderer.rb +4 -5
- data/lib/cri/option_parser.rb +6 -7
- data/lib/cri/version.rb +1 -1
- data/test/test_argument_array.rb +1 -1
- data/test/test_basic_help.rb +2 -2
- data/test/test_basic_root.rb +2 -2
- data/test/test_command.rb +21 -21
- data/test/test_command_dsl.rb +60 -26
- data/test/test_command_runner.rb +1 -1
- data/test/test_help_renderer.rb +45 -0
- data/test/test_option_parser.rb +137 -33
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5285c76db1bf769830b9258e04e041ae4ce35110
|
|
4
|
+
data.tar.gz: a4a34c4114ef34eb00deb31b62a60e7a39111086
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0546e56bded0584b797f35771344ef9057f903322b61e2fa94d17471250bcb92bda6cc77f7a4086526c9acd45e04221ffc773f1b17d8ff37d1add1d36fcce661
|
|
7
|
+
data.tar.gz: 9c94286223b03f5bd3621ef51659a3239e70ef130aa4874156cf66db2650e527c15c74e40729706e83e10a466bdab12623cbc8ab0daf9ad136ff58d5fcfd3c4f
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
cri (2.
|
|
4
|
+
cri (2.9.0)
|
|
5
5
|
colored (~> 1.2)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
@@ -10,21 +10,24 @@ GEM
|
|
|
10
10
|
asciidoctor (1.5.5)
|
|
11
11
|
ast (2.3.0)
|
|
12
12
|
colored (1.2)
|
|
13
|
-
coveralls (0.8.
|
|
13
|
+
coveralls (0.8.21)
|
|
14
14
|
json (>= 1.8, < 3)
|
|
15
15
|
simplecov (~> 0.14.1)
|
|
16
16
|
term-ansicolor (~> 1.3)
|
|
17
17
|
thor (~> 0.19.4)
|
|
18
18
|
tins (~> 1.6)
|
|
19
19
|
docile (1.1.5)
|
|
20
|
-
json (2.0
|
|
21
|
-
minitest (5.10.
|
|
20
|
+
json (2.1.0)
|
|
21
|
+
minitest (5.10.2)
|
|
22
|
+
parallel (1.11.2)
|
|
22
23
|
parser (2.4.0.0)
|
|
23
24
|
ast (~> 2.2)
|
|
24
25
|
powerpack (0.1.1)
|
|
25
|
-
rainbow (2.2.
|
|
26
|
+
rainbow (2.2.2)
|
|
27
|
+
rake
|
|
26
28
|
rake (12.0.0)
|
|
27
|
-
rubocop (0.
|
|
29
|
+
rubocop (0.49.1)
|
|
30
|
+
parallel (~> 1.10)
|
|
28
31
|
parser (>= 2.3.3.1, < 3.0)
|
|
29
32
|
powerpack (~> 0.1)
|
|
30
33
|
rainbow (>= 1.99.1, < 3.0)
|
|
@@ -35,13 +38,13 @@ GEM
|
|
|
35
38
|
docile (~> 1.1.0)
|
|
36
39
|
json (>= 1.8, < 3)
|
|
37
40
|
simplecov-html (~> 0.10.0)
|
|
38
|
-
simplecov-html (0.10.
|
|
39
|
-
term-ansicolor (1.
|
|
41
|
+
simplecov-html (0.10.1)
|
|
42
|
+
term-ansicolor (1.6.0)
|
|
40
43
|
tins (~> 1.0)
|
|
41
44
|
thor (0.19.4)
|
|
42
|
-
tins (1.
|
|
43
|
-
unicode-display_width (1.1
|
|
44
|
-
yard (0.9.
|
|
45
|
+
tins (1.14.0)
|
|
46
|
+
unicode-display_width (1.2.1)
|
|
47
|
+
yard (0.9.9)
|
|
45
48
|
|
|
46
49
|
PLATFORMS
|
|
47
50
|
ruby
|
|
@@ -57,4 +60,4 @@ DEPENDENCIES
|
|
|
57
60
|
yard
|
|
58
61
|
|
|
59
62
|
BUNDLED WITH
|
|
60
|
-
1.
|
|
63
|
+
1.15.1
|
data/NEWS.md
CHANGED
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
Cri News
|
|
2
2
|
========
|
|
3
3
|
|
|
4
|
-
2.
|
|
5
|
-
-----
|
|
4
|
+
## 2.9.0
|
|
6
5
|
|
|
7
6
|
Features:
|
|
8
7
|
|
|
9
|
-
*
|
|
10
|
-
* Allow specifying the default subcommand (#54)
|
|
8
|
+
* Allowed specifying default option value (#55)
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
Enhancements:
|
|
11
|
+
|
|
12
|
+
* Added support for specifying values for combined options (#56)
|
|
13
|
+
|
|
14
|
+
## 2.8.0
|
|
15
|
+
|
|
16
|
+
Features:
|
|
17
|
+
|
|
18
|
+
* Allowed passing `hard_exit: false` to `Command#run` to prevent `SystemExit` (#51)
|
|
19
|
+
* Allowed specifying the default subcommand (#54)
|
|
20
|
+
|
|
21
|
+
## 2.7.1
|
|
14
22
|
|
|
15
23
|
Fixes:
|
|
16
24
|
|
|
17
25
|
* Fixed some grammatical mistakes
|
|
18
26
|
|
|
19
|
-
2.7.0
|
|
20
|
-
-----
|
|
27
|
+
## 2.7.0
|
|
21
28
|
|
|
22
29
|
Features:
|
|
23
30
|
|
|
@@ -28,80 +35,66 @@ Enhancements:
|
|
|
28
35
|
* Added option values to help output (#37, #40, #41)
|
|
29
36
|
* Made option descriptions wrap (#36, #45) [Bart Mesuere]
|
|
30
37
|
|
|
31
|
-
2.6.1
|
|
32
|
-
-----
|
|
38
|
+
## 2.6.1
|
|
33
39
|
|
|
34
40
|
* Disable ANSI color codes when not supported (#31, #32)
|
|
35
41
|
|
|
36
|
-
2.6.0
|
|
37
|
-
-----
|
|
42
|
+
## 2.6.0
|
|
38
43
|
|
|
39
44
|
* Added support for multi-valued options (#29) [Toon Willems]
|
|
40
45
|
|
|
41
|
-
2.5.0
|
|
42
|
-
-----
|
|
46
|
+
## 2.5.0
|
|
43
47
|
|
|
44
48
|
* Made the default help command handle subcommands (#27)
|
|
45
49
|
* Added `#raw` method to argument arrays, returning all arguments including `--` (#22)
|
|
46
50
|
|
|
47
|
-
2.4.1
|
|
48
|
-
-----
|
|
51
|
+
## 2.4.1
|
|
49
52
|
|
|
50
53
|
* Fixed ordering of option groups on Ruby 1.8.x (#14, #15)
|
|
51
54
|
* Fixed ordering of commands when --verbose is passed (#16, #18)
|
|
52
55
|
|
|
53
|
-
2.4.0
|
|
54
|
-
-----
|
|
56
|
+
## 2.4.0
|
|
55
57
|
|
|
56
58
|
* Allowed either short or long option to be, eh, optional (#9, #10) [Ken Coar]
|
|
57
59
|
* Fixed wrap-and-indent behavior (#12) [Ken Coar]
|
|
58
60
|
* Moved version information into `cri/version`
|
|
59
61
|
|
|
60
|
-
2.3.0
|
|
61
|
-
-----
|
|
62
|
+
## 2.3.0
|
|
62
63
|
|
|
63
64
|
* Added colors (#1)
|
|
64
65
|
* Added support for marking commands as hidden
|
|
65
66
|
|
|
66
|
-
2.2.1
|
|
67
|
-
-----
|
|
67
|
+
## 2.2.1
|
|
68
68
|
|
|
69
69
|
* Made command help sort subcommands
|
|
70
70
|
|
|
71
|
-
2.2.0
|
|
72
|
-
-----
|
|
71
|
+
## 2.2.0
|
|
73
72
|
|
|
74
73
|
* Allowed commands with subcommands to have a run block
|
|
75
74
|
|
|
76
|
-
2.1.0
|
|
77
|
-
-----
|
|
75
|
+
## 2.1.0
|
|
78
76
|
|
|
79
77
|
* Added support for runners
|
|
80
78
|
* Split up local/global command options
|
|
81
79
|
|
|
82
|
-
2.0.2
|
|
83
|
-
-----
|
|
80
|
+
## 2.0.2
|
|
84
81
|
|
|
85
82
|
* Added command filename to stack traces
|
|
86
83
|
|
|
87
|
-
2.0.1
|
|
88
|
-
-----
|
|
84
|
+
## 2.0.1
|
|
89
85
|
|
|
90
86
|
* Sorted ambiguous command names
|
|
91
87
|
* Restored compatibility with Ruby 1.8.x
|
|
92
88
|
|
|
93
|
-
2.0.0
|
|
94
|
-
-----
|
|
89
|
+
## 2.0.0
|
|
95
90
|
|
|
96
91
|
* Added DSL
|
|
97
92
|
* Added support for nested commands
|
|
98
93
|
|
|
99
|
-
1.0.1
|
|
100
|
-
-----
|
|
94
|
+
## 1.0.1
|
|
101
95
|
|
|
102
96
|
* Made gem actually include code. D'oh.
|
|
103
97
|
|
|
104
|
-
1.0.0
|
|
105
|
-
-----
|
|
98
|
+
## 1.0.0
|
|
106
99
|
|
|
107
100
|
* Initial release!
|
data/README.adoc
CHANGED
|
@@ -116,18 +116,41 @@ Options can be defined using the following methods:
|
|
|
116
116
|
* `Cri::CommandDSL#required` (implies required argument)
|
|
117
117
|
* `Cri::CommandDSL#optional` (implies optional argument)
|
|
118
118
|
|
|
119
|
-
All these methods take
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
119
|
+
All these methods take these arguments:
|
|
120
|
+
|
|
121
|
+
1. a short option
|
|
122
|
+
2. a long option
|
|
123
|
+
3. a description
|
|
124
|
+
4. optional extra parameters
|
|
125
|
+
|
|
126
|
+
Either the short or the long form can be nil, but not both (because that
|
|
127
|
+
would not make any sense). In the example above, the `--more` option has no
|
|
128
|
+
short form.
|
|
123
129
|
|
|
124
130
|
Each of the above methods also take a block, which will be executed when the
|
|
125
131
|
option is found. The argument to the block are the option value (`true` in
|
|
126
132
|
case the option does not have an argument) and the command.
|
|
127
133
|
|
|
134
|
+
==== Options with default values ====
|
|
135
|
+
|
|
136
|
+
The `:default` parameter sets the option value that will be used if no explicit value is provided:
|
|
137
|
+
|
|
138
|
+
[source,ruby]
|
|
139
|
+
--------------------------------------------------------------------------------
|
|
140
|
+
optional :a, :animal, 'add animal', default: 'giraffe'
|
|
141
|
+
--------------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
In the example above, the value for the `--animal` option will be the string
|
|
144
|
+
`"giraffe"`, unless otherwise specified:
|
|
145
|
+
|
|
146
|
+
--------------------------------------------------------------------------------
|
|
147
|
+
OPTIONS
|
|
148
|
+
-a --animal[=<value>] add animal (default: giraffe)
|
|
149
|
+
--------------------------------------------------------------------------------
|
|
150
|
+
|
|
128
151
|
==== Multivalued options ====
|
|
129
152
|
|
|
130
|
-
Each of these four methods take a `:multiple`
|
|
153
|
+
Each of these four methods take a `:multiple` parameter. When set to true, multiple
|
|
131
154
|
option valus are accepted, and the option values will be stored in an array.
|
|
132
155
|
|
|
133
156
|
For example, to parse the command line options string `-o foo.txt -o bar.txt`
|
data/Rakefile
CHANGED
|
@@ -20,9 +20,9 @@ Rake::TestTask.new(:test_unit) do |t|
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
RuboCop::RakeTask.new(:test_style) do |task|
|
|
23
|
-
task.options = %w
|
|
23
|
+
task.options = %w[--display-cop-names --format simple]
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
task test: %i
|
|
26
|
+
task test: %i[test_unit test_style]
|
|
27
27
|
|
|
28
28
|
task default: :test
|
data/lib/cri/argument_array.rb
CHANGED
|
@@ -8,7 +8,7 @@ module Cri
|
|
|
8
8
|
# @param [Array<String>] raw_arguments A list of raw arguments, i.e.
|
|
9
9
|
# including any separator arguments (`--`).
|
|
10
10
|
def initialize(raw_arguments)
|
|
11
|
-
super(raw_arguments.reject { |a| '--'
|
|
11
|
+
super(raw_arguments.reject { |a| a == '--' })
|
|
12
12
|
@raw_arguments = raw_arguments
|
|
13
13
|
end
|
|
14
14
|
|
data/lib/cri/command_dsl.rb
CHANGED
|
@@ -121,11 +121,16 @@ module Cri
|
|
|
121
121
|
requiredness = params.fetch(:argument, :forbidden)
|
|
122
122
|
multiple = params.fetch(:multiple, false)
|
|
123
123
|
hidden = params.fetch(:hidden, false)
|
|
124
|
+
default = params.fetch(:default, nil)
|
|
124
125
|
|
|
125
126
|
if short.nil? && long.nil?
|
|
126
127
|
raise ArgumentError, 'short and long options cannot both be nil'
|
|
127
128
|
end
|
|
128
129
|
|
|
130
|
+
if default && requiredness != :optional
|
|
131
|
+
raise ArgumentError, "a default value cannot be specified for options with #{requiredness} values"
|
|
132
|
+
end
|
|
133
|
+
|
|
129
134
|
@command.option_definitions << {
|
|
130
135
|
short: short.nil? ? nil : short.to_s,
|
|
131
136
|
long: long.nil? ? nil : long.to_s,
|
|
@@ -134,6 +139,7 @@ module Cri
|
|
|
134
139
|
multiple: multiple,
|
|
135
140
|
block: block,
|
|
136
141
|
hidden: hidden,
|
|
142
|
+
default: default,
|
|
137
143
|
}
|
|
138
144
|
end
|
|
139
145
|
alias opt option
|
data/lib/cri/help_renderer.rb
CHANGED
|
@@ -146,11 +146,10 @@ module Cri
|
|
|
146
146
|
text << "\n"
|
|
147
147
|
|
|
148
148
|
ordered_defs = defs.sort_by { |x| x[:short] || x[:long] }
|
|
149
|
-
ordered_defs.each do |opt_def|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
end
|
|
149
|
+
ordered_defs.reject { |opt_def| opt_def[:hidden] }.each do |opt_def|
|
|
150
|
+
text << format_opt_def(opt_def, length)
|
|
151
|
+
desc = opt_def[:desc] + (opt_def[:default] ? " (default: #{opt_def[:default]})" : '')
|
|
152
|
+
text << fmt.wrap_and_indent(desc, LINE_WIDTH, length + OPT_DESC_SPACING + DESC_INDENT, true) << "\n"
|
|
154
153
|
end
|
|
155
154
|
end
|
|
156
155
|
|
data/lib/cri/option_parser.rb
CHANGED
|
@@ -199,7 +199,7 @@ module Cri
|
|
|
199
199
|
definition = @definitions.find { |d| d[:long] == option_key }
|
|
200
200
|
raise IllegalOptionError.new(option_key) if definition.nil?
|
|
201
201
|
|
|
202
|
-
if %i
|
|
202
|
+
if %i[required optional].include?(definition[:argument])
|
|
203
203
|
# Get option value if necessary
|
|
204
204
|
if option_value.nil?
|
|
205
205
|
option_value = find_option_value(definition, option_key)
|
|
@@ -223,10 +223,7 @@ module Cri
|
|
|
223
223
|
definition = @definitions.find { |d| d[:short] == option_key }
|
|
224
224
|
raise IllegalOptionError.new(option_key) if definition.nil?
|
|
225
225
|
|
|
226
|
-
if
|
|
227
|
-
# This is a combined option and it requires an argument, so complain
|
|
228
|
-
raise OptionRequiresAnArgumentError.new(option_key)
|
|
229
|
-
elsif %i(required optional).include?(definition[:argument])
|
|
226
|
+
if %i[required optional].include?(definition[:argument])
|
|
230
227
|
# Get option value
|
|
231
228
|
option_value = find_option_value(definition, option_key)
|
|
232
229
|
|
|
@@ -242,7 +239,9 @@ module Cri
|
|
|
242
239
|
def find_option_value(definition, option_key)
|
|
243
240
|
option_value = @unprocessed_arguments_and_options.shift
|
|
244
241
|
if option_value.nil? || option_value =~ /^-/
|
|
245
|
-
if definition[:argument] == :
|
|
242
|
+
if definition[:argument] == :optional && definition[:default]
|
|
243
|
+
option_value = definition[:default]
|
|
244
|
+
elsif definition[:argument] == :required
|
|
246
245
|
raise OptionRequiresAnArgumentError.new(option_key)
|
|
247
246
|
else
|
|
248
247
|
@unprocessed_arguments_and_options.unshift(option_value)
|
|
@@ -267,7 +266,7 @@ module Cri
|
|
|
267
266
|
def add_argument(value)
|
|
268
267
|
@raw_arguments << value
|
|
269
268
|
|
|
270
|
-
unless '--'
|
|
269
|
+
unless value == '--'
|
|
271
270
|
delegate.argument_added(value, self) unless delegate.nil?
|
|
272
271
|
end
|
|
273
272
|
end
|
data/lib/cri/version.rb
CHANGED
data/test/test_argument_array.rb
CHANGED
|
@@ -4,7 +4,7 @@ module Cri
|
|
|
4
4
|
class ArgumentArrayTestCase < Cri::TestCase
|
|
5
5
|
def test_initialize
|
|
6
6
|
arr = Cri::ArgumentArray.new(['foo', 'bar', '--', 'baz'])
|
|
7
|
-
assert_equal %w
|
|
7
|
+
assert_equal %w[foo bar baz], arr
|
|
8
8
|
assert_equal ['foo', 'bar', '--', 'baz'], arr.raw
|
|
9
9
|
end
|
|
10
10
|
end
|
data/test/test_basic_help.rb
CHANGED
|
@@ -49,7 +49,7 @@ module Cri
|
|
|
49
49
|
|
|
50
50
|
# Subcommand
|
|
51
51
|
stdout, stderr = capture_io_while do
|
|
52
|
-
help_cmd.run(%w
|
|
52
|
+
help_cmd.run(%w[foo subsubby])
|
|
53
53
|
end
|
|
54
54
|
assert_match(/I am subsubby!/m, stdout)
|
|
55
55
|
assert_equal('', stderr)
|
|
@@ -57,7 +57,7 @@ module Cri
|
|
|
57
57
|
# Non-existing subcommand
|
|
58
58
|
stdout, stderr = capture_io_while do
|
|
59
59
|
assert_raises SystemExit do
|
|
60
|
-
help_cmd.run(%w
|
|
60
|
+
help_cmd.run(%w[foo mysterycmd])
|
|
61
61
|
end
|
|
62
62
|
end
|
|
63
63
|
assert_equal '', stdout
|
data/test/test_basic_root.rb
CHANGED
|
@@ -7,7 +7,7 @@ module Cri
|
|
|
7
7
|
|
|
8
8
|
stdout, _stderr = capture_io_while do
|
|
9
9
|
err = assert_raises SystemExit do
|
|
10
|
-
cmd.run(%w
|
|
10
|
+
cmd.run(%w[-h])
|
|
11
11
|
end
|
|
12
12
|
assert_equal 0, err.status
|
|
13
13
|
end
|
|
@@ -19,7 +19,7 @@ module Cri
|
|
|
19
19
|
cmd = Cri::Command.new_basic_root
|
|
20
20
|
|
|
21
21
|
stdout, _stderr = capture_io_while do
|
|
22
|
-
cmd.run(%w
|
|
22
|
+
cmd.run(%w[-h], {}, hard_exit: false)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
assert stdout =~ /COMMANDS.*\n.*help.*show help/
|
data/test/test_command.rb
CHANGED
|
@@ -120,7 +120,7 @@ module Cri
|
|
|
120
120
|
|
|
121
121
|
def test_invoke_simple_without_opts_or_args
|
|
122
122
|
out, err = capture_io_while do
|
|
123
|
-
simple_cmd.run(%w
|
|
123
|
+
simple_cmd.run(%w[])
|
|
124
124
|
end
|
|
125
125
|
|
|
126
126
|
assert_equal ['Awesome moo!', '', ''], lines(out)
|
|
@@ -129,7 +129,7 @@ module Cri
|
|
|
129
129
|
|
|
130
130
|
def test_invoke_simple_with_args
|
|
131
131
|
out, err = capture_io_while do
|
|
132
|
-
simple_cmd.run(%w
|
|
132
|
+
simple_cmd.run(%w[abc xyz])
|
|
133
133
|
end
|
|
134
134
|
|
|
135
135
|
assert_equal ['Awesome moo!', 'abc,xyz', ''], lines(out)
|
|
@@ -138,7 +138,7 @@ module Cri
|
|
|
138
138
|
|
|
139
139
|
def test_invoke_simple_with_opts
|
|
140
140
|
out, err = capture_io_while do
|
|
141
|
-
simple_cmd.run(%w
|
|
141
|
+
simple_cmd.run(%w[-c -b x])
|
|
142
142
|
end
|
|
143
143
|
|
|
144
144
|
assert_equal ['Awesome moo!', '', 'bbb=x,ccc=true'], lines(out)
|
|
@@ -148,7 +148,7 @@ module Cri
|
|
|
148
148
|
def test_invoke_simple_with_missing_opt_arg
|
|
149
149
|
out, err = capture_io_while do
|
|
150
150
|
err = assert_raises SystemExit do
|
|
151
|
-
simple_cmd.run(%w
|
|
151
|
+
simple_cmd.run(%w[-b])
|
|
152
152
|
end
|
|
153
153
|
assert_equal 1, err.status
|
|
154
154
|
end
|
|
@@ -159,7 +159,7 @@ module Cri
|
|
|
159
159
|
|
|
160
160
|
def test_invoke_simple_with_missing_opt_arg_no_exit
|
|
161
161
|
out, err = capture_io_while do
|
|
162
|
-
simple_cmd.run(%w
|
|
162
|
+
simple_cmd.run(%w[-b], {}, hard_exit: false)
|
|
163
163
|
end
|
|
164
164
|
|
|
165
165
|
assert_equal [], lines(out)
|
|
@@ -169,7 +169,7 @@ module Cri
|
|
|
169
169
|
def test_invoke_simple_with_illegal_opt
|
|
170
170
|
out, err = capture_io_while do
|
|
171
171
|
err = assert_raises SystemExit do
|
|
172
|
-
simple_cmd.run(%w
|
|
172
|
+
simple_cmd.run(%w[-z])
|
|
173
173
|
end
|
|
174
174
|
assert_equal 1, err.status
|
|
175
175
|
end
|
|
@@ -180,7 +180,7 @@ module Cri
|
|
|
180
180
|
|
|
181
181
|
def test_invoke_simple_with_illegal_opt_no_exit
|
|
182
182
|
out, err = capture_io_while do
|
|
183
|
-
simple_cmd.run(%w
|
|
183
|
+
simple_cmd.run(%w[-z], {}, hard_exit: false)
|
|
184
184
|
end
|
|
185
185
|
|
|
186
186
|
assert_equal [], lines(out)
|
|
@@ -189,7 +189,7 @@ module Cri
|
|
|
189
189
|
|
|
190
190
|
def test_invoke_simple_with_opt_with_block
|
|
191
191
|
out, err = capture_io_while do
|
|
192
|
-
simple_cmd.run(%w
|
|
192
|
+
simple_cmd.run(%w[-a 123])
|
|
193
193
|
end
|
|
194
194
|
|
|
195
195
|
assert_equal ['moo:123', 'Awesome moo!', '', 'aaa=123'], lines(out)
|
|
@@ -199,7 +199,7 @@ module Cri
|
|
|
199
199
|
def test_invoke_nested_without_opts_or_args
|
|
200
200
|
out, err = capture_io_while do
|
|
201
201
|
err = assert_raises SystemExit do
|
|
202
|
-
nested_cmd.run(%w
|
|
202
|
+
nested_cmd.run(%w[])
|
|
203
203
|
end
|
|
204
204
|
assert_equal 1, err.status
|
|
205
205
|
end
|
|
@@ -210,7 +210,7 @@ module Cri
|
|
|
210
210
|
|
|
211
211
|
def test_invoke_nested_without_opts_or_args_no_exit
|
|
212
212
|
out, err = capture_io_while do
|
|
213
|
-
nested_cmd.run(%w
|
|
213
|
+
nested_cmd.run(%w[], {}, hard_exit: false)
|
|
214
214
|
end
|
|
215
215
|
|
|
216
216
|
assert_equal [], lines(out)
|
|
@@ -219,7 +219,7 @@ module Cri
|
|
|
219
219
|
|
|
220
220
|
def test_invoke_nested_with_correct_command_name
|
|
221
221
|
out, err = capture_io_while do
|
|
222
|
-
nested_cmd.run(%w
|
|
222
|
+
nested_cmd.run(%w[sub])
|
|
223
223
|
end
|
|
224
224
|
|
|
225
225
|
assert_equal ['Sub-awesome!', '', ''], lines(out)
|
|
@@ -229,7 +229,7 @@ module Cri
|
|
|
229
229
|
def test_invoke_nested_with_incorrect_command_name
|
|
230
230
|
out, err = capture_io_while do
|
|
231
231
|
err = assert_raises SystemExit do
|
|
232
|
-
nested_cmd.run(%w
|
|
232
|
+
nested_cmd.run(%w[oogabooga])
|
|
233
233
|
end
|
|
234
234
|
assert_equal 1, err.status
|
|
235
235
|
end
|
|
@@ -240,7 +240,7 @@ module Cri
|
|
|
240
240
|
|
|
241
241
|
def test_invoke_nested_with_incorrect_command_name_no_exit
|
|
242
242
|
out, err = capture_io_while do
|
|
243
|
-
nested_cmd.run(%w
|
|
243
|
+
nested_cmd.run(%w[oogabooga], {}, hard_exit: false)
|
|
244
244
|
end
|
|
245
245
|
|
|
246
246
|
assert_equal [], lines(out)
|
|
@@ -250,7 +250,7 @@ module Cri
|
|
|
250
250
|
def test_invoke_nested_with_ambiguous_command_name
|
|
251
251
|
out, err = capture_io_while do
|
|
252
252
|
err = assert_raises SystemExit do
|
|
253
|
-
nested_cmd.run(%w
|
|
253
|
+
nested_cmd.run(%w[s])
|
|
254
254
|
end
|
|
255
255
|
assert_equal 1, err.status
|
|
256
256
|
end
|
|
@@ -261,7 +261,7 @@ module Cri
|
|
|
261
261
|
|
|
262
262
|
def test_invoke_nested_with_ambiguous_command_name_no_exit
|
|
263
263
|
out, err = capture_io_while do
|
|
264
|
-
nested_cmd.run(%w
|
|
264
|
+
nested_cmd.run(%w[s], {}, hard_exit: false)
|
|
265
265
|
end
|
|
266
266
|
|
|
267
267
|
assert_equal [], lines(out)
|
|
@@ -270,7 +270,7 @@ module Cri
|
|
|
270
270
|
|
|
271
271
|
def test_invoke_nested_with_alias
|
|
272
272
|
out, err = capture_io_while do
|
|
273
|
-
nested_cmd.run(%w
|
|
273
|
+
nested_cmd.run(%w[sup])
|
|
274
274
|
end
|
|
275
275
|
|
|
276
276
|
assert_equal ['Sub-awesome!', '', ''], lines(out)
|
|
@@ -279,7 +279,7 @@ module Cri
|
|
|
279
279
|
|
|
280
280
|
def test_invoke_nested_with_options_before_command
|
|
281
281
|
out, err = capture_io_while do
|
|
282
|
-
nested_cmd.run(%w
|
|
282
|
+
nested_cmd.run(%w[-a 666 sub])
|
|
283
283
|
end
|
|
284
284
|
|
|
285
285
|
assert_equal ['super:666', 'Sub-awesome!', '', 'aaa=666'], lines(out)
|
|
@@ -288,14 +288,14 @@ module Cri
|
|
|
288
288
|
|
|
289
289
|
def test_invoke_nested_with_run_block
|
|
290
290
|
out, err = capture_io_while do
|
|
291
|
-
nested_cmd_with_run_block.run(%w
|
|
291
|
+
nested_cmd_with_run_block.run(%w[])
|
|
292
292
|
end
|
|
293
293
|
|
|
294
294
|
assert_equal ['super'], lines(out)
|
|
295
295
|
assert_equal [], lines(err)
|
|
296
296
|
|
|
297
297
|
out, err = capture_io_while do
|
|
298
|
-
nested_cmd_with_run_block.run(%w
|
|
298
|
+
nested_cmd_with_run_block.run(%w[sub])
|
|
299
299
|
end
|
|
300
300
|
|
|
301
301
|
assert_equal ['sub'], lines(out)
|
|
@@ -573,7 +573,7 @@ module Cri
|
|
|
573
573
|
end
|
|
574
574
|
|
|
575
575
|
out, _err = capture_io_while do
|
|
576
|
-
cmd.run(%w
|
|
576
|
+
cmd.run(%w[foo -- bar])
|
|
577
577
|
end
|
|
578
578
|
assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
|
|
579
579
|
end
|
|
@@ -599,7 +599,7 @@ module Cri
|
|
|
599
599
|
end
|
|
600
600
|
|
|
601
601
|
out, _err = capture_io_while do
|
|
602
|
-
cmd.run(%w
|
|
602
|
+
cmd.run(%w[foo -- bar])
|
|
603
603
|
end
|
|
604
604
|
assert_equal "args=foo,bar args.raw=foo,--,bar\n", out
|
|
605
605
|
end
|
data/test/test_command_dsl.rb
CHANGED
|
@@ -26,7 +26,7 @@ module Cri
|
|
|
26
26
|
|
|
27
27
|
# Run
|
|
28
28
|
$did_it_work = :sadly_not
|
|
29
|
-
command.run(%w
|
|
29
|
+
command.run(%w[-a x -b y -c -d -e])
|
|
30
30
|
assert_equal :probably, $did_it_work
|
|
31
31
|
|
|
32
32
|
# Check
|
|
@@ -36,14 +36,17 @@ module Cri
|
|
|
36
36
|
assert_equal 'This command does a lot of stuff.', command.description
|
|
37
37
|
|
|
38
38
|
# Check options
|
|
39
|
-
expected_option_definitions =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
expected_option_definitions =
|
|
40
|
+
Set.new(
|
|
41
|
+
[
|
|
42
|
+
{ short: 'a', long: 'aaa', desc: 'opt a', argument: :optional, multiple: true, hidden: false, block: nil, default: nil },
|
|
43
|
+
{ short: 'b', long: 'bbb', desc: 'opt b', argument: :required, multiple: false, hidden: false, block: nil, default: nil },
|
|
44
|
+
{ short: 'c', long: 'ccc', desc: 'opt c', argument: :optional, multiple: false, hidden: false, block: nil, default: nil },
|
|
45
|
+
{ short: 'd', long: 'ddd', desc: 'opt d', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
|
|
46
|
+
{ short: 'e', long: 'eee', desc: 'opt e', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
|
|
47
|
+
{ short: 'f', long: 'fff', desc: 'opt f', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil },
|
|
48
|
+
],
|
|
49
|
+
)
|
|
47
50
|
actual_option_definitions = Set.new(command.option_definitions)
|
|
48
51
|
assert_equal expected_option_definitions, actual_option_definitions
|
|
49
52
|
end
|
|
@@ -68,14 +71,17 @@ module Cri
|
|
|
68
71
|
|
|
69
72
|
# Run
|
|
70
73
|
$did_it_work = :sadly_not
|
|
71
|
-
command.run(%w
|
|
74
|
+
command.run(%w[-s --long])
|
|
72
75
|
assert_equal :probably, $did_it_work
|
|
73
76
|
|
|
74
77
|
# Check options
|
|
75
|
-
expected_option_definitions =
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
78
|
+
expected_option_definitions =
|
|
79
|
+
Set.new(
|
|
80
|
+
[
|
|
81
|
+
{ short: 's', long: nil, desc: 'short', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
|
|
82
|
+
{ short: nil, long: 'long', desc: 'long', argument: :forbidden, multiple: false, hidden: false, block: nil, default: nil },
|
|
83
|
+
],
|
|
84
|
+
)
|
|
79
85
|
actual_option_definitions = Set.new(command.option_definitions)
|
|
80
86
|
assert_equal expected_option_definitions, actual_option_definitions
|
|
81
87
|
end
|
|
@@ -93,11 +99,14 @@ module Cri
|
|
|
93
99
|
command = dsl.command
|
|
94
100
|
|
|
95
101
|
# Check options
|
|
96
|
-
expected_option_definitions =
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
102
|
+
expected_option_definitions =
|
|
103
|
+
Set.new(
|
|
104
|
+
[
|
|
105
|
+
{ short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: true, hidden: false, block: nil, default: nil },
|
|
106
|
+
{ short: 'r', long: 'required', desc: 'req', argument: :required, multiple: true, hidden: false, block: nil, default: nil },
|
|
107
|
+
{ short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: true, hidden: false, block: nil, default: nil },
|
|
108
|
+
],
|
|
109
|
+
)
|
|
101
110
|
actual_option_definitions = Set.new(command.option_definitions)
|
|
102
111
|
assert_equal expected_option_definitions, actual_option_definitions
|
|
103
112
|
end
|
|
@@ -115,11 +124,14 @@ module Cri
|
|
|
115
124
|
command = dsl.command
|
|
116
125
|
|
|
117
126
|
# Check options
|
|
118
|
-
expected_option_definitions =
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
127
|
+
expected_option_definitions =
|
|
128
|
+
Set.new(
|
|
129
|
+
[
|
|
130
|
+
{ short: 'f', long: 'flag', desc: 'flag', argument: :forbidden, multiple: false, hidden: true, block: nil, default: nil },
|
|
131
|
+
{ short: 'r', long: 'required', desc: 'req', argument: :required, multiple: false, hidden: true, block: nil, default: nil },
|
|
132
|
+
{ short: 'o', long: 'optional', desc: 'opt', argument: :optional, multiple: false, hidden: true, block: nil, default: nil },
|
|
133
|
+
],
|
|
134
|
+
)
|
|
123
135
|
actual_option_definitions = Set.new(command.option_definitions)
|
|
124
136
|
assert_equal expected_option_definitions, actual_option_definitions
|
|
125
137
|
end
|
|
@@ -149,6 +161,28 @@ module Cri
|
|
|
149
161
|
end
|
|
150
162
|
end
|
|
151
163
|
|
|
164
|
+
def test_default_value_errors_when_requiredness_is_required
|
|
165
|
+
dsl = Cri::CommandDSL.new
|
|
166
|
+
|
|
167
|
+
err = assert_raises ArgumentError do
|
|
168
|
+
dsl.instance_eval do
|
|
169
|
+
required 'a', 'animal', 'Specify animal', default: 'giraffe'
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
assert_equal('a default value cannot be specified for options with required values', err.message)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def test_default_value_errors_when_requiredness_is_forbidden
|
|
176
|
+
dsl = Cri::CommandDSL.new
|
|
177
|
+
|
|
178
|
+
err = assert_raises ArgumentError do
|
|
179
|
+
dsl.instance_eval do
|
|
180
|
+
flag 'a', 'animal', 'Allow animal', default: 'giraffe'
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
assert_equal('a default value cannot be specified for options with forbidden values', err.message)
|
|
184
|
+
end
|
|
185
|
+
|
|
152
186
|
def test_subcommand
|
|
153
187
|
# Define
|
|
154
188
|
dsl = Cri::CommandDSL.new
|
|
@@ -175,7 +209,7 @@ module Cri
|
|
|
175
209
|
command = dsl.command
|
|
176
210
|
|
|
177
211
|
# Check
|
|
178
|
-
assert_equal %w
|
|
212
|
+
assert_equal %w[aah moo], command.aliases.sort
|
|
179
213
|
end
|
|
180
214
|
|
|
181
215
|
def test_run_arity
|
|
@@ -204,7 +238,7 @@ module Cri
|
|
|
204
238
|
|
|
205
239
|
# Check
|
|
206
240
|
$did_it_work = false
|
|
207
|
-
command.run(%w
|
|
241
|
+
command.run(%w[certainly])
|
|
208
242
|
assert_equal 'certainly', $did_it_work
|
|
209
243
|
end
|
|
210
244
|
end
|
data/test/test_command_runner.rb
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
|
|
3
|
+
module Cri
|
|
4
|
+
class HelpRendererTestCase < Cri::TestCase
|
|
5
|
+
# NOTE: Additional test cases are in test_command.rb
|
|
6
|
+
|
|
7
|
+
def help_for(cmd)
|
|
8
|
+
io = StringIO.new
|
|
9
|
+
Cri::HelpRenderer.new(cmd, io: io).render
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_simple
|
|
13
|
+
expected = <<EOS
|
|
14
|
+
NAME
|
|
15
|
+
help - show help
|
|
16
|
+
|
|
17
|
+
USAGE
|
|
18
|
+
help [command_name]
|
|
19
|
+
|
|
20
|
+
DESCRIPTION
|
|
21
|
+
Show help for the given command, or show general help. When no command is
|
|
22
|
+
given, a list of available commands is displayed, as well as a list of
|
|
23
|
+
global command-line options. When a command is given, a command
|
|
24
|
+
description, as well as command-specific command-line options, are shown.
|
|
25
|
+
|
|
26
|
+
OPTIONS
|
|
27
|
+
-v --verbose show more detailed help
|
|
28
|
+
EOS
|
|
29
|
+
|
|
30
|
+
cmd = Cri::Command.new_basic_help
|
|
31
|
+
assert_equal(expected, help_for(cmd))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def test_with_defaults
|
|
35
|
+
cmd = Cri::Command.define do
|
|
36
|
+
name 'build'
|
|
37
|
+
optional nil, :'with-animal', 'Add animal', default: 'giraffe'
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
help = help_for(cmd)
|
|
41
|
+
|
|
42
|
+
assert_match(/^ --with-animal\[=<value>\] Add animal \(default: giraffe\)$/, help)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
data/test/test_option_parser.rb
CHANGED
|
@@ -3,17 +3,17 @@ require 'helper'
|
|
|
3
3
|
module Cri
|
|
4
4
|
class OptionParserTestCase < Cri::TestCase
|
|
5
5
|
def test_parse_without_options
|
|
6
|
-
input = %w
|
|
6
|
+
input = %w[foo bar baz]
|
|
7
7
|
definitions = []
|
|
8
8
|
|
|
9
9
|
parser = Cri::OptionParser.parse(input, definitions)
|
|
10
10
|
|
|
11
11
|
assert_equal({}, parser.options)
|
|
12
|
-
assert_equal(%w
|
|
12
|
+
assert_equal(%w[foo bar baz], parser.arguments)
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def test_parse_with_invalid_option
|
|
16
|
-
input = %w
|
|
16
|
+
input = %w[foo -x]
|
|
17
17
|
definitions = []
|
|
18
18
|
|
|
19
19
|
assert_raises(Cri::OptionParser::IllegalOptionError) do
|
|
@@ -22,7 +22,7 @@ module Cri
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def test_parse_with_unused_options
|
|
25
|
-
input = %w
|
|
25
|
+
input = %w[foo]
|
|
26
26
|
definitions = [
|
|
27
27
|
{ long: 'aaa', short: 'a', argument: :forbidden },
|
|
28
28
|
]
|
|
@@ -33,7 +33,7 @@ module Cri
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def test_parse_with_long_valueless_option
|
|
36
|
-
input = %w
|
|
36
|
+
input = %w[foo --aaa bar]
|
|
37
37
|
definitions = [
|
|
38
38
|
{ long: 'aaa', short: 'a', argument: :forbidden },
|
|
39
39
|
]
|
|
@@ -41,11 +41,11 @@ module Cri
|
|
|
41
41
|
parser = Cri::OptionParser.parse(input, definitions)
|
|
42
42
|
|
|
43
43
|
assert(parser.options[:aaa])
|
|
44
|
-
assert_equal(%w
|
|
44
|
+
assert_equal(%w[foo bar], parser.arguments)
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
def test_parse_with_long_valueful_option
|
|
48
|
-
input = %w
|
|
48
|
+
input = %w[foo --aaa xxx bar]
|
|
49
49
|
definitions = [
|
|
50
50
|
{ long: 'aaa', short: 'a', argument: :required },
|
|
51
51
|
]
|
|
@@ -53,11 +53,11 @@ module Cri
|
|
|
53
53
|
parser = Cri::OptionParser.parse(input, definitions)
|
|
54
54
|
|
|
55
55
|
assert_equal({ aaa: 'xxx' }, parser.options)
|
|
56
|
-
assert_equal(%w
|
|
56
|
+
assert_equal(%w[foo bar], parser.arguments)
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def test_parse_with_long_valueful_equalsign_option
|
|
60
|
-
input = %w
|
|
60
|
+
input = %w[foo --aaa=xxx bar]
|
|
61
61
|
definitions = [
|
|
62
62
|
{ long: 'aaa', short: 'a', argument: :required },
|
|
63
63
|
]
|
|
@@ -65,11 +65,11 @@ module Cri
|
|
|
65
65
|
parser = Cri::OptionParser.parse(input, definitions)
|
|
66
66
|
|
|
67
67
|
assert_equal({ aaa: 'xxx' }, parser.options)
|
|
68
|
-
assert_equal(%w
|
|
68
|
+
assert_equal(%w[foo bar], parser.arguments)
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
def test_parse_with_long_valueful_option_with_missing_value
|
|
72
|
-
input = %w
|
|
72
|
+
input = %w[foo --aaa]
|
|
73
73
|
definitions = [
|
|
74
74
|
{ long: 'aaa', short: 'a', argument: :required },
|
|
75
75
|
]
|
|
@@ -80,7 +80,7 @@ module Cri
|
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
def test_parse_with_two_long_valueful_options
|
|
83
|
-
input = %w
|
|
83
|
+
input = %w[foo --all --port 2]
|
|
84
84
|
definitions = [
|
|
85
85
|
{ long: 'all', short: 'a', argument: :required },
|
|
86
86
|
{ long: 'port', short: 'p', argument: :required },
|
|
@@ -92,7 +92,7 @@ module Cri
|
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
def test_parse_with_long_valueless_option_with_optional_value
|
|
95
|
-
input = %w
|
|
95
|
+
input = %w[foo --aaa]
|
|
96
96
|
definitions = [
|
|
97
97
|
{ long: 'aaa', short: 'a', argument: :optional },
|
|
98
98
|
]
|
|
@@ -104,7 +104,7 @@ module Cri
|
|
|
104
104
|
end
|
|
105
105
|
|
|
106
106
|
def test_parse_with_long_valueful_option_with_optional_value
|
|
107
|
-
input = %w
|
|
107
|
+
input = %w[foo --aaa xxx]
|
|
108
108
|
definitions = [
|
|
109
109
|
{ long: 'aaa', short: 'a', argument: :optional },
|
|
110
110
|
]
|
|
@@ -116,7 +116,7 @@ module Cri
|
|
|
116
116
|
end
|
|
117
117
|
|
|
118
118
|
def test_parse_with_long_valueless_option_with_optional_value_and_more_options
|
|
119
|
-
input = %w
|
|
119
|
+
input = %w[foo --aaa -b -c]
|
|
120
120
|
definitions = [
|
|
121
121
|
{ long: 'aaa', short: 'a', argument: :optional },
|
|
122
122
|
{ long: 'bbb', short: 'b', argument: :forbidden },
|
|
@@ -132,7 +132,7 @@ module Cri
|
|
|
132
132
|
end
|
|
133
133
|
|
|
134
134
|
def test_parse_with_short_valueless_options
|
|
135
|
-
input = %w
|
|
135
|
+
input = %w[foo -a bar]
|
|
136
136
|
definitions = [
|
|
137
137
|
{ long: 'aaa', short: 'a', argument: :forbidden },
|
|
138
138
|
]
|
|
@@ -140,11 +140,11 @@ module Cri
|
|
|
140
140
|
parser = Cri::OptionParser.parse(input, definitions)
|
|
141
141
|
|
|
142
142
|
assert(parser.options[:aaa])
|
|
143
|
-
assert_equal(%w
|
|
143
|
+
assert_equal(%w[foo bar], parser.arguments)
|
|
144
144
|
end
|
|
145
145
|
|
|
146
146
|
def test_parse_with_short_valueful_option_with_missing_value
|
|
147
|
-
input = %w
|
|
147
|
+
input = %w[foo -a]
|
|
148
148
|
definitions = [
|
|
149
149
|
{ long: 'aaa', short: 'a', argument: :required },
|
|
150
150
|
]
|
|
@@ -155,7 +155,7 @@ module Cri
|
|
|
155
155
|
end
|
|
156
156
|
|
|
157
157
|
def test_parse_with_short_combined_valueless_options
|
|
158
|
-
input = %w
|
|
158
|
+
input = %w[foo -abc bar]
|
|
159
159
|
definitions = [
|
|
160
160
|
{ long: 'aaa', short: 'a', argument: :forbidden },
|
|
161
161
|
{ long: 'bbb', short: 'b', argument: :forbidden },
|
|
@@ -167,24 +167,27 @@ module Cri
|
|
|
167
167
|
assert(parser.options[:aaa])
|
|
168
168
|
assert(parser.options[:bbb])
|
|
169
169
|
assert(parser.options[:ccc])
|
|
170
|
-
assert_equal(%w
|
|
170
|
+
assert_equal(%w[foo bar], parser.arguments)
|
|
171
171
|
end
|
|
172
172
|
|
|
173
173
|
def test_parse_with_short_combined_valueful_options_with_missing_value
|
|
174
|
-
input = %w
|
|
174
|
+
input = %w[foo -abc bar qux]
|
|
175
175
|
definitions = [
|
|
176
176
|
{ long: 'aaa', short: 'a', argument: :required },
|
|
177
177
|
{ long: 'bbb', short: 'b', argument: :forbidden },
|
|
178
178
|
{ long: 'ccc', short: 'c', argument: :forbidden },
|
|
179
179
|
]
|
|
180
180
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
181
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
|
182
|
+
|
|
183
|
+
assert_equal('bar', parser.options[:aaa])
|
|
184
|
+
assert(parser.options[:bbb])
|
|
185
|
+
assert(parser.options[:ccc])
|
|
186
|
+
assert_equal(%w[foo qux], parser.arguments)
|
|
184
187
|
end
|
|
185
188
|
|
|
186
189
|
def test_parse_with_two_short_valueful_options
|
|
187
|
-
input = %w
|
|
190
|
+
input = %w[foo -a -p 2]
|
|
188
191
|
definitions = [
|
|
189
192
|
{ long: 'all', short: 'a', argument: :required },
|
|
190
193
|
{ long: 'port', short: 'p', argument: :required },
|
|
@@ -196,7 +199,7 @@ module Cri
|
|
|
196
199
|
end
|
|
197
200
|
|
|
198
201
|
def test_parse_with_short_valueless_option_with_optional_value
|
|
199
|
-
input = %w
|
|
202
|
+
input = %w[foo -a]
|
|
200
203
|
definitions = [
|
|
201
204
|
{ long: 'aaa', short: 'a', argument: :optional },
|
|
202
205
|
]
|
|
@@ -208,7 +211,7 @@ module Cri
|
|
|
208
211
|
end
|
|
209
212
|
|
|
210
213
|
def test_parse_with_short_valueful_option_with_optional_value
|
|
211
|
-
input = %w
|
|
214
|
+
input = %w[foo -a xxx]
|
|
212
215
|
definitions = [
|
|
213
216
|
{ long: 'aaa', short: 'a', argument: :optional },
|
|
214
217
|
]
|
|
@@ -220,7 +223,7 @@ module Cri
|
|
|
220
223
|
end
|
|
221
224
|
|
|
222
225
|
def test_parse_with_short_valueless_option_with_optional_value_and_more_options
|
|
223
|
-
input = %w
|
|
226
|
+
input = %w[foo -a -b -c]
|
|
224
227
|
definitions = [
|
|
225
228
|
{ long: 'aaa', short: 'a', argument: :optional },
|
|
226
229
|
{ long: 'bbb', short: 'b', argument: :forbidden },
|
|
@@ -236,7 +239,7 @@ module Cri
|
|
|
236
239
|
end
|
|
237
240
|
|
|
238
241
|
def test_parse_with_single_hyphen
|
|
239
|
-
input = %w
|
|
242
|
+
input = %w[foo - bar]
|
|
240
243
|
definitions = []
|
|
241
244
|
|
|
242
245
|
parser = Cri::OptionParser.parse(input, definitions)
|
|
@@ -246,7 +249,7 @@ module Cri
|
|
|
246
249
|
end
|
|
247
250
|
|
|
248
251
|
def test_parse_with_end_marker
|
|
249
|
-
input = %w
|
|
252
|
+
input = %w[foo bar -- -x --yyy -abc]
|
|
250
253
|
definitions = []
|
|
251
254
|
|
|
252
255
|
parser = Cri::OptionParser.parse(input, definitions)
|
|
@@ -257,7 +260,7 @@ module Cri
|
|
|
257
260
|
end
|
|
258
261
|
|
|
259
262
|
def test_parse_with_end_marker_between_option_key_and_value
|
|
260
|
-
input = %w
|
|
263
|
+
input = %w[foo --aaa -- zzz]
|
|
261
264
|
definitions = [
|
|
262
265
|
{ long: 'aaa', short: 'a', argument: :required },
|
|
263
266
|
]
|
|
@@ -268,15 +271,116 @@ module Cri
|
|
|
268
271
|
end
|
|
269
272
|
|
|
270
273
|
def test_parse_with_multiple_options
|
|
271
|
-
input = %w
|
|
274
|
+
input = %w[foo -o test -o test2 -v -v -v]
|
|
272
275
|
definitions = [
|
|
273
276
|
{ long: 'long', short: 'o', argument: :required, multiple: true },
|
|
274
277
|
{ long: 'verbose', short: 'v', multiple: true },
|
|
275
278
|
]
|
|
276
279
|
parser = Cri::OptionParser.parse(input, definitions)
|
|
277
280
|
|
|
278
|
-
assert_equal(%w
|
|
281
|
+
assert_equal(%w[test test2], parser.options[:long])
|
|
279
282
|
assert_equal(3, parser.options[:verbose].size)
|
|
280
283
|
end
|
|
284
|
+
|
|
285
|
+
def test_parse_with_default_required_no_value
|
|
286
|
+
input = %w[foo -a]
|
|
287
|
+
definitions = [
|
|
288
|
+
{ long: 'animal', short: 'a', argument: :required, default: 'donkey' },
|
|
289
|
+
]
|
|
290
|
+
|
|
291
|
+
assert_raises(Cri::OptionParser::OptionRequiresAnArgumentError) do
|
|
292
|
+
Cri::OptionParser.parse(input, definitions)
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def test_parse_with_default_required_value
|
|
297
|
+
input = %w[foo -a giraffe]
|
|
298
|
+
definitions = [
|
|
299
|
+
{ long: 'animal', short: 'a', argument: :required, default: 'donkey' },
|
|
300
|
+
]
|
|
301
|
+
|
|
302
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
|
303
|
+
|
|
304
|
+
assert_equal({ animal: 'giraffe' }, parser.options)
|
|
305
|
+
assert_equal(['foo'], parser.arguments)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
def test_parse_with_default_optional_no_value
|
|
309
|
+
input = %w[foo -a]
|
|
310
|
+
definitions = [
|
|
311
|
+
{ long: 'animal', short: 'a', argument: :optional, default: 'donkey' },
|
|
312
|
+
]
|
|
313
|
+
|
|
314
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
|
315
|
+
|
|
316
|
+
assert_equal({ animal: 'donkey' }, parser.options)
|
|
317
|
+
assert_equal(['foo'], parser.arguments)
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
def test_parse_with_default_optional_value
|
|
321
|
+
input = %w[foo -a giraffe]
|
|
322
|
+
definitions = [
|
|
323
|
+
{ long: 'animal', short: 'a', argument: :optional, default: 'donkey' },
|
|
324
|
+
]
|
|
325
|
+
|
|
326
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
|
327
|
+
|
|
328
|
+
assert_equal({ animal: 'giraffe' }, parser.options)
|
|
329
|
+
assert_equal(['foo'], parser.arguments)
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
def test_parse_with_default_optional_value_and_arg
|
|
333
|
+
input = %w[foo -a gi raffe]
|
|
334
|
+
definitions = [
|
|
335
|
+
{ long: 'animal', short: 'a', argument: :optional, default: 'donkey' },
|
|
336
|
+
]
|
|
337
|
+
|
|
338
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
|
339
|
+
|
|
340
|
+
assert_equal({ animal: 'gi' }, parser.options)
|
|
341
|
+
assert_equal(%w[foo raffe], parser.arguments)
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def test_parse_with_combined_required_options
|
|
345
|
+
input = %w[foo -abc xxx yyy zzz]
|
|
346
|
+
definitions = [
|
|
347
|
+
{ long: 'aaa', short: 'a', argument: :forbidden },
|
|
348
|
+
{ long: 'bbb', short: 'b', argument: :required },
|
|
349
|
+
{ long: 'ccc', short: 'c', argument: :required },
|
|
350
|
+
]
|
|
351
|
+
|
|
352
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
|
353
|
+
|
|
354
|
+
assert_equal({ aaa: true, bbb: 'xxx', ccc: 'yyy' }, parser.options)
|
|
355
|
+
assert_equal(%w[foo zzz], parser.arguments)
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def test_parse_with_combined_optional_options
|
|
359
|
+
input = %w[foo -abc xxx yyy zzz]
|
|
360
|
+
definitions = [
|
|
361
|
+
{ long: 'aaa', short: 'a', argument: :forbidden },
|
|
362
|
+
{ long: 'bbb', short: 'b', argument: :optional },
|
|
363
|
+
{ long: 'ccc', short: 'c', argument: :required },
|
|
364
|
+
]
|
|
365
|
+
|
|
366
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
|
367
|
+
|
|
368
|
+
assert_equal({ aaa: true, bbb: 'xxx', ccc: 'yyy' }, parser.options)
|
|
369
|
+
assert_equal(%w[foo zzz], parser.arguments)
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
def test_parse_with_combined_optional_options_with_missing_value
|
|
373
|
+
input = %w[foo -abc xxx]
|
|
374
|
+
definitions = [
|
|
375
|
+
{ long: 'aaa', short: 'a', argument: :forbidden },
|
|
376
|
+
{ long: 'bbb', short: 'b', argument: :required },
|
|
377
|
+
{ long: 'ccc', short: 'c', argument: :optional, default: 'c default' },
|
|
378
|
+
]
|
|
379
|
+
|
|
380
|
+
parser = Cri::OptionParser.parse(input, definitions)
|
|
381
|
+
|
|
382
|
+
assert_equal({ aaa: true, bbb: 'xxx', ccc: 'c default' }, parser.options)
|
|
383
|
+
assert_equal(%w[foo], parser.arguments)
|
|
384
|
+
end
|
|
281
385
|
end
|
|
282
386
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cri
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.9.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Denis Defreyne
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-
|
|
11
|
+
date: 2017-06-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: colored
|
|
@@ -77,6 +77,7 @@ files:
|
|
|
77
77
|
- test/test_command.rb
|
|
78
78
|
- test/test_command_dsl.rb
|
|
79
79
|
- test/test_command_runner.rb
|
|
80
|
+
- test/test_help_renderer.rb
|
|
80
81
|
- test/test_option_parser.rb
|
|
81
82
|
- test/test_string_formatter.rb
|
|
82
83
|
homepage: http://stoneship.org/software/cri/
|
|
@@ -101,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
101
102
|
version: '0'
|
|
102
103
|
requirements: []
|
|
103
104
|
rubyforge_project:
|
|
104
|
-
rubygems_version: 2.6.
|
|
105
|
+
rubygems_version: 2.6.12
|
|
105
106
|
signing_key:
|
|
106
107
|
specification_version: 4
|
|
107
108
|
summary: a library for building easy-to-use command-line tools
|