clamp 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ module Clamp
2
+
3
+ class Attribute
4
+
5
+ attr_reader :description, :attribute_name, :default_value
6
+
7
+ def help_rhs
8
+ rhs = description
9
+ if defined?(@default_value)
10
+ rhs += " (default: #{@default_value.inspect})"
11
+ end
12
+ rhs
13
+ end
14
+
15
+ def help
16
+ [help_lhs, help_rhs]
17
+ end
18
+
19
+ def ivar_name
20
+ "@#{attribute_name}"
21
+ end
22
+
23
+ def read_method
24
+ attribute_name
25
+ end
26
+
27
+ def default_method
28
+ "default_#{read_method}"
29
+ end
30
+
31
+ def write_method
32
+ "#{attribute_name}="
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -11,34 +11,27 @@ module Clamp
11
11
  end
12
12
 
13
13
  def define_reader_for(attribute)
14
- reader_name = attribute.attribute_name
15
- reader_name += "?" if attribute.respond_to?(:flag?) && attribute.flag?
16
- ivar_name = "@#{attribute.attribute_name}"
17
- define_method(reader_name) do
18
- if instance_variable_defined?(ivar_name)
19
- instance_variable_get(ivar_name)
20
- elsif parent_command && parent_command.respond_to?(reader_name)
21
- parent_command.send(reader_name)
22
- elsif respond_to?("default_#{attribute.attribute_name}")
23
- send("default_#{attribute.attribute_name}")
14
+ define_method(attribute.read_method) do
15
+ if instance_variable_defined?(attribute.ivar_name)
16
+ instance_variable_get(attribute.ivar_name)
17
+ elsif respond_to?(attribute.default_method)
18
+ send(attribute.default_method)
24
19
  end
25
20
  end
26
21
  end
27
22
 
28
23
  def define_default_for(attribute)
29
- if attribute.respond_to?(:default_value)
30
- define_method("default_#{attribute.attribute_name}") do
31
- attribute.default_value
32
- end
24
+ define_method(attribute.default_method) do
25
+ attribute.default_value
33
26
  end
34
27
  end
35
28
 
36
29
  def define_writer_for(attribute, &block)
37
- define_method("#{attribute.attribute_name}=") do |value|
30
+ define_method(attribute.write_method) do |value|
38
31
  if block
39
32
  value = instance_exec(value, &block)
40
33
  end
41
- instance_variable_set("@#{attribute.attribute_name}", value)
34
+ instance_variable_set(attribute.ivar_name, value)
42
35
  end
43
36
  end
44
37
 
data/lib/clamp/command.rb CHANGED
@@ -88,7 +88,6 @@ module Clamp
88
88
  protected
89
89
 
90
90
  attr_accessor :context
91
- attr_accessor :parent_command
92
91
 
93
92
  private
94
93
 
data/lib/clamp/option.rb CHANGED
@@ -1,6 +1,8 @@
1
+ require 'clamp/attribute'
2
+
1
3
  module Clamp
2
4
 
3
- class Option
5
+ class Option < Attribute
4
6
 
5
7
  def initialize(switches, type, description, options = {})
6
8
  @switches = Array(switches)
@@ -14,7 +16,7 @@ module Clamp
14
16
  end
15
17
  end
16
18
 
17
- attr_reader :switches, :type, :description, :default_value
19
+ attr_reader :switches, :type
18
20
 
19
21
  def attribute_name
20
22
  @attribute_name ||= long_switch.sub(/^--(\[no-\])?/, '').tr('-', '_')
@@ -35,6 +37,14 @@ module Clamp
35
37
  def flag_value(switch)
36
38
  !(switch =~ /^--no-(.*)/ && switches.member?("--\[no-\]#{$1}"))
37
39
  end
40
+
41
+ def read_method
42
+ if flag?
43
+ super + "?"
44
+ else
45
+ super
46
+ end
47
+ end
38
48
 
39
49
  def extract_value(switch, arguments)
40
50
  if flag?
@@ -44,14 +54,10 @@ module Clamp
44
54
  end
45
55
  end
46
56
 
47
- def help
57
+ def help_lhs
48
58
  lhs = switches.join(", ")
49
59
  lhs += " " + type unless flag?
50
- rhs = description
51
- if defined?(@default_value)
52
- rhs += " (default: #{@default_value.inspect})"
53
- end
54
- [lhs, rhs]
60
+ lhs
55
61
  end
56
62
 
57
63
  private
@@ -22,8 +22,6 @@ module Clamp
22
22
  recognised_options.find { |o| o.handles?(switch) }
23
23
  end
24
24
 
25
- protected
26
-
27
25
  def declared_options
28
26
  my_declared_options + inherited_declared_options
29
27
  end
@@ -1,6 +1,8 @@
1
+ require 'clamp/attribute'
2
+
1
3
  module Clamp
2
4
 
3
- class Parameter
5
+ class Parameter < Attribute
4
6
 
5
7
  def initialize(name, description, options = {})
6
8
  @name = name
@@ -14,14 +16,10 @@ module Clamp
14
16
  end
15
17
  end
16
18
 
17
- attr_reader :name, :description, :attribute_name, :default_value
19
+ attr_reader :name, :attribute_name
18
20
 
19
- def help
20
- rhs = description
21
- if defined?(@default_value)
22
- rhs += " (default: #{@default_value.inspect})"
23
- end
24
- [name, rhs]
21
+ def help_lhs
22
+ name
25
23
  end
26
24
 
27
25
  def consume(arguments)
@@ -9,7 +9,12 @@ module Clamp
9
9
  signal_usage_error "no subcommand specified" unless subcommand_name
10
10
  subcommand_class = find_subcommand_class(subcommand_name)
11
11
  subcommand = subcommand_class.new("#{invocation_path} #{subcommand_name}", context)
12
- subcommand.parent_command = self
12
+ self.class.declared_options.each do |option|
13
+ option_set = instance_variable_defined?(option.ivar_name)
14
+ if option_set && subcommand.respond_to?(option.write_method)
15
+ subcommand.send(option.write_method, self.send(option.read_method))
16
+ end
17
+ end
13
18
  subcommand.run(subcommand_arguments)
14
19
  end
15
20
 
data/lib/clamp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Clamp
2
- VERSION = "0.1.6".freeze
2
+ VERSION = "0.1.7".freeze
3
3
  end
@@ -118,39 +118,37 @@ describe Clamp::Command do
118
118
 
119
119
  @command_class = Class.new(Clamp::Command) do
120
120
 
121
- option "--direction", "DIR", "which way"
121
+ option "--direction", "DIR", "which way", :default => "home"
122
122
 
123
- subcommand "walk", "step carefully in the appointed direction" do
123
+ subcommand "move", "move in the appointed direction" do
124
124
 
125
125
  def execute
126
- if direction
127
- puts "walking #{direction}"
128
- else
129
- puts "wandering #{context[:default_direction]} by default"
130
- end
126
+ motion = context[:motion] || "walking"
127
+ puts "#{motion} #{direction}"
131
128
  end
132
129
 
133
130
  end
134
131
 
135
132
  end
136
133
 
137
- @command = @command_class.new("go", :default_direction => "south")
134
+ @command = @command_class.new("go")
138
135
 
139
136
  end
140
137
 
141
138
  it "accepts parents options (specified after the subcommand)" do
142
- @command.run(["walk", "--direction", "north"])
139
+ @command.run(["move", "--direction", "north"])
143
140
  stdout.should =~ /walking north/
144
141
  end
145
142
 
146
143
  it "accepts parents options (specified before the subcommand)" do
147
- @command.run(["--direction", "north", "walk"])
144
+ @command.run(["--direction", "north", "move"])
148
145
  stdout.should =~ /walking north/
149
146
  end
150
147
 
151
148
  it "has access to command context" do
152
- @command.run(["walk"])
153
- stdout.should =~ /wandering south by default/
149
+ @command = @command_class.new("go", :motion => "wandering")
150
+ @command.run(["move"])
151
+ stdout.should =~ /wandering home/
154
152
  end
155
153
 
156
154
  end
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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 7
10
+ version: 0.1.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Williams
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-29 00:00:00 +11:00
18
+ date: 2011-01-30 00:00:00 +11:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -40,6 +40,7 @@ files:
40
40
  - examples/fubar
41
41
  - examples/speak
42
42
  - lib/clamp.rb
43
+ - lib/clamp/attribute.rb
43
44
  - lib/clamp/attribute_declaration.rb
44
45
  - lib/clamp/command.rb
45
46
  - lib/clamp/errors.rb