clamp 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Clamp::Option do
3
+ describe Clamp::Option::Definition do
4
4
 
5
5
  describe "with String argument" do
6
6
 
7
7
  let(:option) do
8
- Clamp::Option.new("--key-file", "FILE", "SSH identity")
8
+ described_class.new("--key-file", "FILE", "SSH identity")
9
9
  end
10
10
 
11
11
  it "has a long_switch" do
@@ -27,21 +27,29 @@ describe Clamp::Option do
27
27
  end
28
28
 
29
29
  it "can be overridden" do
30
- option = Clamp::Option.new("--key-file", "FILE", "SSH identity", :attribute_name => "ssh_identity")
30
+ option = described_class.new("--key-file", "FILE", "SSH identity", :attribute_name => "ssh_identity")
31
31
  option.attribute_name.should == "ssh_identity"
32
32
  end
33
33
 
34
34
  end
35
35
 
36
+ describe "#write_method" do
37
+
38
+ it "is derived from the attribute_name" do
39
+ option.write_method.should == "key_file="
40
+ end
41
+
42
+ end
43
+
36
44
  describe "#default_value" do
37
45
 
38
46
  it "defaults to nil" do
39
- option = Clamp::Option.new("-n", "N", "iterations")
47
+ option = described_class.new("-n", "N", "iterations")
40
48
  option.default_value.should == nil
41
49
  end
42
50
 
43
51
  it "can be overridden" do
44
- option = Clamp::Option.new("-n", "N", "iterations", :default => 1)
52
+ option = described_class.new("-n", "N", "iterations", :default => 1)
45
53
  option.default_value.should == 1
46
54
  end
47
55
 
@@ -60,7 +68,21 @@ describe Clamp::Option do
60
68
  describe "flag" do
61
69
 
62
70
  let(:option) do
63
- Clamp::Option.new("--verbose", :flag, "Blah blah blah")
71
+ described_class.new("--verbose", :flag, "Blah blah blah")
72
+ end
73
+
74
+ describe "#default_conversion_block" do
75
+
76
+ it "converts truthy values to true" do
77
+ option.default_conversion_block.call("true").should == true
78
+ option.default_conversion_block.call("yes").should == true
79
+ end
80
+
81
+ it "converts falsey values to false" do
82
+ option.default_conversion_block.call("false").should == false
83
+ option.default_conversion_block.call("no").should == false
84
+ end
85
+
64
86
  end
65
87
 
66
88
  describe "#help" do
@@ -76,7 +98,7 @@ describe Clamp::Option do
76
98
  describe "negatable flag" do
77
99
 
78
100
  let(:option) do
79
- Clamp::Option.new("--[no-]force", :flag, "Force installation")
101
+ described_class.new("--[no-]force", :flag, "Force installation")
80
102
  end
81
103
 
82
104
  it "handles both positive and negative forms" do
@@ -106,7 +128,7 @@ describe Clamp::Option do
106
128
  describe "with both short and long switches" do
107
129
 
108
130
  let(:option) do
109
- Clamp::Option.new(["-k", "--key-file"], "FILE", "SSH identity")
131
+ described_class.new(["-k", "--key-file"], "FILE", "SSH identity")
110
132
  end
111
133
 
112
134
  it "handles both switches" do
@@ -127,7 +149,7 @@ describe Clamp::Option do
127
149
  describe "with an associated environment variable" do
128
150
 
129
151
  let(:option) do
130
- Clamp::Option.new("-x", "X", "mystery option", :environment_variable => "APP_X")
152
+ described_class.new("-x", "X", "mystery option", :environment_variable => "APP_X")
131
153
  end
132
154
 
133
155
  describe "#help" do
@@ -141,7 +163,7 @@ describe Clamp::Option do
141
163
  describe "and a default value" do
142
164
 
143
165
  let(:option) do
144
- Clamp::Option.new("-x", "X", "mystery option", :environment_variable => "APP_X", :default => "xyz")
166
+ described_class.new("-x", "X", "mystery option", :environment_variable => "APP_X", :default => "xyz")
145
167
  end
146
168
 
147
169
  describe "#help" do
@@ -156,6 +178,47 @@ describe Clamp::Option do
156
178
 
157
179
  end
158
180
 
181
+ describe "multivalued" do
182
+
183
+ let(:option) do
184
+ described_class.new(["-H", "--header"], "HEADER", "extra header", :multivalued => true)
185
+ end
186
+
187
+ it "is multivalued" do
188
+ option.should be_multivalued
189
+ end
190
+
191
+ describe "#default_value" do
192
+
193
+ it "defaults to an empty Array" do
194
+ option.default_value.should == []
195
+ end
196
+
197
+ it "can be overridden" do
198
+ option = described_class.new("-H", "HEADER", "extra header", :multivalued => true, :default => [1,2,3])
199
+ option.default_value.should == [1,2,3]
200
+ end
201
+
202
+ end
203
+
204
+ describe "#attribute_name" do
205
+
206
+ it "gets a _list suffix" do
207
+ option.attribute_name.should == "header_list"
208
+ end
209
+
210
+ end
211
+
212
+ describe "#write_method" do
213
+
214
+ it "is derived from the attribute_name" do
215
+ option.write_method.should == "append_to_header_list"
216
+ end
217
+
218
+ end
219
+
220
+ end
221
+
159
222
  describe "in subcommand" do
160
223
 
161
224
  let(:command_class) do
@@ -171,7 +234,7 @@ describe Clamp::Option do
171
234
  describe "Command#help" do
172
235
 
173
236
  it "includes help for each option exactly once" do
174
- subcommand = command_class.new("").send(:find_subcommand, 'foo')
237
+ subcommand = command_class.send(:find_subcommand, 'foo')
175
238
  subcommand_help = subcommand.subcommand_class.help("")
176
239
  subcommand_help.lines.grep(/--bar BAR/).count.should == 1
177
240
  end
@@ -183,14 +246,14 @@ describe Clamp::Option do
183
246
  describe "a required option" do
184
247
  it "rejects :default" do
185
248
  expect do
186
- Clamp::Option.new("--key-file", "FILE", "SSH identity",
249
+ described_class.new("--key-file", "FILE", "SSH identity",
187
250
  :required => true, :default => "hello")
188
251
  end.to raise_error(ArgumentError)
189
252
  end
190
253
 
191
254
  it "rejects :flag options" do
192
255
  expect do
193
- Clamp::Option.new("--awesome", :flag, "Be awesome?", :required => true)
256
+ described_class.new("--awesome", :flag, "Be awesome?", :required => true)
194
257
  end.to raise_error(ArgumentError)
195
258
  end
196
259
  end
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Clamp::Parameter do
3
+ describe Clamp::Parameter::Definition do
4
4
 
5
5
  describe "normal" do
6
6
 
7
7
  let(:parameter) do
8
- Clamp::Parameter.new("COLOR", "hue of choice")
8
+ described_class.new("COLOR", "hue of choice")
9
9
  end
10
10
 
11
11
  it "has a name" do
@@ -27,7 +27,7 @@ describe Clamp::Parameter do
27
27
  end
28
28
 
29
29
  it "can be overridden" do
30
- parameter = Clamp::Parameter.new("COLOR", "hue of choice", :attribute_name => "hue")
30
+ parameter = described_class.new("COLOR", "hue of choice", :attribute_name => "hue")
31
31
  parameter.attribute_name.should == "hue"
32
32
  end
33
33
 
@@ -37,7 +37,7 @@ describe Clamp::Parameter do
37
37
 
38
38
  it "consumes one argument" do
39
39
  arguments = %w(a b c)
40
- parameter.consume(arguments).should == "a"
40
+ parameter.consume(arguments).should == ["a"]
41
41
  arguments.should == %w(b c)
42
42
  end
43
43
 
@@ -59,7 +59,7 @@ describe Clamp::Parameter do
59
59
  describe "optional (name in square brackets)" do
60
60
 
61
61
  let(:parameter) do
62
- Clamp::Parameter.new("[COLOR]", "hue of choice")
62
+ described_class.new("[COLOR]", "hue of choice")
63
63
  end
64
64
 
65
65
  it "is single-valued" do
@@ -78,15 +78,15 @@ describe Clamp::Parameter do
78
78
 
79
79
  it "consumes one argument" do
80
80
  arguments = %w(a b c)
81
- parameter.consume(arguments).should == "a"
81
+ parameter.consume(arguments).should == ["a"]
82
82
  arguments.should == %w(b c)
83
83
  end
84
84
 
85
85
  describe "with no arguments" do
86
86
 
87
- it "returns nil" do
87
+ it "consumes nothing" do
88
88
  arguments = []
89
- parameter.consume(arguments).should == nil
89
+ parameter.consume(arguments).should == []
90
90
  end
91
91
 
92
92
  end
@@ -98,7 +98,7 @@ describe Clamp::Parameter do
98
98
  describe "list (name followed by ellipsis)" do
99
99
 
100
100
  let(:parameter) do
101
- Clamp::Parameter.new("FILE ...", "files to process")
101
+ described_class.new("FILE ...", "files to process")
102
102
  end
103
103
 
104
104
  it "is multi-valued" do
@@ -113,6 +113,14 @@ describe Clamp::Parameter do
113
113
 
114
114
  end
115
115
 
116
+ describe "#write_method" do
117
+
118
+ it "is derived from the attribute_name" do
119
+ parameter.write_method.should == "append_to_file_list"
120
+ end
121
+
122
+ end
123
+
116
124
  describe "#consume" do
117
125
 
118
126
  it "consumes all the remaining arguments" do
@@ -137,7 +145,7 @@ describe Clamp::Parameter do
137
145
  context "with a weird parameter name, and an explicit attribute_name" do
138
146
 
139
147
  let(:parameter) do
140
- Clamp::Parameter.new("KEY=VALUE ...", "config-settings", :attribute_name => :config_settings)
148
+ described_class.new("KEY=VALUE ...", "config-settings", :attribute_name => :config_settings)
141
149
  end
142
150
 
143
151
  describe "#attribute_name" do
@@ -155,7 +163,7 @@ describe Clamp::Parameter do
155
163
  describe "optional list" do
156
164
 
157
165
  let(:parameter) do
158
- Clamp::Parameter.new("[FILES] ...", "files to process")
166
+ described_class.new("[FILES] ...", "files to process")
159
167
  end
160
168
 
161
169
  it "is multi-valued" do
@@ -189,7 +197,7 @@ describe Clamp::Parameter do
189
197
  describe "with specified default value" do
190
198
 
191
199
  let(:parameter) do
192
- Clamp::Parameter.new("[FILES] ...", "files to process", :default => %w(a b c))
200
+ described_class.new("[FILES] ...", "files to process", :default => %w(a b c))
193
201
  end
194
202
 
195
203
  describe "#default_value" do
@@ -220,7 +228,7 @@ describe Clamp::Parameter do
220
228
 
221
229
  it "don't override defaults" do
222
230
  arguments = []
223
- parameter.consume(arguments).should == nil
231
+ parameter.consume(arguments).should == []
224
232
  end
225
233
 
226
234
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-07 00:00:00.000000000 Z
11
+ date: 2013-04-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ! "Clamp provides an object-model for command-line utilities. \nIt handles
14
14
  parsing of command-line options, and generation of usage help.\n"
@@ -17,40 +17,44 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - .autotest
20
21
  - .gitignore
21
22
  - .rspec
22
23
  - .travis.yml
24
+ - CHANGES.md
23
25
  - Gemfile
24
- - README.markdown
26
+ - README.md
25
27
  - Rakefile
26
28
  - clamp.gemspec
27
29
  - examples/admin
28
30
  - examples/flipflop
29
31
  - examples/fubar
30
32
  - examples/gitdown
33
+ - examples/scoop
31
34
  - examples/speak
32
35
  - lib/clamp.rb
33
- - lib/clamp/attribute.rb
34
- - lib/clamp/attribute_declaration.rb
36
+ - lib/clamp/attribute/declaration.rb
37
+ - lib/clamp/attribute/definition.rb
35
38
  - lib/clamp/command.rb
36
39
  - lib/clamp/errors.rb
37
40
  - lib/clamp/help.rb
38
- - lib/clamp/option.rb
39
41
  - lib/clamp/option/declaration.rb
42
+ - lib/clamp/option/definition.rb
40
43
  - lib/clamp/option/parsing.rb
41
- - lib/clamp/parameter.rb
42
44
  - lib/clamp/parameter/declaration.rb
45
+ - lib/clamp/parameter/definition.rb
43
46
  - lib/clamp/parameter/parsing.rb
44
- - lib/clamp/subcommand.rb
45
47
  - lib/clamp/subcommand/declaration.rb
48
+ - lib/clamp/subcommand/definition.rb
46
49
  - lib/clamp/subcommand/execution.rb
47
50
  - lib/clamp/subcommand/parsing.rb
51
+ - lib/clamp/truthy.rb
48
52
  - lib/clamp/version.rb
49
53
  - spec/clamp/command_group_spec.rb
50
54
  - spec/clamp/command_spec.rb
55
+ - spec/clamp/option/definition_spec.rb
51
56
  - spec/clamp/option_module_spec.rb
52
- - spec/clamp/option_spec.rb
53
- - spec/clamp/parameter_spec.rb
57
+ - spec/clamp/parameter/definition_spec.rb
54
58
  - spec/spec_helper.rb
55
59
  homepage: http://github.com/mdub/clamp
56
60
  licenses: []
@@ -78,7 +82,7 @@ summary: a minimal framework for command-line utilities
78
82
  test_files:
79
83
  - spec/clamp/command_group_spec.rb
80
84
  - spec/clamp/command_spec.rb
85
+ - spec/clamp/option/definition_spec.rb
81
86
  - spec/clamp/option_module_spec.rb
82
- - spec/clamp/option_spec.rb
83
- - spec/clamp/parameter_spec.rb
87
+ - spec/clamp/parameter/definition_spec.rb
84
88
  - spec/spec_helper.rb
@@ -1,44 +0,0 @@
1
- module Clamp
2
-
3
- class Attribute
4
-
5
- attr_reader :description, :attribute_name, :default_value, :environment_variable
6
-
7
- def help_rhs
8
- description + default_description
9
- end
10
-
11
- def help
12
- [help_lhs, help_rhs]
13
- end
14
-
15
- def ivar_name
16
- "@#{attribute_name}"
17
- end
18
-
19
- def read_method
20
- attribute_name
21
- end
22
-
23
- def default_method
24
- "default_#{read_method}"
25
- end
26
-
27
- def write_method
28
- "#{attribute_name}="
29
- end
30
-
31
- private
32
-
33
- def default_description
34
- default_sources = [
35
- ("$#{@environment_variable}" if defined?(@environment_variable)),
36
- (@default_value.inspect if defined?(@default_value))
37
- ].compact
38
- return "" if default_sources.empty?
39
- " (default: " + default_sources.join(", or ") + ")"
40
- end
41
-
42
- end
43
-
44
- end
@@ -1,40 +0,0 @@
1
- module Clamp
2
-
3
- module AttributeDeclaration
4
-
5
- protected
6
-
7
- def define_accessors_for(attribute, &block)
8
- define_reader_for(attribute)
9
- define_default_for(attribute)
10
- define_writer_for(attribute, &block)
11
- end
12
-
13
- def define_reader_for(attribute)
14
- define_method(attribute.read_method) do
15
- if instance_variable_defined?(attribute.ivar_name)
16
- instance_variable_get(attribute.ivar_name)
17
- else
18
- send(attribute.default_method)
19
- end
20
- end
21
- end
22
-
23
- def define_default_for(attribute)
24
- define_method(attribute.default_method) do
25
- attribute.default_value
26
- end
27
- end
28
-
29
- def define_writer_for(attribute, &block)
30
- define_method(attribute.write_method) do |value|
31
- if block
32
- value = instance_exec(value, &block)
33
- end
34
- instance_variable_set(attribute.ivar_name, value)
35
- end
36
- end
37
-
38
- end
39
-
40
- end