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