cri 2.0a3 → 2.0b1

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
@@ -2,8 +2,20 @@
2
2
 
3
3
  module Cri
4
4
 
5
+ # @todo Document
6
+ class Error < ::StandardError
7
+ end
8
+
9
+ # @todo Document
10
+ class NotImplementedError < Error
11
+ end
12
+
13
+ # @todo Document
14
+ class NoHelpAvailableError < Error
15
+ end
16
+
5
17
  # The current Cri version.
6
- VERSION = '2.0a3'
18
+ VERSION = '2.0b1'
7
19
 
8
20
  autoload 'Command', 'cri/command'
9
21
  autoload 'CommandDSL', 'cri/command_dsl'
@@ -197,7 +197,8 @@ module Cri
197
197
 
198
198
  # Execute
199
199
  if @block.nil?
200
- raise "No implementation available for '#{self.name}'"
200
+ raise NotImplementedError,
201
+ "No implementation available for '#{self.name}'"
201
202
  end
202
203
  block.call(global_opts, args, self)
203
204
  else
@@ -10,13 +10,18 @@ commandline options. When a command is given, a command description as well as
10
10
  command-specific commandline options are shown.
11
11
  EOS
12
12
 
13
- run do |opts, args|
13
+ run do |opts, args, cmd|
14
+ if cmd.supercommand.nil?
15
+ raise NoHelpAvailableError,
16
+ "No help available because the help command has no supercommand"
17
+ end
18
+
14
19
  if args.empty?
15
- puts self.supercommand.help
20
+ puts cmd.supercommand.help
16
21
  elsif args.size == 1
17
- puts self.supercommand.command_named(args[0]).help
22
+ puts cmd.supercommand.command_named(args[0]).help
18
23
  else
19
- $stderr.puts self.usage
24
+ $stderr.puts cmd.usage
20
25
  exit 1
21
26
  end
22
27
  end
@@ -5,17 +5,13 @@ module Cri
5
5
  # Cri::OptionParser is used for parsing commandline options.
6
6
  class OptionParser
7
7
 
8
- # Superclass for generic option parser errors.
9
- class GenericError < StandardError
10
- end
11
-
12
8
  # Error that will be raised when an unknown option is encountered.
13
- class IllegalOptionError < Cri::OptionParser::GenericError
9
+ class IllegalOptionError < Cri::Error
14
10
  end
15
11
 
16
12
  # Error that will be raised when an option without argument is
17
13
  # encountered.
18
- class OptionRequiresAnArgumentError < Cri::OptionParser::GenericError
14
+ class OptionRequiresAnArgumentError < Cri::Error
19
15
  end
20
16
 
21
17
  # The delegate to which events will be sent. The following methods will
@@ -4,23 +4,41 @@ require 'stringio'
4
4
 
5
5
  class Cri::TestCase < MiniTest::Unit::TestCase
6
6
 
7
- def capture_io_while(&block)
8
- $orig_stdout = $stdout
9
- $orig_stderr = $stderr
7
+ def setup
8
+ @orig_io = capture_io
9
+ end
10
10
 
11
- $stdout = StringIO.new
12
- $stderr = StringIO.new
11
+ def teardown
12
+ uncapture_io(*@orig_io)
13
+ end
13
14
 
15
+ def capture_io_while(&block)
16
+ orig_io = capture_io
14
17
  block.call
15
-
16
18
  [ $stdout.string, $stderr.string ]
17
19
  ensure
18
- $stdout = $orig_stdout
19
- $stderr = $orig_stderr
20
+ uncapture_io(*orig_io)
20
21
  end
21
22
 
22
23
  def lines(string)
23
24
  string.scan(/^.*\n/).map { |s| s.chomp }
24
25
  end
25
26
 
27
+ private
28
+
29
+ def capture_io
30
+ orig_stdout = $stdout
31
+ orig_stderr = $stderr
32
+
33
+ $stdout = StringIO.new
34
+ $stderr = StringIO.new
35
+
36
+ [ orig_stdout, orig_stderr ]
37
+ end
38
+
39
+ def uncapture_io(orig_stdout, orig_stderr)
40
+ $stdout = orig_stdout
41
+ $stderr = orig_stderr
42
+ end
43
+
26
44
  end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ class Cri::BasicHelpTestCase < Cri::TestCase
4
+
5
+ def test_run_without_supercommand
6
+ cmd = Cri::Command.new_basic_help
7
+
8
+ assert_raises Cri::NoHelpAvailableError do
9
+ cmd.run([])
10
+ end
11
+ end
12
+
13
+ def test_run_with_supercommand
14
+ cmd = Cri::Command.define do
15
+ name 'meh'
16
+ end
17
+
18
+ help_cmd = Cri::Command.new_basic_help
19
+ cmd.add_command(help_cmd)
20
+
21
+ help_cmd.run([])
22
+ end
23
+
24
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cri
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 3
5
- version: 2.0a3
5
+ version: 2.0b1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Denis Defreyne
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-20 00:00:00 Z
13
+ date: 2011-06-22 00:00:00 Z
14
14
  dependencies: []
15
15
 
16
16
  description: Cri allows building easy-to-use commandline interfaces with support for subcommands.
@@ -40,6 +40,7 @@ files:
40
40
  - lib/cri.rb
41
41
  - test/helper.rb
42
42
  - test/test_base.rb
43
+ - test/test_basic_help.rb
43
44
  - test/test_command.rb
44
45
  - test/test_command_dsl.rb
45
46
  - test/test_core_ext.rb