clamp 0.1.5 → 0.1.6
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.
- data/README.markdown +21 -1
- data/clamp.gemspec +1 -1
- data/lib/clamp/attribute_declaration.rb +10 -1
- data/lib/clamp/parameter.rb +9 -2
- data/lib/clamp/version.rb +1 -1
- data/spec/clamp/command_spec.rb +42 -5
- metadata +7 -7
data/README.markdown
CHANGED
@@ -42,7 +42,7 @@ Calling `run` on a command class creates an instance of it, then invokes it usin
|
|
42
42
|
|
43
43
|
SpeakCommand.run
|
44
44
|
|
45
|
-
Class-level methods like `option` and `parameter` declare attributes (in a similar way to `attr_accessor`), and arrange for them to be populated automatically based on command-line arguments. They are
|
45
|
+
Class-level methods like `option` and `parameter` declare attributes (in a similar way to `attr_accessor`), and arrange for them to be populated automatically based on command-line arguments. They are also used to generate `help` documentation.
|
46
46
|
|
47
47
|
Declaring options
|
48
48
|
-----------------
|
@@ -148,6 +148,26 @@ While Clamp provides an attribute-writer method for each declared option or para
|
|
148
148
|
@server_address, @server_port = server.split(":")
|
149
149
|
end
|
150
150
|
|
151
|
+
### Default values
|
152
|
+
|
153
|
+
Default values can be specified for options:
|
154
|
+
|
155
|
+
option "--flavour", "FLAVOUR", "ice-cream flavour", :default => "chocolate"
|
156
|
+
|
157
|
+
and also for optional parameters
|
158
|
+
|
159
|
+
parameter "[HOST]", "server host", :default => "localhost"
|
160
|
+
|
161
|
+
For more advanced cases, you can also specify default values by defining a method called "`default_#{attribute_name}`":
|
162
|
+
|
163
|
+
option "--http-port", "PORT", "web-server port", :default => 9000
|
164
|
+
|
165
|
+
option "--admin-port", "PORT", "admin port"
|
166
|
+
|
167
|
+
def default_admin_port
|
168
|
+
http_port + 1
|
169
|
+
end
|
170
|
+
|
151
171
|
Declaring Subcommands
|
152
172
|
---------------------
|
153
173
|
|
data/clamp.gemspec
CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
|
|
13
13
|
|
14
14
|
s.summary = %q{a minimal framework for command-line utilities}
|
15
15
|
s.description = <<EOF
|
16
|
-
Clamp
|
16
|
+
Clamp provides an object-model for command-line utilities.
|
17
17
|
It handles parsing of command-line options, and generation of usage help.
|
18
18
|
EOF
|
19
19
|
|
@@ -6,6 +6,7 @@ module Clamp
|
|
6
6
|
|
7
7
|
def define_accessors_for(attribute, &block)
|
8
8
|
define_reader_for(attribute)
|
9
|
+
define_default_for(attribute)
|
9
10
|
define_writer_for(attribute, &block)
|
10
11
|
end
|
11
12
|
|
@@ -18,7 +19,15 @@ module Clamp
|
|
18
19
|
instance_variable_get(ivar_name)
|
19
20
|
elsif parent_command && parent_command.respond_to?(reader_name)
|
20
21
|
parent_command.send(reader_name)
|
21
|
-
elsif
|
22
|
+
elsif respond_to?("default_#{attribute.attribute_name}")
|
23
|
+
send("default_#{attribute.attribute_name}")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def define_default_for(attribute)
|
29
|
+
if attribute.respond_to?(:default_value)
|
30
|
+
define_method("default_#{attribute.attribute_name}") do
|
22
31
|
attribute.default_value
|
23
32
|
end
|
24
33
|
end
|
data/lib/clamp/parameter.rb
CHANGED
@@ -9,12 +9,19 @@ module Clamp
|
|
9
9
|
if options.has_key?(:attribute_name)
|
10
10
|
@attribute_name = options[:attribute_name].to_s
|
11
11
|
end
|
12
|
+
if options.has_key?(:default)
|
13
|
+
@default_value = options[:default]
|
14
|
+
end
|
12
15
|
end
|
13
16
|
|
14
|
-
attr_reader :name, :description, :attribute_name
|
17
|
+
attr_reader :name, :description, :attribute_name, :default_value
|
15
18
|
|
16
19
|
def help
|
17
|
-
|
20
|
+
rhs = description
|
21
|
+
if defined?(@default_value)
|
22
|
+
rhs += " (default: #{@default_value.inspect})"
|
23
|
+
end
|
24
|
+
[name, rhs]
|
18
25
|
end
|
19
26
|
|
20
27
|
def consume(arguments)
|
data/lib/clamp/version.rb
CHANGED
data/spec/clamp/command_spec.rb
CHANGED
@@ -79,20 +79,37 @@ describe Clamp::Command do
|
|
79
79
|
|
80
80
|
end
|
81
81
|
|
82
|
-
describe "with
|
82
|
+
describe "with default method" do
|
83
83
|
|
84
|
-
|
85
|
-
option "--
|
84
|
+
before do
|
85
|
+
@command.class.option "--port", "PORT", "port"
|
86
|
+
@command.class.class_eval do
|
87
|
+
def default_port
|
88
|
+
4321
|
89
|
+
end
|
90
|
+
end
|
86
91
|
end
|
87
92
|
|
88
93
|
it "sets the specified default value" do
|
89
|
-
@command.
|
94
|
+
@command.port.should == 4321
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "with :default value" do
|
100
|
+
|
101
|
+
before do
|
102
|
+
@command.class.option "--port", "PORT", "port to listen on", :default => 4321
|
103
|
+
end
|
104
|
+
|
105
|
+
it "declares default method" do
|
106
|
+
@command.default_port.should == 4321
|
90
107
|
end
|
91
108
|
|
92
109
|
describe "#help" do
|
93
110
|
|
94
111
|
it "describes the default value" do
|
95
|
-
@command.help.should include("
|
112
|
+
@command.help.should include("port to listen on (default: 4321)")
|
96
113
|
end
|
97
114
|
|
98
115
|
end
|
@@ -297,6 +314,26 @@ describe Clamp::Command do
|
|
297
314
|
|
298
315
|
end
|
299
316
|
|
317
|
+
describe "with :default value" do
|
318
|
+
|
319
|
+
before do
|
320
|
+
@command.class.parameter "[FLAVOUR]", "flavour of the month", :default => "chocolate"
|
321
|
+
end
|
322
|
+
|
323
|
+
it "sets the specified default value" do
|
324
|
+
@command.flavour.should == "chocolate"
|
325
|
+
end
|
326
|
+
|
327
|
+
describe "#help" do
|
328
|
+
|
329
|
+
it "describes the default value" do
|
330
|
+
@command.help.should include("flavour of the month (default: \"chocolate\")")
|
331
|
+
end
|
332
|
+
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
336
|
+
|
300
337
|
describe "with a block" do
|
301
338
|
|
302
339
|
before do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clamp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 6
|
10
|
+
version: 0.1.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mike Williams
|
@@ -15,12 +15,12 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-29 00:00:00 +11:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
22
22
|
description: |
|
23
|
-
Clamp
|
23
|
+
Clamp provides an object-model for command-line utilities.
|
24
24
|
It handles parsing of command-line options, and generation of usage help.
|
25
25
|
|
26
26
|
email: mdub@dogbiscuit.org
|
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
89
|
requirements: []
|
90
90
|
|
91
91
|
rubyforge_project:
|
92
|
-
rubygems_version: 1.
|
92
|
+
rubygems_version: 1.4.2
|
93
93
|
signing_key:
|
94
94
|
specification_version: 3
|
95
95
|
summary: a minimal framework for command-line utilities
|