clamp 1.3.2 → 1.3.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.
- checksums.yaml +4 -4
- data/.editorconfig +1 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +12 -8
- data/.travis.yml +2 -1
- data/CHANGES.md +4 -0
- data/CODEOWNERS +1 -0
- data/Gemfile +8 -5
- data/Guardfile +2 -2
- data/Rakefile +1 -3
- data/clamp.gemspec +2 -1
- data/lib/clamp/attribute/declaration.rb +1 -0
- data/lib/clamp/attribute/definition.rb +4 -2
- data/lib/clamp/attribute/instance.rb +2 -1
- data/lib/clamp/help.rb +4 -5
- data/lib/clamp/messages.rb +3 -3
- data/lib/clamp/option/declaration.rb +2 -0
- data/lib/clamp/option/definition.rb +10 -5
- data/lib/clamp/option/parsing.rb +8 -3
- data/lib/clamp/parameter/declaration.rb +3 -0
- data/lib/clamp/parameter/definition.rb +8 -5
- data/lib/clamp/parameter/parsing.rb +4 -6
- data/lib/clamp/subcommand/declaration.rb +3 -1
- data/lib/clamp/subcommand/execution.rb +2 -0
- data/lib/clamp/subcommand/parsing.rb +1 -0
- data/lib/clamp/truthy.rb +1 -1
- data/lib/clamp/version.rb +1 -1
- data/spec/clamp/command_group_spec.rb +64 -45
- data/spec/clamp/{option_module_spec.rb → command_option_module_spec.rb} +2 -1
- data/spec/clamp/{option_reordering_spec.rb → command_option_reordering_spec.rb} +3 -3
- data/spec/clamp/command_spec.rb +364 -148
- data/spec/clamp/{help_spec.rb → help/builder_spec.rb} +22 -4
- data/spec/clamp/messages_spec.rb +11 -6
- data/spec/clamp/option/definition_spec.rb +92 -40
- data/spec/clamp/parameter/definition_spec.rb +120 -50
- data/spec/spec_helper.rb +5 -6
- metadata +14 -21
@@ -30,27 +30,41 @@ describe Clamp::Help::Builder do
|
|
30
30
|
|
31
31
|
context "with multiple rows" do
|
32
32
|
|
33
|
-
|
33
|
+
before do
|
34
|
+
|
34
35
|
builder.row("foo", "bar")
|
35
36
|
builder.row("flibble", "blurk")
|
36
37
|
builder.row("x", "y")
|
37
|
-
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
let(:expected_output) do
|
42
|
+
[
|
38
43
|
" foo bar\n",
|
39
44
|
" flibble blurk\n",
|
40
45
|
" x y\n"
|
41
46
|
]
|
42
47
|
end
|
43
48
|
|
49
|
+
it "arranges them in two columns" do
|
50
|
+
expect(output.lines).to eq expected_output
|
51
|
+
end
|
52
|
+
|
44
53
|
end
|
45
54
|
|
46
55
|
context "with a mixture of lines and rows" do
|
47
56
|
|
48
|
-
|
57
|
+
before do
|
58
|
+
|
49
59
|
builder.line("ABCDEFGHIJKLMNOP")
|
50
60
|
builder.row("flibble", "blurk")
|
51
61
|
builder.line("Another section heading")
|
52
62
|
builder.row("x", "y")
|
53
|
-
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
let(:expected_output) do
|
67
|
+
[
|
54
68
|
"ABCDEFGHIJKLMNOP\n",
|
55
69
|
" flibble blurk\n",
|
56
70
|
"Another section heading\n",
|
@@ -58,6 +72,10 @@ describe Clamp::Help::Builder do
|
|
58
72
|
]
|
59
73
|
end
|
60
74
|
|
75
|
+
it "still arranges them in two columns" do
|
76
|
+
expect(output.lines).to eq expected_output
|
77
|
+
end
|
78
|
+
|
61
79
|
end
|
62
80
|
|
63
81
|
end
|
data/spec/clamp/messages_spec.rb
CHANGED
@@ -17,28 +17,33 @@ describe Clamp::Messages do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it "allows setting custom messages" do
|
20
|
-
expect(Clamp.message(:too_many_arguments)).to
|
20
|
+
expect(Clamp.message(:too_many_arguments)).to eq "Way too many!"
|
21
21
|
end
|
22
22
|
|
23
23
|
it "fallbacks to a default message" do
|
24
|
-
expect(Clamp.message(:no_value_provided)).to
|
24
|
+
expect(Clamp.message(:no_value_provided)).to eq "no value provided"
|
25
25
|
end
|
26
26
|
|
27
27
|
it "formats the message" do
|
28
|
-
expect(Clamp.message(:custom_message, what: "hello", whom: "Clamp")).to
|
28
|
+
expect(Clamp.message(:custom_message, what: "hello", whom: "Clamp")).to eq "Say hello to Clamp"
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
describe "clear_messages!" do
|
33
|
-
|
34
|
-
|
33
|
+
let!(:default_msg) { Clamp.message(:too_many_arguments).clone }
|
34
|
+
|
35
|
+
before do
|
35
36
|
|
36
37
|
Clamp.messages = {
|
37
38
|
too_many_arguments: "Way too many!"
|
38
39
|
}
|
40
|
+
|
39
41
|
Clamp.clear_messages!
|
40
42
|
|
41
|
-
|
43
|
+
end
|
44
|
+
|
45
|
+
it "clears messages to the defualt state" do
|
46
|
+
expect(Clamp.message(:too_many_arguments)).to eq default_msg
|
42
47
|
end
|
43
48
|
end
|
44
49
|
|
@@ -11,26 +11,26 @@ describe Clamp::Option::Definition do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "has a long_switch" do
|
14
|
-
expect(option.long_switch).to
|
14
|
+
expect(option.long_switch).to eq "--key-file"
|
15
15
|
end
|
16
16
|
|
17
17
|
it "has a type" do
|
18
|
-
expect(option.type).to
|
18
|
+
expect(option.type).to eq "FILE"
|
19
19
|
end
|
20
20
|
|
21
21
|
it "has a description" do
|
22
|
-
expect(option.description).to
|
22
|
+
expect(option.description).to eq "SSH identity"
|
23
23
|
end
|
24
24
|
|
25
25
|
describe "#attribute_name" do
|
26
26
|
|
27
27
|
it "is derived from the (long) switch" do
|
28
|
-
expect(option.attribute_name).to
|
28
|
+
expect(option.attribute_name).to eq "key_file"
|
29
29
|
end
|
30
30
|
|
31
31
|
it "can be overridden" do
|
32
32
|
option = described_class.new("--key-file", "FILE", "SSH identity", attribute_name: "ssh_identity")
|
33
|
-
expect(option.attribute_name).to
|
33
|
+
expect(option.attribute_name).to eq "ssh_identity"
|
34
34
|
end
|
35
35
|
|
36
36
|
end
|
@@ -38,7 +38,7 @@ describe Clamp::Option::Definition do
|
|
38
38
|
describe "#write_method" do
|
39
39
|
|
40
40
|
it "is derived from the attribute_name" do
|
41
|
-
expect(option.write_method).to
|
41
|
+
expect(option.write_method).to eq "key_file="
|
42
42
|
end
|
43
43
|
|
44
44
|
end
|
@@ -47,12 +47,12 @@ describe Clamp::Option::Definition do
|
|
47
47
|
|
48
48
|
it "defaults to nil" do
|
49
49
|
option = described_class.new("-n", "N", "iterations")
|
50
|
-
expect(option.default_value).to
|
50
|
+
expect(option.default_value).to be_nil
|
51
51
|
end
|
52
52
|
|
53
53
|
it "can be overridden" do
|
54
54
|
option = described_class.new("-n", "N", "iterations", default: 1)
|
55
|
-
expect(option.default_value).to
|
55
|
+
expect(option.default_value).to eq 1
|
56
56
|
end
|
57
57
|
|
58
58
|
end
|
@@ -60,14 +60,14 @@ describe Clamp::Option::Definition do
|
|
60
60
|
describe "#help" do
|
61
61
|
|
62
62
|
it "combines switch, type and description" do
|
63
|
-
expect(option.help).to
|
63
|
+
expect(option.help).to eq ["--key-file FILE", "SSH identity"]
|
64
64
|
end
|
65
65
|
|
66
66
|
end
|
67
67
|
|
68
68
|
end
|
69
69
|
|
70
|
-
context "flag" do
|
70
|
+
context "when flag" do
|
71
71
|
|
72
72
|
let(:option) do
|
73
73
|
described_class.new("--verbose", :flag, "Blah blah blah")
|
@@ -75,14 +75,28 @@ describe Clamp::Option::Definition do
|
|
75
75
|
|
76
76
|
describe "#default_conversion_block" do
|
77
77
|
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
context "with 'true' value" do
|
79
|
+
it "converts to true" do
|
80
|
+
expect(option.default_conversion_block.call("true")).to be true
|
81
|
+
end
|
81
82
|
end
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
84
|
+
context "with 'yes' value" do
|
85
|
+
it "converts to true" do
|
86
|
+
expect(option.default_conversion_block.call("yes")).to be true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context "with 'false' value" do
|
91
|
+
it "converts to false" do
|
92
|
+
expect(option.default_conversion_block.call("false")).to be false
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "with 'no' value" do
|
97
|
+
it "converts to false" do
|
98
|
+
expect(option.default_conversion_block.call("no")).to be false
|
99
|
+
end
|
86
100
|
end
|
87
101
|
|
88
102
|
end
|
@@ -90,29 +104,43 @@ describe Clamp::Option::Definition do
|
|
90
104
|
describe "#help" do
|
91
105
|
|
92
106
|
it "excludes option argument" do
|
93
|
-
expect(option.help).to
|
107
|
+
expect(option.help).to eq ["--verbose", "Blah blah blah"]
|
94
108
|
end
|
95
109
|
|
96
110
|
end
|
97
111
|
|
98
112
|
end
|
99
113
|
|
100
|
-
context "negatable flag" do
|
114
|
+
context "when negatable flag" do
|
101
115
|
|
102
116
|
let(:option) do
|
103
117
|
described_class.new("--[no-]force", :flag, "Force installation")
|
104
118
|
end
|
105
119
|
|
106
|
-
|
107
|
-
|
108
|
-
|
120
|
+
describe "positive form" do
|
121
|
+
it "handles this" do
|
122
|
+
expect(option.handles?("--force")).to be true
|
123
|
+
end
|
109
124
|
end
|
110
125
|
|
111
|
-
describe "
|
126
|
+
describe "negative form" do
|
127
|
+
it "handles this" do
|
128
|
+
expect(option.handles?("--no-force")).to be true
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe "#flag_set?" do
|
133
|
+
|
134
|
+
describe "positive variant" do
|
135
|
+
it "returns true" do
|
136
|
+
expect(option.flag_set?("--force")).to be true
|
137
|
+
end
|
138
|
+
end
|
112
139
|
|
113
|
-
|
114
|
-
|
115
|
-
|
140
|
+
describe "negative variant" do
|
141
|
+
it "returns false" do
|
142
|
+
expect(option.flag_set?("--no-force")).to be false
|
143
|
+
end
|
116
144
|
end
|
117
145
|
|
118
146
|
end
|
@@ -120,7 +148,7 @@ describe Clamp::Option::Definition do
|
|
120
148
|
describe "#attribute_name" do
|
121
149
|
|
122
150
|
it "is derived from the (long) switch" do
|
123
|
-
expect(option.attribute_name).to
|
151
|
+
expect(option.attribute_name).to eq "force"
|
124
152
|
end
|
125
153
|
|
126
154
|
end
|
@@ -133,15 +161,22 @@ describe Clamp::Option::Definition do
|
|
133
161
|
described_class.new(["-k", "--key-file"], "FILE", "SSH identity")
|
134
162
|
end
|
135
163
|
|
136
|
-
|
137
|
-
|
138
|
-
|
164
|
+
describe "long switch" do
|
165
|
+
it "handles this" do
|
166
|
+
expect(option.handles?("--key-file")).to be true
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "short switch" do
|
171
|
+
it "handles this" do
|
172
|
+
expect(option.handles?("-k")).to be true
|
173
|
+
end
|
139
174
|
end
|
140
175
|
|
141
176
|
describe "#help" do
|
142
177
|
|
143
178
|
it "includes both switches" do
|
144
|
-
expect(option.help).to
|
179
|
+
expect(option.help).to eq ["-k, --key-file FILE", "SSH identity"]
|
145
180
|
end
|
146
181
|
|
147
182
|
end
|
@@ -157,12 +192,12 @@ describe Clamp::Option::Definition do
|
|
157
192
|
describe "#help" do
|
158
193
|
|
159
194
|
it "describes environment variable" do
|
160
|
-
expect(option.help).to
|
195
|
+
expect(option.help).to eq ["-x X", "mystery option (default: $APP_X)"]
|
161
196
|
end
|
162
197
|
|
163
198
|
end
|
164
199
|
|
165
|
-
context "
|
200
|
+
context "with a default value" do
|
166
201
|
|
167
202
|
let(:option) do
|
168
203
|
described_class.new("-x", "X", "mystery option", environment_variable: "APP_X", default: "xyz")
|
@@ -171,7 +206,23 @@ describe Clamp::Option::Definition do
|
|
171
206
|
describe "#help" do
|
172
207
|
|
173
208
|
it "describes both environment variable and default" do
|
174
|
-
expect(option.help).to
|
209
|
+
expect(option.help).to eq ["-x X", %{mystery option (default: $APP_X, or "xyz")}]
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
context "when it is required" do
|
217
|
+
|
218
|
+
let(:option) do
|
219
|
+
described_class.new("-x", "X", "mystery option", environment_variable: "APP_X", required: true)
|
220
|
+
end
|
221
|
+
|
222
|
+
describe "#help" do
|
223
|
+
|
224
|
+
it "describes the environment variable as the default" do
|
225
|
+
expect(option.help).to eql ["-x X", %{mystery option (required, default: $APP_X)}]
|
175
226
|
end
|
176
227
|
|
177
228
|
end
|
@@ -180,7 +231,7 @@ describe Clamp::Option::Definition do
|
|
180
231
|
|
181
232
|
end
|
182
233
|
|
183
|
-
context "multivalued" do
|
234
|
+
context "when multivalued" do
|
184
235
|
|
185
236
|
let(:option) do
|
186
237
|
described_class.new(["-H", "--header"], "HEADER", "extra header", multivalued: true)
|
@@ -193,12 +244,12 @@ describe Clamp::Option::Definition do
|
|
193
244
|
describe "#default_value" do
|
194
245
|
|
195
246
|
it "defaults to an empty Array" do
|
196
|
-
expect(option.default_value).to
|
247
|
+
expect(option.default_value).to be_empty
|
197
248
|
end
|
198
249
|
|
199
250
|
it "can be overridden" do
|
200
251
|
option = described_class.new("-H", "HEADER", "extra header", multivalued: true, default: [1, 2, 3])
|
201
|
-
expect(option.default_value).to
|
252
|
+
expect(option.default_value).to eq [1, 2, 3]
|
202
253
|
end
|
203
254
|
|
204
255
|
end
|
@@ -206,7 +257,7 @@ describe Clamp::Option::Definition do
|
|
206
257
|
describe "#attribute_name" do
|
207
258
|
|
208
259
|
it "gets a _list suffix" do
|
209
|
-
expect(option.attribute_name).to
|
260
|
+
expect(option.attribute_name).to eq "header_list"
|
210
261
|
end
|
211
262
|
|
212
263
|
end
|
@@ -214,7 +265,7 @@ describe Clamp::Option::Definition do
|
|
214
265
|
describe "#append_method" do
|
215
266
|
|
216
267
|
it "is derived from the attribute_name" do
|
217
|
-
expect(option.append_method).to
|
268
|
+
expect(option.append_method).to eq "append_to_header_list"
|
218
269
|
end
|
219
270
|
|
220
271
|
end
|
@@ -238,7 +289,7 @@ describe Clamp::Option::Definition do
|
|
238
289
|
it "includes help for each option exactly once" do
|
239
290
|
subcommand = command_class.send(:find_subcommand, "foo")
|
240
291
|
subcommand_help = subcommand.subcommand_class.help("")
|
241
|
-
expect(subcommand_help.lines.grep(/--bar BAR/).count).to
|
292
|
+
expect(subcommand_help.lines.grep(/--bar BAR/).count).to eq 1
|
242
293
|
end
|
243
294
|
|
244
295
|
end
|
@@ -262,6 +313,7 @@ describe Clamp::Option::Definition do
|
|
262
313
|
|
263
314
|
describe "a hidden option" do
|
264
315
|
let(:option) { described_class.new("--unseen", :flag, "Something", hidden: true) }
|
316
|
+
|
265
317
|
it "is hidden" do
|
266
318
|
expect(option).to be_hidden
|
267
319
|
end
|
@@ -285,7 +337,7 @@ describe Clamp::Option::Definition do
|
|
285
337
|
it "sets the expected accessor" do
|
286
338
|
command = command_class.new("foo")
|
287
339
|
command.run(["--unseen"])
|
288
|
-
expect(command
|
340
|
+
expect(command).to be_unseen
|
289
341
|
end
|
290
342
|
end
|
291
343
|
end
|