cri 2.0a2 → 2.0a3

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/lib/cri.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module Cri
4
4
 
5
5
  # The current Cri version.
6
- VERSION = '2.0a2'
6
+ VERSION = '2.0a3'
7
7
 
8
8
  autoload 'Command', 'cri/command'
9
9
  autoload 'CommandDSL', 'cri/command_dsl'
@@ -76,10 +76,12 @@ module Cri
76
76
  # @todo Document
77
77
  def self.define(string=nil, &block)
78
78
  dsl = Cri::CommandDSL.new
79
- if block
79
+ if string
80
+ dsl.instance_eval(string)
81
+ elsif block.arity == 0
80
82
  dsl.instance_eval(&block)
81
83
  else
82
- dsl.instance_eval(string)
84
+ block.call(dsl)
83
85
  end
84
86
  dsl.command
85
87
  end
@@ -105,7 +107,11 @@ module Cri
105
107
  # @todo Document
106
108
  def modify(&block)
107
109
  dsl = Cri::CommandDSL.new(self)
108
- dsl.instance_eval(&block)
110
+ if block.arity == 0
111
+ dsl.instance_eval(&block)
112
+ else
113
+ block.call(dsl)
114
+ end
109
115
  self
110
116
  end
111
117
 
@@ -128,7 +134,11 @@ module Cri
128
134
  # Execute DSL
129
135
  dsl = Cri::CommandDSL.new
130
136
  dsl.name name unless name.nil?
131
- dsl.instance_eval(&block)
137
+ if block.arity == 0
138
+ dsl.instance_eval(&block)
139
+ else
140
+ block.call(dsl)
141
+ end
132
142
 
133
143
  # Create command
134
144
  cmd = dsl.command
@@ -189,7 +199,7 @@ module Cri
189
199
  if @block.nil?
190
200
  raise "No implementation available for '#{self.name}'"
191
201
  end
192
- self.instance_exec(global_opts, args, &block)
202
+ block.call(global_opts, args, self)
193
203
  else
194
204
  # Parse up to command name
195
205
  stuff = partition(opts_and_args)
@@ -284,7 +294,7 @@ module Cri
284
294
  opts.each_pair do |key, value|
285
295
  opt_def = global_option_definitions.find { |o| o[:long] == key.to_s }
286
296
  block = opt_def[:block]
287
- self.instance_exec(value, &block) if block
297
+ block.call(value, self) if block
288
298
  end
289
299
  end
290
300
 
@@ -73,9 +73,9 @@ module Cri
73
73
 
74
74
  # @todo Document
75
75
  def run(&block)
76
- if block.arity != 2
76
+ unless block.arity != 2 || block.arity != 3
77
77
  raise ArgumentError,
78
- "The block given to Cri::Command#run expects exactly two args"
78
+ "The block given to Cri::Command#run expects two or three args"
79
79
  end
80
80
 
81
81
  @command.block = block
@@ -9,16 +9,16 @@ class Cri::CommandTestCase < Cri::TestCase
9
9
  summary 'does stuff'
10
10
  description 'This command does a lot of stuff.'
11
11
 
12
- option :a, :aaa, 'opt a', :argument => :optional do |value|
13
- $stdout.puts "#{name}:#{value}"
12
+ option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
13
+ $stdout.puts "#{cmd.name}:#{value}"
14
14
  end
15
15
  required :b, :bbb, 'opt b'
16
16
  optional :c, :ccc, 'opt c'
17
17
  flag :d, :ddd, 'opt d'
18
18
  forbidden :e, :eee, 'opt e'
19
19
 
20
- run do |opts, args|
21
- $stdout.puts "Awesome #{name}!"
20
+ run do |opts, args, c|
21
+ $stdout.puts "Awesome #{c.name}!"
22
22
 
23
23
  $stdout.puts args.join(',')
24
24
 
@@ -45,8 +45,8 @@ class Cri::CommandTestCase < Cri::TestCase
45
45
  summary 'does super stuff'
46
46
  description 'This command does super stuff.'
47
47
 
48
- option :a, :aaa, 'opt a', :argument => :optional do |value|
49
- $stdout.puts "#{name}:#{value}"
48
+ option :a, :aaa, 'opt a', :argument => :optional do |value, cmd|
49
+ $stdout.puts "#{cmd.name}:#{value}"
50
50
  end
51
51
  required :b, :bbb, 'opt b'
52
52
  optional :c, :ccc, 'opt c'
@@ -220,7 +220,20 @@ class Cri::CommandTestCase < Cri::TestCase
220
220
  bare_cmd.help
221
221
  end
222
222
 
223
- def test_modify
223
+ def test_modify_with_block_argument
224
+ cmd = Cri::Command.define do |c|
225
+ c.name 'build'
226
+ end
227
+ assert_equal 'build', cmd.name
228
+
229
+ cmd.modify do |c|
230
+ c.name 'compile'
231
+ end
232
+
233
+ assert_equal 'compile', cmd.name
234
+ end
235
+
236
+ def test_modify_without_block_argument
224
237
  cmd = Cri::Command.define do
225
238
  name 'build'
226
239
  end
@@ -248,4 +261,38 @@ class Cri::CommandTestCase < Cri::TestCase
248
261
  assert_equal 'help', cmd.subcommands.to_a[0].name
249
262
  end
250
263
 
264
+ def test_define_with_block_argument
265
+ cmd = Cri::Command.define do |c|
266
+ c.name 'moo'
267
+ end
268
+
269
+ assert_equal 'moo', cmd.name
270
+ end
271
+
272
+ def test_define_without_block_argument
273
+ cmd = Cri::Command.define do
274
+ name 'moo'
275
+ end
276
+
277
+ assert_equal 'moo', cmd.name
278
+ end
279
+
280
+ def test_define_subcommand_with_block_argument
281
+ cmd = bare_cmd
282
+ cmd.define_command do |c|
283
+ c.name 'baresub'
284
+ end
285
+
286
+ assert_equal 'baresub', cmd.subcommands.to_a[0].name
287
+ end
288
+
289
+ def test_define_subcommand_without_block_argument
290
+ cmd = bare_cmd
291
+ cmd.define_command do
292
+ name 'baresub'
293
+ end
294
+
295
+ assert_equal 'baresub', cmd.subcommands.to_a[0].name
296
+ end
297
+
251
298
  end
@@ -5,7 +5,7 @@ class Cri::CommandDSLTestCase < Cri::TestCase
5
5
  def test_create_command
6
6
  # Define
7
7
  dsl = Cri::CommandDSL.new
8
- dsl.instance_eval do
8
+ dsl.instance_eval do
9
9
  name 'moo'
10
10
  usage 'dunno whatever'
11
11
  summary 'does stuff'
@@ -51,8 +51,8 @@ class Cri::CommandDSLTestCase < Cri::TestCase
51
51
  dsl = Cri::CommandDSL.new
52
52
  dsl.instance_eval do
53
53
  name 'super'
54
- subcommand do
55
- name 'sub'
54
+ subcommand do |c|
55
+ c.name 'sub'
56
56
  end
57
57
  end
58
58
  command = dsl.command
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cri
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 3
5
- version: 2.0a2
5
+ version: 2.0a3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Denis Defreyne