clamp 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -221,7 +221,7 @@ You can mark a subcommand as "default" by using `default_subcommand` to declare
221
221
 
222
222
  ### Subcommand options and parameters
223
223
 
224
- Options are inheritable, so any options declared for a parent command are supported for it's subcommands. Parameters, on the other hand, are not inherited - each subcommand must declare it's own parameter list.
224
+ Options are inheritable, so any options declared for a command are supported for it's sub-classes (e.g. those created using `subcommand`). Parameters, on the other hand, are not inherited - each subcommand must declare it's own parameter list.
225
225
 
226
226
  Note that, if a subcommand accepts options, they must be specified on the command-line _after_ the subcommand name.
227
227
 
data/examples/gitdown ADDED
@@ -0,0 +1,61 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ # Demonstrate how subcommands can be declared as classes
4
+
5
+ require "clamp"
6
+
7
+ module GitDown
8
+
9
+ class AbstractCommand < Clamp::Command
10
+
11
+ option ["-v", "--verbose"], :flag, "be verbose"
12
+
13
+ option "--version", :flag, "show version" do
14
+ puts "GitDown-0.0.0a"
15
+ exit(0)
16
+ end
17
+
18
+ end
19
+
20
+ class CloneCommand < AbstractCommand
21
+
22
+ parameter "REPOSITORY", "repository to clone"
23
+ parameter "[DIR]", "working directory", :default => "."
24
+
25
+ def execute
26
+ raise NotImplementedError
27
+ end
28
+
29
+ end
30
+
31
+ class PullCommand < AbstractCommand
32
+
33
+ option "--[no-]commit", :flag, "Perform the merge and commit the result."
34
+
35
+ def execute
36
+ raise NotImplementedError
37
+ end
38
+
39
+ end
40
+
41
+ class StatusCommand < AbstractCommand
42
+
43
+ option ["-s", "--short"], :flag, "Give the output in the short-format."
44
+
45
+ def execute
46
+ raise NotImplementedError
47
+ end
48
+
49
+ end
50
+
51
+ class MainCommand < AbstractCommand
52
+
53
+ subcommand "clone", "Clone a remote repository.", CloneCommand
54
+ subcommand "pull", "Fetch and merge updates.", PullCommand
55
+ subcommand "status", "Display status of local repository.", StatusCommand
56
+
57
+ end
58
+
59
+ end
60
+
61
+ GitDown::MainCommand.run
@@ -27,16 +27,6 @@ module Clamp
27
27
  end
28
28
 
29
29
  def documented_options
30
- declared_options + inherited_declared_options
31
- end
32
-
33
- def recognised_options
34
- documented_options + standard_options
35
- end
36
-
37
- private
38
-
39
- def inherited_declared_options
40
30
  ancestors.inject([]) do |options, ancestor|
41
31
  if ancestor.kind_of?(Clamp::Option::Declaration)
42
32
  options + ancestor.declared_options
@@ -46,6 +36,10 @@ module Clamp
46
36
  end
47
37
  end
48
38
 
39
+ def recognised_options
40
+ documented_options + standard_options
41
+ end
42
+
49
43
  HELP_OPTION = Clamp::Option.new("--help", :flag, "print help", :attribute_name => :help_requested)
50
44
 
51
45
  def standard_options
data/lib/clamp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Clamp
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.2.1".freeze
3
3
  end
@@ -131,15 +131,22 @@ describe Clamp::Command do
131
131
 
132
132
  before do
133
133
 
134
+ speed_options = Module.new do
135
+ extend Clamp::Option::Declaration
136
+ option "--speed", "SPEED", "how fast", :default => "slowly"
137
+ end
138
+
134
139
  @command_class = Class.new(Clamp::Command) do
135
140
 
136
141
  option "--direction", "DIR", "which way", :default => "home"
137
142
 
143
+ include speed_options
144
+
138
145
  subcommand "move", "move in the appointed direction" do
139
146
 
140
147
  def execute
141
148
  motion = context[:motion] || "walking"
142
- puts "#{motion} #{direction}"
149
+ puts "#{motion} #{direction} #{speed}"
143
150
  end
144
151
 
145
152
  end
@@ -150,16 +157,21 @@ describe Clamp::Command do
150
157
 
151
158
  end
152
159
 
153
- it "accepts parents options (specified after the subcommand)" do
160
+ it "accepts options defined in superclass (specified after the subcommand)" do
154
161
  @command.run(["move", "--direction", "north"])
155
162
  stdout.should =~ /walking north/
156
163
  end
157
164
 
158
- it "accepts parents options (specified before the subcommand)" do
165
+ it "accepts options defined in superclass (specified before the subcommand)" do
159
166
  @command.run(["--direction", "north", "move"])
160
167
  stdout.should =~ /walking north/
161
168
  end
162
169
 
170
+ it "accepts options defined in included modules" do
171
+ @command.run(["move", "--speed", "very quickly"])
172
+ stdout.should =~ /walking home very quickly/
173
+ end
174
+
163
175
  it "has access to command context" do
164
176
  @command = @command_class.new("go", :motion => "wandering")
165
177
  @command.run(["move"])
@@ -122,5 +122,28 @@ describe Clamp::Option do
122
122
  end
123
123
 
124
124
  end
125
+
126
+ describe "in subcommand" do
127
+ before do
128
+
129
+ @command = Class.new(Clamp::Command) do
130
+ subcommand "foo", "FOO!" do
131
+ option "--bar", "BAR", "Bars foo."
132
+ end
133
+ end
134
+
135
+ end
136
+
137
+ describe "Command#help" do
138
+
139
+ it "includes help for each option exactly once" do
140
+ subcommand = @command.new("").send(:find_subcommand, 'foo')
141
+ subcommand_help = subcommand.subcommand_class.help("")
142
+ subcommand_help.lines.grep(/--bar BAR/).count.should == 1
143
+ end
144
+
145
+ end
146
+
147
+ end
125
148
 
126
- end
149
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: clamp
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Mike Williams
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-24 00:00:00 Z
13
+ date: 2011-06-27 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: |
@@ -32,6 +32,7 @@ files:
32
32
  - clamp.gemspec
33
33
  - examples/flipflop
34
34
  - examples/fubar
35
+ - examples/gitdown
35
36
  - examples/speak
36
37
  - lib/clamp.rb
37
38
  - lib/clamp/attribute.rb
@@ -68,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
69
  requirements:
69
70
  - - ">="
70
71
  - !ruby/object:Gem::Version
71
- hash: 1396886237993251491
72
+ hash: -3288603544677427312
72
73
  segments:
73
74
  - 0
74
75
  version: "0"
@@ -77,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
78
  requirements:
78
79
  - - ">="
79
80
  - !ruby/object:Gem::Version
80
- hash: 1396886237993251491
81
+ hash: -3288603544677427312
81
82
  segments:
82
83
  - 0
83
84
  version: "0"