main 2.0.0 → 2.1.0

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/README CHANGED
@@ -370,6 +370,36 @@ DOCS
370
370
  API section below
371
371
 
372
372
  HISTORY
373
+ 2.1.0
374
+ - added custom error handling dsl for parameters, this includes the ability
375
+ to prepend, append, or replace the standard error handlers:
376
+
377
+ require 'main'
378
+
379
+ Main {
380
+ argument 'x' do
381
+ error :before do
382
+ puts 'this fires *before* normal error handling using #instance_eval...'
383
+ end
384
+
385
+ error do
386
+ puts 'this fires *instead of* normal error handling using #instance_eval...'
387
+ end
388
+
389
+ error :after do
390
+ puts 'this fires *after* normal error handling using #instance_eval...'
391
+ end
392
+ end
393
+
394
+ run(){ p param['x'].given? }
395
+ }
396
+
397
+ - added ability to exit at any time bypassing *all* error handling using
398
+ 'throw :exit, 42' where 42 is the desired exit status. throw without a
399
+ status simply exits with 0.
400
+
401
+ - added 'help!' method which simply dumps out usage and exits
402
+
373
403
  2.0.0
374
404
  - removed need for proxy.rb via Main::Base.wrap_run!
375
405
  - added error handling hooks for parameter parsing
@@ -617,6 +647,20 @@ API
617
647
  # defaults for paramters of > 1 arity
618
648
  #
619
649
  defaults 40, 2
650
+ #
651
+ # you can add custom per-parameter error handlers using the following
652
+ #
653
+ error :before do
654
+ puts 'this fires *before* normal error handling using #instance_eval...'
655
+ end
656
+
657
+ error do
658
+ puts 'this fires *instead of* normal error handling using #instance_eval...'
659
+ end
660
+
661
+ error :after do
662
+ puts 'this fires *after* normal error handling using #instance_eval...'
663
+ end
620
664
  }
621
665
 
622
666
  ###########################################################################
data/README.tmpl CHANGED
@@ -186,6 +186,36 @@ DOCS
186
186
  API section below
187
187
 
188
188
  HISTORY
189
+ 2.1.0
190
+ - added custom error handling dsl for parameters, this includes the ability
191
+ to prepend, append, or replace the standard error handlers:
192
+
193
+ require 'main'
194
+
195
+ Main {
196
+ argument 'x' do
197
+ error :before do
198
+ puts 'this fires *before* normal error handling using #instance_eval...'
199
+ end
200
+
201
+ error do
202
+ puts 'this fires *instead of* normal error handling using #instance_eval...'
203
+ end
204
+
205
+ error :after do
206
+ puts 'this fires *after* normal error handling using #instance_eval...'
207
+ end
208
+ end
209
+
210
+ run(){ p param['x'].given? }
211
+ }
212
+
213
+ - added ability to exit at any time bypassing *all* error handling using
214
+ 'throw :exit, 42' where 42 is the desired exit status. throw without a
215
+ status simply exits with 0.
216
+
217
+ - added 'help!' method which simply dumps out usage and exits
218
+
189
219
  2.0.0
190
220
  - removed need for proxy.rb via Main::Base.wrap_run!
191
221
  - added error handling hooks for parameter parsing
@@ -433,6 +463,20 @@ API
433
463
  # defaults for paramters of > 1 arity
434
464
  #
435
465
  defaults 40, 2
466
+ #
467
+ # you can add custom per-parameter error handlers using the following
468
+ #
469
+ error :before do
470
+ puts 'this fires *before* normal error handling using #instance_eval...'
471
+ end
472
+
473
+ error do
474
+ puts 'this fires *instead of* normal error handling using #instance_eval...'
475
+ end
476
+
477
+ error :after do
478
+ puts 'this fires *after* normal error handling using #instance_eval...'
479
+ end
436
480
  }
437
481
 
438
482
  ###########################################################################
data/a.rb CHANGED
@@ -1,26 +1,21 @@
1
1
  require 'main'
2
2
 
3
- main(){
4
- argument('foo'){
5
- arity -1
6
- }
3
+ puts Main.version
7
4
 
8
- mode 'a' do
9
- argument('bar'){
10
- arity -1
11
- }
12
- run {
13
- foobar
14
- puts 'a'
15
- }
16
- end
5
+ Main {
6
+ argument 'x' do
7
+ error :before do
8
+ puts 'this fires *before* normal error handling using #instance_eval...'
9
+ end
17
10
 
18
- run {
19
- foobar
20
- puts 'no mode'
21
- }
11
+ error do
12
+ puts 'this fires *instead of* normal error handling using #instance_eval...'
13
+ end
22
14
 
23
- def foobar
24
- puts :foobar
15
+ error :after do
16
+ puts 'this fires *after* normal error handling using #instance_eval...'
17
+ end
25
18
  end
19
+
20
+ run(){ p param['x'].given? }
26
21
  }
data/gemspec.rb CHANGED
@@ -19,6 +19,9 @@ Gem::Specification::new do |spec|
19
19
  spec.has_rdoc = File::exist? "doc"
20
20
  spec.test_suite_file = "test/#{ lib }.rb" if File::directory? "test"
21
21
 
22
+ spec.add_dependency 'attributes', '>= 4.1.0'
23
+ spec.add_dependency 'arrayfields', '>= 4.3.0'
24
+
22
25
  spec.extensions << "extconf.rb" if File::exists? "extconf.rb"
23
26
 
24
27
  spec.author = "Ara T. Howard"
data/lib/main.rb CHANGED
@@ -2,7 +2,7 @@ module Main
2
2
  #
3
3
  # top level constants
4
4
  #
5
- Main::VERSION = '2.0.0' unless
5
+ Main::VERSION = '2.1.0' unless
6
6
  defined? Main::VERSION
7
7
  def self.version() Main::VERSION end
8
8
 
@@ -19,33 +19,28 @@ module Main
19
19
  require 'logger'
20
20
  require 'enumerator'
21
21
  require 'set'
22
-
23
22
  #
24
23
  # we try to use gems to pick up dependancies
25
24
  #
26
-
27
- =begin
28
25
  begin
29
26
  require 'rubygems'
30
27
  rescue LoadError
31
28
  42
32
29
  end
33
30
  begin
31
+ gem 'attributes', '~> 4.1.0'
34
32
  require 'attributes'
35
- rescue LoadError
33
+ rescue Exception
36
34
  require libdir + 'attributes'
37
35
  end
38
36
  begin
37
+ gem 'attributes', '~> 4.3.0'
39
38
  require 'arrayfields'
40
- rescue LoadError
39
+ rescue Exception
41
40
  require libdir + 'arrayfields'
42
41
  end
43
- =end
44
- require libdir + 'attributes'
45
- require libdir + 'arrayfields'
46
-
47
42
  #
48
- # main libs
43
+ # main's own libs
49
44
  #
50
45
  require libdir + 'stdext'
51
46
  require libdir + 'softspoken'
@@ -58,4 +53,3 @@ module Main
58
53
  require libdir + 'base'
59
54
  require libdir + 'factories'
60
55
  end
61
-
data/lib/main/base.rb CHANGED
@@ -6,28 +6,34 @@ module Main
6
6
 
7
7
  class_eval do
8
8
  def run *a, &b
9
- begin
10
- argv.push "--#{ argv.shift }" if argv.first == 'help'
9
+ status =
10
+ catch :exit do
11
+ begin
12
+ argv.push "--#{ argv.shift }" if argv.first == 'help'
11
13
 
12
- return mode_run! if mode_given?
14
+ return mode_run! if mode_given?
13
15
 
14
- parse_parameters
16
+ parse_parameters
15
17
 
16
- if params['help'] and params['help'].given?
17
- print usage.to_s
18
- exit
19
- end
18
+ if params['help'] and params['help'].given?
19
+ print usage.to_s
20
+ exit
21
+ end
20
22
 
21
- pre_run
23
+ pre_run
22
24
 
23
- self.class.const_get(:RUN).bind(self).call(*a, &b)
25
+ self.class.const_get(:RUN).bind(self).call(*a, &b)
24
26
 
25
- post_run
27
+ post_run
26
28
 
27
- finalize
28
- rescue Exception => e
29
- handle_exception e
30
- end
29
+ finalize
30
+ rescue Exception => e
31
+ handle_exception e
32
+ end
33
+ nil
34
+ end
35
+
36
+ handle_throw status
31
37
  end
32
38
  end
33
39
  end
@@ -322,20 +328,53 @@ module Main
322
328
  main.run
323
329
  end
324
330
 
331
+ def help! status = 0
332
+ print usage.to_s
333
+ exit(status)
334
+ end
335
+
325
336
  def handle_exception e
326
- if e.respond_to? :status
327
- exit_status(( e.status ))
337
+ if e.respond_to?(:before_handler)
338
+ fcall(e, :before_handler, self)
328
339
  end
329
340
 
330
- if Softspoken === e or SystemExit === e
331
- stderr.puts e.message unless(SystemExit === e and e.message.to_s == 'exit')
341
+ if e.respond_to?(:handler)
342
+ fcall(e, :handler, self)
332
343
  else
333
- fatal{ e }
344
+ if e.respond_to? :status
345
+ exit_status(( e.status ))
346
+ end
347
+
348
+ if Softspoken === e or SystemExit === e
349
+ stderr.puts e.message unless(SystemExit === e and e.message.to_s == 'exit')
350
+ else
351
+ fatal{ e }
352
+ end
353
+ end
354
+
355
+ if e.respond_to?(:after_handler)
356
+ fcall(e, :after_handler, self)
334
357
  end
335
358
 
336
359
  exit_status(( exit_failure )) if exit_status == exit_success
337
360
  exit_status(( Integer(exit_status) rescue(exit_status ? 0 : 1) ))
338
361
  exit exit_status
339
362
  end
363
+
364
+ def fcall obj, m, *argv, &block
365
+ m = obj.method m
366
+ arity = m.arity
367
+ if arity >= 0
368
+ argv = argv[0, arity]
369
+ else
370
+ arity = arity.abs - 1
371
+ argv = argv[0, arity] + argv[arity .. -1]
372
+ end
373
+ m.call *argv, &block
374
+ end
375
+
376
+ def handle_throw status
377
+ exit(( Integer(status) rescue 0 ))
378
+ end
340
379
  end
341
380
  end
@@ -69,6 +69,10 @@ module Main
69
69
  attribute 'arity' => 1
70
70
  attribute 'required' => false
71
71
 
72
+ attribute 'before_handler'
73
+ attribute 'error_handler'
74
+ attribute 'after_handler'
75
+
72
76
  def initialize name, *names, &block
73
77
  @names = Cast.list_of_string name, *names
74
78
  @names.map! do |name|
@@ -136,9 +140,11 @@ module Main
136
140
 
137
141
  def setup!
138
142
  return false unless given?
139
- check_arity
140
- apply_casting
141
- check_validation
143
+ adding_handlers do
144
+ check_arity
145
+ apply_casting
146
+ check_validation
147
+ end
142
148
  true
143
149
  end
144
150
 
@@ -182,6 +188,42 @@ module Main
182
188
  end
183
189
  end
184
190
 
191
+ def add_handlers e
192
+ sc =
193
+ class << e
194
+ self
195
+ end
196
+
197
+ this = self
198
+
199
+ if before_handler?
200
+ sc.module_eval do
201
+ define_method(:before_handler){|main| main.instance_eval &this.before_handler}
202
+ end
203
+ end
204
+
205
+ if error_handler?
206
+ sc.module_eval do
207
+ define_method(:handler){|main| main.instance_eval &this.error_handler}
208
+ end
209
+ end
210
+
211
+ if after_handler?
212
+ sc.module_eval do
213
+ define_method(:after_handler){|main| main.instance_eval &this.after_handler}
214
+ end
215
+ end
216
+ end
217
+
218
+ def adding_handlers
219
+ begin
220
+ yield
221
+ rescue Exception => e
222
+ add_handlers e
223
+ raise
224
+ end
225
+ end
226
+
185
227
  class Argument < Parameter
186
228
  attribute 'required' => true
187
229
 
@@ -385,8 +427,10 @@ module Main
385
427
 
386
428
  def validate!
387
429
  each do |p|
388
- next if p.arity == -1
389
- raise NotGiven, "#{ p.typename } not given" if(p.required? and (not p.given?))
430
+ p.adding_handlers do
431
+ next if p.arity == -1
432
+ raise NotGiven, "#{ p.typename } not given" if(p.required? and (not p.given?))
433
+ end
390
434
  end
391
435
  end
392
436
 
@@ -516,6 +560,10 @@ module Main
516
560
  def arity?
517
561
  p.arity?
518
562
  end
563
+
564
+ def error which = :error, &block
565
+ p.send "#{ which }_handler=", block
566
+ end
519
567
  end
520
568
 
521
569
  class Table < ::Array
data/main-2.0.0.gem ADDED
Binary file
data/test/main.rb CHANGED
@@ -41,6 +41,9 @@ class T < Test::Unit::TestCase
41
41
  raise
42
42
  end
43
43
  end
44
+
45
+ define_method :handle_throw do |*a|
46
+ end
44
47
  end
45
48
 
46
49
  main = klass.new argv, env
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: main
5
5
  version: !ruby/object:Gem::Version
6
- version: 2.0.0
7
- date: 2007-10-18 00:00:00 -06:00
6
+ version: 2.1.0
7
+ date: 2007-10-20 00:00:00 -06:00
8
8
  summary: main
9
9
  require_paths:
10
10
  - lib
@@ -50,6 +50,7 @@ files:
50
50
  - lib/main/usage.rb
51
51
  - lib/main/util.rb
52
52
  - lib/main.rb
53
+ - main-2.0.0.gem
53
54
  - README
54
55
  - README.tmpl
55
56
  - samples
@@ -61,7 +62,6 @@ files:
61
62
  - samples/f.rb
62
63
  - test
63
64
  - test/main.rb
64
- - TODO
65
65
  test_files:
66
66
  - test/main.rb
67
67
  rdoc_options: []
@@ -74,5 +74,22 @@ extensions: []
74
74
 
75
75
  requirements: []
76
76
 
77
- dependencies: []
78
-
77
+ dependencies:
78
+ - !ruby/object:Gem::Dependency
79
+ name: attributes
80
+ version_requirement:
81
+ version_requirements: !ruby/object:Gem::Version::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 4.1.0
86
+ version:
87
+ - !ruby/object:Gem::Dependency
88
+ name: arrayfields
89
+ version_requirement:
90
+ version_requirements: !ruby/object:Gem::Version::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 4.3.0
95
+ version:
data/TODO DELETED
@@ -1,2 +0,0 @@
1
- * error handling for parameter parsing - callback hooks
2
- * mode handling