clive 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -120,6 +120,21 @@ Anything that isn't a command, switch or flag is taken as an argument. These are
120
120
  my_file --size big /usr/bin
121
121
  #=> ["/usr/bin"]
122
122
 
123
+
124
+ ### Error Handling
125
+
126
+ You are able to intercept errors when an option does not exist in a similar way to `method_missing`.
127
+
128
+ c = Clive.new do
129
+ option_missing do |name|
130
+ puts "#{name} was used but not defined"
131
+ end
132
+ end
133
+ c.parse("--hey")
134
+ #=> hey was used but not defined
135
+
136
+ I was hoping to provide a similar way of intercepting commands as well but these could also be arguments which means it could result in unexpected results. For this reason I will not be implementing `command_missing`.
137
+
123
138
  ### Putting It All Together
124
139
 
125
140
  require 'clive'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
data/clive.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{clive}
8
- s.version = "0.4.0"
8
+ s.version = "0.4.1"
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"]
12
- s.date = %q{2010-09-19}
12
+ s.date = %q{2010-10-25}
13
13
  s.description = %q{Clive is a DSL for creating a command line interface. It is for people who, like me, love OptionParser's syntax and love GLI's commands.}
14
14
  s.email = %q{m@hawx.me}
15
15
  s.extra_rdoc_files = [
data/lib/clive/command.rb CHANGED
@@ -29,6 +29,8 @@ class Clive
29
29
  @base = false
30
30
  @commands = Clive::Array.new
31
31
  @options = Clive::Array.new
32
+
33
+ @option_missing = Proc.new {|e| raise NoOptionError.new(e)}
32
34
 
33
35
  if args.length == 1 && args[0] == true
34
36
  @base = true
@@ -183,15 +185,20 @@ class Clive
183
185
  r << [:argument, v]
184
186
  end
185
187
  else
186
- raise InvalidOption.new(v)
188
+ @option_missing.call(v)
187
189
  end
188
190
  end
189
191
  end
190
192
  r
191
193
  end
194
+
195
+ # @group Missing Helpers
196
+
197
+ def option_missing(&block)
198
+ @option_missing = block
199
+ end
192
200
 
193
-
194
- #### CREATION HELPERS ####
201
+ # @group Creators
195
202
 
196
203
  # Add a new command to +@commands+
197
204
  #
@@ -230,7 +237,7 @@ class Clive
230
237
  end
231
238
  alias_method :boolean, :bool
232
239
 
233
- #### HELP STUFF ####
240
+ # @group Help
234
241
 
235
242
  # This actually creates a switch with "-h" and "--help" that controls
236
243
  # the help on this command.
@@ -31,6 +31,11 @@ class Clive
31
31
  def reason; "parse error"; end
32
32
  end
33
33
 
34
+ # Long name is missing for bool
35
+ class MissingLongName < CliveError
36
+ def reason; "missing long name"; end
37
+ end
38
+
34
39
  # A flag has a missing argument
35
40
  class MissingArgument < ParseError
36
41
  def reason; "missing argument"; end
@@ -42,13 +47,8 @@ class Clive
42
47
  end
43
48
 
44
49
  # An option that wasn't defined has been found
45
- class InvalidOption < ParseError
50
+ class NoOptionError < ParseError
46
51
  def reason; "invalid option"; end
47
52
  end
48
53
 
49
- # Long name is missing for bool
50
- class MissingLongName < CliveError
51
- def reason; "missing long name"; end
52
- end
53
-
54
54
  end
data/test/bin_test CHANGED
@@ -5,46 +5,51 @@ require 'clive'
5
5
  options = {}
6
6
  c = Clive.new do
7
7
 
8
- switch(:v, :verbose, "Run verbosely") {
8
+ switch :v, :verbose, "Run verbosely" do
9
9
  options[:verbose] = true
10
- }
10
+ end
11
11
 
12
- flag(:t, :type, "TYPE", "Change type to TYPE") do |i|
12
+ flag :t, :type, "TYPE", "Change type to TYPE" do |i|
13
13
  options[:type] = i
14
14
  end
15
15
 
16
16
  flag(:long_only, "A flag with only a long") {}
17
17
 
18
- flag(:p, :print, "[TEXT]", "Print TEXT, defaults to 'hey'") do |i|
18
+ flag :p, :print, "[TEXT]", "Print TEXT, defaults to 'hey'" do |i|
19
19
  i ||= 'hey'
20
20
  p i
21
21
  end
22
22
 
23
23
  flag(:z, :apple, "More applez") {}
24
24
 
25
- command(:add, "Add something") {
25
+ command :add, "Add something" do
26
26
 
27
27
  header "Usage: bin_test add [options]\n" +
28
28
  "Use add to add things to add, which in turn can be added."
29
29
 
30
- switch(:f, :full, "Use full") {
30
+ switch :f, :full, "Use full" do
31
31
  options[:full] = true
32
- }
32
+ end
33
33
 
34
34
  # Use with app-name add -e
35
- switch(:e, :empty, "Use empty") {
35
+ switch :e, :empty, "Use empty" do
36
36
  options[:full] = false
37
- }
37
+ end
38
38
 
39
39
  footer "If the header was a little confusing, don't worry the footer thought so too."
40
40
 
41
- }
41
+ end
42
42
 
43
- command(:remove, :delete, "Get rid of something") {
44
- switch(:f, :force, "Force") {
43
+ command :remove, :delete, "Get rid of something" do
44
+ switch :f, :force, "Force" do
45
45
  p "deleting! now"
46
- }
47
- }
46
+ end
47
+ end
48
+
49
+
50
+ option_missing do |name|
51
+ puts "hi, #{name}"
52
+ end
48
53
 
49
54
  end
50
55
 
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clive
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 4
9
- - 0
10
- version: 0.4.0
8
+ - 1
9
+ version: 0.4.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - Joshua Hawxwell
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-09-19 00:00:00 +01:00
17
+ date: 2010-10-25 00:00:00 +01:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
@@ -40,7 +38,6 @@ dependencies:
40
38
  requirements:
41
39
  - - ">="
42
40
  - !ruby/object:Gem::Version
43
- hash: 3
44
41
  segments:
45
42
  - 0
46
43
  version: "0"
@@ -95,7 +92,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
92
  requirements:
96
93
  - - ">="
97
94
  - !ruby/object:Gem::Version
98
- hash: 3
99
95
  segments:
100
96
  - 0
101
97
  version: "0"
@@ -104,7 +100,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
100
  requirements:
105
101
  - - ">="
106
102
  - !ruby/object:Gem::Version
107
- hash: 3
108
103
  segments:
109
104
  - 0
110
105
  version: "0"