clive 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{clive}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joshua Hawxwell"]
@@ -33,7 +33,8 @@ Gem::Specification.new do |s|
33
33
  "lib/clive/tokens.rb",
34
34
  "test/bin_test",
35
35
  "test/helper.rb",
36
- "test/test_clive.rb"
36
+ "test/test_clive.rb",
37
+ "test/test_exceptions.rb"
37
38
  ]
38
39
  s.homepage = %q{http://github.com/hawx/clive}
39
40
  s.rdoc_options = ["--charset=UTF-8"]
@@ -42,7 +43,8 @@ Gem::Specification.new do |s|
42
43
  s.summary = %q{Imagine if optparse and gli had a son called clive.}
43
44
  s.test_files = [
44
45
  "test/helper.rb",
45
- "test/test_clive.rb"
46
+ "test/test_clive.rb",
47
+ "test/test_exceptions.rb"
46
48
  ]
47
49
 
48
50
  if s.respond_to? :specification_version then
@@ -21,6 +21,49 @@ require 'clive/booleans'
21
21
  #
22
22
  class Clive
23
23
 
24
+ # general problem with input
25
+ class ParseError < StandardError
26
+ attr_accessor :args, :reason
27
+
28
+ def initialize(*args)
29
+ @args = args
30
+ @reason = "parse error"
31
+ end
32
+
33
+ def self.filter_backtrace(array)
34
+ unless $DEBUG
35
+ array = [$0]
36
+ end
37
+ array
38
+ end
39
+
40
+ def set_backtrace(array)
41
+ super(self.class.filter_backtrace(array))
42
+ end
43
+
44
+ def message
45
+ @reason + ': ' + args.join(' ')
46
+ end
47
+ alias_method :to_s, :message
48
+
49
+ end
50
+
51
+ # a flag has a missing argument
52
+ class MissingArgument < ParseError
53
+ def initialize(*args)
54
+ @args = args
55
+ @reason = "missing argument"
56
+ end
57
+ end
58
+
59
+ # a option that wasn't defined has been found
60
+ class InvalidOption < ParseError
61
+ def initialize(*args)
62
+ @args = args
63
+ @reason = "invalid option"
64
+ end
65
+ end
66
+
24
67
  attr_accessor :base
25
68
 
26
69
  def initialize(&block)
@@ -82,6 +82,7 @@ class Clive
82
82
  when :switch
83
83
  v.run
84
84
  when :flag
85
+ raise MissingArgument.new(v.long||v.short) unless i[2]
85
86
  v.run(i[2])
86
87
  when :argument
87
88
  r << v
@@ -138,7 +139,7 @@ class Clive
138
139
  tokens << [:flag, flag]
139
140
  pre -= [[k, v]] unless @base
140
141
  else
141
- raise "error, flag/switch '#{v}' does not exist" if @base
142
+ raise InvalidOption.new(v) if @base
142
143
  end
143
144
  when :word
144
145
  if tokens.last
@@ -1,10 +1,11 @@
1
- require 'rubygems'
2
1
  require 'test/unit'
3
2
  require 'shoulda'
3
+ require 'rr'
4
4
 
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
7
  require 'clive'
8
8
 
9
9
  class Test::Unit::TestCase
10
+ include RR::Adapters::TestUnit
10
11
  end
@@ -0,0 +1,28 @@
1
+ require 'helper'
2
+
3
+ class TestClive < Test::Unit::TestCase
4
+
5
+ context "When given bad input" do
6
+
7
+ should "warn of missing arguments" do
8
+ c = Clive.new do
9
+ flag(:hello) {}
10
+ end
11
+ assert_raise Clive::MissingArgument do
12
+ c.parse(["--hello"])
13
+ end
14
+ end
15
+
16
+ should "warn of invalid options" do
17
+ c = Clive.new do
18
+ switch(:hello) {}
19
+ end
20
+ assert_raise Clive::InvalidOption do
21
+ c.parse(["--what"])
22
+ end
23
+ end
24
+
25
+
26
+ end
27
+
28
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clive
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hawxwell
@@ -73,6 +73,7 @@ files:
73
73
  - test/bin_test
74
74
  - test/helper.rb
75
75
  - test/test_clive.rb
76
+ - test/test_exceptions.rb
76
77
  has_rdoc: true
77
78
  homepage: http://github.com/hawx/clive
78
79
  licenses: []
@@ -110,3 +111,4 @@ summary: Imagine if optparse and gli had a son called clive.
110
111
  test_files:
111
112
  - test/helper.rb
112
113
  - test/test_clive.rb
114
+ - test/test_exceptions.rb