clamp 0.6.3 → 0.6.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/.autotest +2 -0
- data/.travis.yml +2 -0
- data/CHANGES.md +8 -0
- data/Gemfile +3 -3
- data/README.md +17 -3
- data/Rakefile +1 -1
- data/examples/fubar +1 -1
- data/lib/clamp/attribute/instance.rb +2 -5
- data/lib/clamp/command.rb +10 -0
- data/lib/clamp/errors.rb +12 -0
- data/lib/clamp/version.rb +1 -1
- data/spec/clamp/command_group_spec.rb +31 -31
- data/spec/clamp/command_spec.rb +244 -188
- data/spec/clamp/option/definition_spec.rb +37 -37
- data/spec/clamp/option_module_spec.rb +2 -2
- data/spec/clamp/parameter/definition_spec.rb +37 -37
- data/spec/spec_helper.rb +12 -0
- metadata +11 -13
@@ -2,33 +2,33 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Clamp::Option::Definition do
|
4
4
|
|
5
|
-
|
5
|
+
context "with String argument" do
|
6
6
|
|
7
7
|
let(:option) do
|
8
8
|
described_class.new("--key-file", "FILE", "SSH identity")
|
9
9
|
end
|
10
10
|
|
11
11
|
it "has a long_switch" do
|
12
|
-
option.long_switch.
|
12
|
+
expect(option.long_switch).to eql "--key-file"
|
13
13
|
end
|
14
14
|
|
15
15
|
it "has a type" do
|
16
|
-
option.type.
|
16
|
+
expect(option.type).to eql "FILE"
|
17
17
|
end
|
18
18
|
|
19
19
|
it "has a description" do
|
20
|
-
option.description.
|
20
|
+
expect(option.description).to eql "SSH identity"
|
21
21
|
end
|
22
22
|
|
23
23
|
describe "#attribute_name" do
|
24
24
|
|
25
25
|
it "is derived from the (long) switch" do
|
26
|
-
option.attribute_name.
|
26
|
+
expect(option.attribute_name).to eql "key_file"
|
27
27
|
end
|
28
28
|
|
29
29
|
it "can be overridden" do
|
30
30
|
option = described_class.new("--key-file", "FILE", "SSH identity", :attribute_name => "ssh_identity")
|
31
|
-
option.attribute_name.
|
31
|
+
expect(option.attribute_name).to eql "ssh_identity"
|
32
32
|
end
|
33
33
|
|
34
34
|
end
|
@@ -36,7 +36,7 @@ describe Clamp::Option::Definition do
|
|
36
36
|
describe "#write_method" do
|
37
37
|
|
38
38
|
it "is derived from the attribute_name" do
|
39
|
-
option.write_method.
|
39
|
+
expect(option.write_method).to eql "key_file="
|
40
40
|
end
|
41
41
|
|
42
42
|
end
|
@@ -45,12 +45,12 @@ describe Clamp::Option::Definition do
|
|
45
45
|
|
46
46
|
it "defaults to nil" do
|
47
47
|
option = described_class.new("-n", "N", "iterations")
|
48
|
-
option.default_value.
|
48
|
+
expect(option.default_value).to eql nil
|
49
49
|
end
|
50
50
|
|
51
51
|
it "can be overridden" do
|
52
52
|
option = described_class.new("-n", "N", "iterations", :default => 1)
|
53
|
-
option.default_value.
|
53
|
+
expect(option.default_value).to eql 1
|
54
54
|
end
|
55
55
|
|
56
56
|
end
|
@@ -58,14 +58,14 @@ describe Clamp::Option::Definition do
|
|
58
58
|
describe "#help" do
|
59
59
|
|
60
60
|
it "combines switch, type and description" do
|
61
|
-
option.help.
|
61
|
+
expect(option.help).to eql ["--key-file FILE", "SSH identity"]
|
62
62
|
end
|
63
63
|
|
64
64
|
end
|
65
65
|
|
66
66
|
end
|
67
67
|
|
68
|
-
|
68
|
+
context "flag" do
|
69
69
|
|
70
70
|
let(:option) do
|
71
71
|
described_class.new("--verbose", :flag, "Blah blah blah")
|
@@ -74,13 +74,13 @@ describe Clamp::Option::Definition do
|
|
74
74
|
describe "#default_conversion_block" do
|
75
75
|
|
76
76
|
it "converts truthy values to true" do
|
77
|
-
option.default_conversion_block.call("true").
|
78
|
-
option.default_conversion_block.call("yes").
|
77
|
+
expect(option.default_conversion_block.call("true")).to eql true
|
78
|
+
expect(option.default_conversion_block.call("yes")).to eql true
|
79
79
|
end
|
80
80
|
|
81
81
|
it "converts falsey values to false" do
|
82
|
-
option.default_conversion_block.call("false").
|
83
|
-
option.default_conversion_block.call("no").
|
82
|
+
expect(option.default_conversion_block.call("false")).to eql false
|
83
|
+
expect(option.default_conversion_block.call("no")).to eql false
|
84
84
|
end
|
85
85
|
|
86
86
|
end
|
@@ -88,29 +88,29 @@ describe Clamp::Option::Definition do
|
|
88
88
|
describe "#help" do
|
89
89
|
|
90
90
|
it "excludes option argument" do
|
91
|
-
option.help.
|
91
|
+
expect(option.help).to eql ["--verbose", "Blah blah blah"]
|
92
92
|
end
|
93
93
|
|
94
94
|
end
|
95
95
|
|
96
96
|
end
|
97
97
|
|
98
|
-
|
98
|
+
context "negatable flag" do
|
99
99
|
|
100
100
|
let(:option) do
|
101
101
|
described_class.new("--[no-]force", :flag, "Force installation")
|
102
102
|
end
|
103
103
|
|
104
104
|
it "handles both positive and negative forms" do
|
105
|
-
option.handles?("--force").
|
106
|
-
option.handles?("--no-force").
|
105
|
+
expect(option.handles?("--force")).to be true
|
106
|
+
expect(option.handles?("--no-force")).to be true
|
107
107
|
end
|
108
108
|
|
109
109
|
describe "#flag_value" do
|
110
110
|
|
111
111
|
it "returns true for the positive variant" do
|
112
|
-
option.flag_value("--force").
|
113
|
-
option.flag_value("--no-force").
|
112
|
+
expect(option.flag_value("--force")).to be true
|
113
|
+
expect(option.flag_value("--no-force")).to be false
|
114
114
|
end
|
115
115
|
|
116
116
|
end
|
@@ -118,35 +118,35 @@ describe Clamp::Option::Definition do
|
|
118
118
|
describe "#attribute_name" do
|
119
119
|
|
120
120
|
it "is derived from the (long) switch" do
|
121
|
-
option.attribute_name.
|
121
|
+
expect(option.attribute_name).to eql "force"
|
122
122
|
end
|
123
123
|
|
124
124
|
end
|
125
125
|
|
126
126
|
end
|
127
127
|
|
128
|
-
|
128
|
+
context "with both short and long switches" do
|
129
129
|
|
130
130
|
let(:option) do
|
131
131
|
described_class.new(["-k", "--key-file"], "FILE", "SSH identity")
|
132
132
|
end
|
133
133
|
|
134
134
|
it "handles both switches" do
|
135
|
-
option.handles?("--key-file").
|
136
|
-
option.handles?("-k").
|
135
|
+
expect(option.handles?("--key-file")).to be true
|
136
|
+
expect(option.handles?("-k")).to be true
|
137
137
|
end
|
138
138
|
|
139
139
|
describe "#help" do
|
140
140
|
|
141
141
|
it "includes both switches" do
|
142
|
-
option.help.
|
142
|
+
expect(option.help).to eql ["-k, --key-file FILE", "SSH identity"]
|
143
143
|
end
|
144
144
|
|
145
145
|
end
|
146
146
|
|
147
147
|
end
|
148
148
|
|
149
|
-
|
149
|
+
context "with an associated environment variable" do
|
150
150
|
|
151
151
|
let(:option) do
|
152
152
|
described_class.new("-x", "X", "mystery option", :environment_variable => "APP_X")
|
@@ -155,12 +155,12 @@ describe Clamp::Option::Definition do
|
|
155
155
|
describe "#help" do
|
156
156
|
|
157
157
|
it "describes environment variable" do
|
158
|
-
option.help.
|
158
|
+
expect(option.help).to eql ["-x X", "mystery option (default: $APP_X)"]
|
159
159
|
end
|
160
160
|
|
161
161
|
end
|
162
162
|
|
163
|
-
|
163
|
+
context "and a default value" do
|
164
164
|
|
165
165
|
let(:option) do
|
166
166
|
described_class.new("-x", "X", "mystery option", :environment_variable => "APP_X", :default => "xyz")
|
@@ -169,7 +169,7 @@ describe Clamp::Option::Definition do
|
|
169
169
|
describe "#help" do
|
170
170
|
|
171
171
|
it "describes both environment variable and default" do
|
172
|
-
option.help.
|
172
|
+
expect(option.help).to eql ["-x X", %{mystery option (default: $APP_X, or "xyz")}]
|
173
173
|
end
|
174
174
|
|
175
175
|
end
|
@@ -178,25 +178,25 @@ describe Clamp::Option::Definition do
|
|
178
178
|
|
179
179
|
end
|
180
180
|
|
181
|
-
|
181
|
+
context "multivalued" do
|
182
182
|
|
183
183
|
let(:option) do
|
184
184
|
described_class.new(["-H", "--header"], "HEADER", "extra header", :multivalued => true)
|
185
185
|
end
|
186
186
|
|
187
187
|
it "is multivalued" do
|
188
|
-
option.
|
188
|
+
expect(option).to be_multivalued
|
189
189
|
end
|
190
190
|
|
191
191
|
describe "#default_value" do
|
192
192
|
|
193
193
|
it "defaults to an empty Array" do
|
194
|
-
option.default_value.
|
194
|
+
expect(option.default_value).to eql []
|
195
195
|
end
|
196
196
|
|
197
197
|
it "can be overridden" do
|
198
198
|
option = described_class.new("-H", "HEADER", "extra header", :multivalued => true, :default => [1,2,3])
|
199
|
-
option.default_value.
|
199
|
+
expect(option.default_value).to eql [1,2,3]
|
200
200
|
end
|
201
201
|
|
202
202
|
end
|
@@ -204,7 +204,7 @@ describe Clamp::Option::Definition do
|
|
204
204
|
describe "#attribute_name" do
|
205
205
|
|
206
206
|
it "gets a _list suffix" do
|
207
|
-
option.attribute_name.
|
207
|
+
expect(option.attribute_name).to eql "header_list"
|
208
208
|
end
|
209
209
|
|
210
210
|
end
|
@@ -212,7 +212,7 @@ describe Clamp::Option::Definition do
|
|
212
212
|
describe "#append_method" do
|
213
213
|
|
214
214
|
it "is derived from the attribute_name" do
|
215
|
-
option.append_method.
|
215
|
+
expect(option.append_method).to eql "append_to_header_list"
|
216
216
|
end
|
217
217
|
|
218
218
|
end
|
@@ -236,7 +236,7 @@ describe Clamp::Option::Definition do
|
|
236
236
|
it "includes help for each option exactly once" do
|
237
237
|
subcommand = command_class.send(:find_subcommand, 'foo')
|
238
238
|
subcommand_help = subcommand.subcommand_class.help("")
|
239
|
-
subcommand_help.lines.grep(/--bar BAR/).count.
|
239
|
+
expect(subcommand_help.lines.grep(/--bar BAR/).count).to eql 1
|
240
240
|
end
|
241
241
|
|
242
242
|
end
|
@@ -4,7 +4,7 @@ describe Clamp::Command do
|
|
4
4
|
|
5
5
|
include OutputCapture
|
6
6
|
|
7
|
-
|
7
|
+
context "with included module" do
|
8
8
|
|
9
9
|
let(:command) do
|
10
10
|
|
@@ -29,7 +29,7 @@ describe Clamp::Command do
|
|
29
29
|
|
30
30
|
it "accepts options from included module" do
|
31
31
|
command.run(["--size", "42"])
|
32
|
-
stdout.
|
32
|
+
expect(stdout).to eql "size = 42\n"
|
33
33
|
end
|
34
34
|
|
35
35
|
end
|
@@ -2,33 +2,33 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Clamp::Parameter::Definition do
|
4
4
|
|
5
|
-
|
5
|
+
context "normal" do
|
6
6
|
|
7
7
|
let(:parameter) do
|
8
8
|
described_class.new("COLOR", "hue of choice")
|
9
9
|
end
|
10
10
|
|
11
11
|
it "has a name" do
|
12
|
-
parameter.name.
|
12
|
+
expect(parameter.name).to eql "COLOR"
|
13
13
|
end
|
14
14
|
|
15
15
|
it "has a description" do
|
16
|
-
parameter.description.
|
16
|
+
expect(parameter.description).to eql "hue of choice"
|
17
17
|
end
|
18
18
|
|
19
19
|
it "is single-valued" do
|
20
|
-
parameter.
|
20
|
+
expect(parameter).to_not be_multivalued
|
21
21
|
end
|
22
22
|
|
23
23
|
describe "#attribute_name" do
|
24
24
|
|
25
25
|
it "is derived from the name" do
|
26
|
-
parameter.attribute_name.
|
26
|
+
expect(parameter.attribute_name).to eql "color"
|
27
27
|
end
|
28
28
|
|
29
29
|
it "can be overridden" do
|
30
30
|
parameter = described_class.new("COLOR", "hue of choice", :attribute_name => "hue")
|
31
|
-
parameter.attribute_name.
|
31
|
+
expect(parameter.attribute_name).to eql "hue"
|
32
32
|
end
|
33
33
|
|
34
34
|
end
|
@@ -37,17 +37,17 @@ describe Clamp::Parameter::Definition do
|
|
37
37
|
|
38
38
|
it "consumes one argument" do
|
39
39
|
arguments = %w(a b c)
|
40
|
-
parameter.consume(arguments).
|
41
|
-
arguments.
|
40
|
+
expect(parameter.consume(arguments)).to eql ["a"]
|
41
|
+
expect(arguments).to eql %w(b c)
|
42
42
|
end
|
43
43
|
|
44
44
|
describe "with no arguments" do
|
45
45
|
|
46
46
|
it "raises an Argument error" do
|
47
47
|
arguments = []
|
48
|
-
|
48
|
+
expect do
|
49
49
|
parameter.consume(arguments)
|
50
|
-
end.
|
50
|
+
end.to raise_error(ArgumentError)
|
51
51
|
end
|
52
52
|
|
53
53
|
end
|
@@ -56,20 +56,20 @@ describe Clamp::Parameter::Definition do
|
|
56
56
|
|
57
57
|
end
|
58
58
|
|
59
|
-
|
59
|
+
context "optional (name in square brackets)" do
|
60
60
|
|
61
61
|
let(:parameter) do
|
62
62
|
described_class.new("[COLOR]", "hue of choice")
|
63
63
|
end
|
64
64
|
|
65
65
|
it "is single-valued" do
|
66
|
-
parameter.
|
66
|
+
expect(parameter).to_not be_multivalued
|
67
67
|
end
|
68
68
|
|
69
69
|
describe "#attribute_name" do
|
70
70
|
|
71
71
|
it "omits the brackets" do
|
72
|
-
parameter.attribute_name.
|
72
|
+
expect(parameter.attribute_name).to eql "color"
|
73
73
|
end
|
74
74
|
|
75
75
|
end
|
@@ -78,15 +78,15 @@ describe Clamp::Parameter::Definition do
|
|
78
78
|
|
79
79
|
it "consumes one argument" do
|
80
80
|
arguments = %w(a b c)
|
81
|
-
parameter.consume(arguments).
|
82
|
-
arguments.
|
81
|
+
expect(parameter.consume(arguments)).to eql ["a"]
|
82
|
+
expect(arguments).to eql %w(b c)
|
83
83
|
end
|
84
84
|
|
85
85
|
describe "with no arguments" do
|
86
86
|
|
87
87
|
it "consumes nothing" do
|
88
88
|
arguments = []
|
89
|
-
parameter.consume(arguments).
|
89
|
+
expect(parameter.consume(arguments)).to eql []
|
90
90
|
end
|
91
91
|
|
92
92
|
end
|
@@ -95,20 +95,20 @@ describe Clamp::Parameter::Definition do
|
|
95
95
|
|
96
96
|
end
|
97
97
|
|
98
|
-
|
98
|
+
context "list (name followed by ellipsis)" do
|
99
99
|
|
100
100
|
let(:parameter) do
|
101
101
|
described_class.new("FILE ...", "files to process")
|
102
102
|
end
|
103
103
|
|
104
104
|
it "is multi-valued" do
|
105
|
-
parameter.
|
105
|
+
expect(parameter).to be_multivalued
|
106
106
|
end
|
107
107
|
|
108
108
|
describe "#attribute_name" do
|
109
109
|
|
110
110
|
it "gets a _list suffix" do
|
111
|
-
parameter.attribute_name.
|
111
|
+
expect(parameter.attribute_name).to eql "file_list"
|
112
112
|
end
|
113
113
|
|
114
114
|
end
|
@@ -116,7 +116,7 @@ describe Clamp::Parameter::Definition do
|
|
116
116
|
describe "#append_method" do
|
117
117
|
|
118
118
|
it "is derived from the attribute_name" do
|
119
|
-
parameter.append_method.
|
119
|
+
expect(parameter.append_method).to eql "append_to_file_list"
|
120
120
|
end
|
121
121
|
|
122
122
|
end
|
@@ -125,17 +125,17 @@ describe Clamp::Parameter::Definition do
|
|
125
125
|
|
126
126
|
it "consumes all the remaining arguments" do
|
127
127
|
arguments = %w(a b c)
|
128
|
-
parameter.consume(arguments).
|
129
|
-
arguments.
|
128
|
+
expect(parameter.consume(arguments)).to eql %w(a b c)
|
129
|
+
expect(arguments).to eql []
|
130
130
|
end
|
131
131
|
|
132
132
|
describe "with no arguments" do
|
133
133
|
|
134
134
|
it "raises an Argument error" do
|
135
135
|
arguments = []
|
136
|
-
|
136
|
+
expect do
|
137
137
|
parameter.consume(arguments)
|
138
|
-
end.
|
138
|
+
end.to raise_error(ArgumentError)
|
139
139
|
end
|
140
140
|
|
141
141
|
end
|
@@ -151,7 +151,7 @@ describe Clamp::Parameter::Definition do
|
|
151
151
|
describe "#attribute_name" do
|
152
152
|
|
153
153
|
it "is the specified one" do
|
154
|
-
parameter.attribute_name.
|
154
|
+
expect(parameter.attribute_name).to eql "config_settings"
|
155
155
|
end
|
156
156
|
|
157
157
|
end
|
@@ -160,20 +160,20 @@ describe Clamp::Parameter::Definition do
|
|
160
160
|
|
161
161
|
end
|
162
162
|
|
163
|
-
|
163
|
+
context "optional list" do
|
164
164
|
|
165
165
|
let(:parameter) do
|
166
166
|
described_class.new("[FILES] ...", "files to process")
|
167
167
|
end
|
168
168
|
|
169
169
|
it "is multi-valued" do
|
170
|
-
parameter.
|
170
|
+
expect(parameter).to be_multivalued
|
171
171
|
end
|
172
172
|
|
173
173
|
describe "#attribute_name" do
|
174
174
|
|
175
175
|
it "gets a _list suffix" do
|
176
|
-
parameter.attribute_name.
|
176
|
+
expect(parameter.attribute_name).to eql "files_list"
|
177
177
|
end
|
178
178
|
|
179
179
|
end
|
@@ -181,7 +181,7 @@ describe Clamp::Parameter::Definition do
|
|
181
181
|
describe "#default_value" do
|
182
182
|
|
183
183
|
it "is an empty list" do
|
184
|
-
parameter.default_value.
|
184
|
+
expect(parameter.default_value).to eql []
|
185
185
|
end
|
186
186
|
|
187
187
|
end
|
@@ -189,12 +189,12 @@ describe Clamp::Parameter::Definition do
|
|
189
189
|
describe "#help" do
|
190
190
|
|
191
191
|
it "does not include default" do
|
192
|
-
parameter.help_rhs.
|
192
|
+
expect(parameter.help_rhs).to_not include("default:")
|
193
193
|
end
|
194
194
|
|
195
195
|
end
|
196
196
|
|
197
|
-
|
197
|
+
context "with specified default value" do
|
198
198
|
|
199
199
|
let(:parameter) do
|
200
200
|
described_class.new("[FILES] ...", "files to process", :default => %w(a b c))
|
@@ -203,7 +203,7 @@ describe Clamp::Parameter::Definition do
|
|
203
203
|
describe "#default_value" do
|
204
204
|
|
205
205
|
it "is that specified" do
|
206
|
-
parameter.default_value.
|
206
|
+
expect(parameter.default_value).to eql %w(a b c)
|
207
207
|
end
|
208
208
|
|
209
209
|
end
|
@@ -211,7 +211,7 @@ describe Clamp::Parameter::Definition do
|
|
211
211
|
describe "#help" do
|
212
212
|
|
213
213
|
it "includes the default value" do
|
214
|
-
parameter.help_rhs.
|
214
|
+
expect(parameter.help_rhs).to include("default:")
|
215
215
|
end
|
216
216
|
|
217
217
|
end
|
@@ -220,15 +220,15 @@ describe Clamp::Parameter::Definition do
|
|
220
220
|
|
221
221
|
it "consumes all the remaining arguments" do
|
222
222
|
arguments = %w(a b c)
|
223
|
-
parameter.consume(arguments).
|
224
|
-
arguments.
|
223
|
+
expect(parameter.consume(arguments)).to eql %w(a b c)
|
224
|
+
expect(arguments).to eql []
|
225
225
|
end
|
226
226
|
|
227
|
-
|
227
|
+
context "with no arguments" do
|
228
228
|
|
229
229
|
it "don't override defaults" do
|
230
230
|
arguments = []
|
231
|
-
parameter.consume(arguments).
|
231
|
+
expect(parameter.consume(arguments)).to eql []
|
232
232
|
end
|
233
233
|
|
234
234
|
end
|