main 0.0.1 → 0.0.2

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.
@@ -1,14 +1,15 @@
1
1
  module Main
2
2
  def self.new *a, &b
3
- m = Class.new Base
4
- m.default_options!
5
- m.class_eval &b if b
6
- m.new
3
+ klass = Class.new Base
4
+ klass.default_options!
5
+ klass.class_eval &b if b
6
+ main = klass.new *a, &b
7
+ Proxy.new main
7
8
  end
8
9
 
9
10
  module ::Kernel
10
11
  def Main *a, &b
11
- ::Main.new(*a, &b).__run__
12
+ ::Main.new(*a, &b).run
12
13
  end
13
14
  alias_method 'main', 'Main'
14
15
  end
@@ -354,7 +354,8 @@ module Main
354
354
  #p.add_value p.default if(p.default? and (not p.given?))
355
355
  if(p.defaults? and (not p.given?))
356
356
  p.defaults.each do |default|
357
- p.add_value default
357
+ #p.add_value default
358
+ p.values << default # so as NOT to set given?
358
359
  end
359
360
  end
360
361
  end
data/lib/main/proxy.rb ADDED
@@ -0,0 +1,54 @@
1
+ module Main
2
+ class Proxy
3
+ instance_methods.each{|m| undef_method m unless m[%r/__/]}
4
+
5
+ attribute 'main'
6
+
7
+ def initialize main
8
+ @main = main
9
+ end
10
+
11
+ def as_main &b
12
+ @main.instance_eval &b
13
+ end
14
+
15
+ def run
16
+ as_main do
17
+ parse_parameters
18
+
19
+ if params['help'] and params['help'].given?
20
+ print usage.to_s
21
+ exit
22
+ end
23
+
24
+ pre_run
25
+
26
+ begin
27
+ run
28
+ rescue Exception => e
29
+ if SystemExit === e
30
+ exit_status(( e.status ))
31
+ else
32
+ fatal{ e }
33
+ exit_status(( EXIT_FAILURE )) if exit_status == exit_success
34
+ end
35
+ end
36
+
37
+ post_run
38
+
39
+ finalize
40
+
41
+ exit_status(( Integer(exit_status) rescue(exit_status ? 0 : 1) ))
42
+ exit exit_status
43
+ end
44
+ end
45
+
46
+ def method_missing m, *a, &b
47
+ if @main.respond_to? m
48
+ @main.send m, *a, &b
49
+ else
50
+ raise
51
+ end
52
+ end
53
+ end
54
+ end
data/test/main.rb CHANGED
@@ -29,7 +29,7 @@ class T < Test::Unit::TestCase
29
29
  begin
30
30
  main = Object::Main.new(&b)
31
31
  main.logger = @logger
32
- main.__run__
32
+ main.run
33
33
  rescue Exception => e
34
34
  if SystemExit === e
35
35
  @status = e.status
@@ -517,6 +517,121 @@ class T < Test::Unit::TestCase
517
517
  assert u['name2'] == nil
518
518
  }
519
519
  end
520
+ #
521
+ # io redirection
522
+ #
523
+ class ::Object
524
+ require 'tempfile'
525
+ def infile buf
526
+ t = Tempfile.new rand.to_s
527
+ t << buf
528
+ t.close
529
+ open t.path, 'r+'
530
+ end
531
+ def outfile
532
+ t = Tempfile.new rand.to_s
533
+ t.close
534
+ open t.path, 'w+'
535
+ end
536
+ end
537
+ def test_0330
538
+ s = "foo\nbar\n"
539
+ sio = StringIO.new s
540
+ $buf = nil
541
+ assert_nothing_raised{
542
+ main{
543
+ stdin sio
544
+ def run
545
+ $buf = STDIN.read
546
+ end
547
+ }
548
+ }
549
+ assert $buf == s
550
+ end
551
+ def test_0340
552
+ s = "foo\nbar\n"
553
+ $sio = StringIO.new s
554
+ $buf = nil
555
+ assert_nothing_raised{
556
+ main{
557
+ def run
558
+ self.stdin = $sio
559
+ $buf = STDIN.read
560
+ end
561
+ }
562
+ }
563
+ assert $buf == s
564
+ end
565
+ def test_0350
566
+ s = "foo\nbar\n"
567
+ $buf = nil
568
+ assert_nothing_raised{
569
+ main{
570
+ stdin infile(s)
571
+ def run
572
+ $buf = STDIN.read
573
+ end
574
+ }
575
+ }
576
+ assert $buf == s
577
+ end
578
+ def test_0360
579
+ sout = outfile
580
+ assert_nothing_raised{
581
+ main{
582
+ stdout sout
583
+ def run
584
+ puts 42
585
+ end
586
+ }
587
+ }
588
+ assert test(?e, sout.path)
589
+ assert IO.read(sout.path) == "42\n"
590
+ end
591
+ def test_0370
592
+ m = nil
593
+ assert_nothing_raised{
594
+ m = main{
595
+ stdout StringIO.new
596
+ def run
597
+ puts 42
598
+ end
599
+ }
600
+ }
601
+ assert m
602
+ assert_nothing_raised{ m.stdout.rewind }
603
+ assert m.stdout.read == "42\n"
604
+ end
605
+ #
606
+ # main ctor
607
+ #
608
+ def test_0380
609
+ argv = %w( a b c )
610
+ $argv = nil
611
+ assert_nothing_raised{
612
+ main(argv){
613
+ def run
614
+ $argv = @argv
615
+ end
616
+ }
617
+ }
618
+ assert argv == $argv
619
+ end
620
+ def test_0390
621
+ argv = %w( a b c )
622
+ env = {'key' => 'val', 'foo' => 'bar'}
623
+ $argv = nil
624
+ $env = nil
625
+ assert_nothing_raised{
626
+ main(argv, env){
627
+ def run
628
+ $argv = @argv
629
+ $env = @env
630
+ end
631
+ }
632
+ }
633
+ assert argv == $argv
634
+ end
520
635
 
521
636
  end
522
637
 
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: 0.0.1
7
- date: 2007-03-20 00:00:00 -06:00
6
+ version: 0.0.2
7
+ date: 2007-03-23 00:00:00 -06:00
8
8
  summary: main
9
9
  require_paths:
10
10
  - lib
@@ -29,7 +29,6 @@ post_install_message:
29
29
  authors:
30
30
  - Ara T. Howard
31
31
  files:
32
- - a.rb
33
32
  - lib
34
33
  - lib/main
35
34
  - lib/main/factories.rb
@@ -39,13 +38,15 @@ files:
39
38
  - lib/main/usage.rb
40
39
  - lib/main/base.rb
41
40
  - lib/main/getoptlong.rb
41
+ - lib/main/proxy.rb
42
+ - lib/main/attributes.rb
43
+ - lib/main/arrayfields.rb
42
44
  - lib/main.rb
43
45
  - test
44
46
  - test/main.rb
45
47
  - gemspec.rb
46
48
  - README.tmpl
47
49
  - README
48
- - main-0.0.1.gem
49
50
  - gen_readme.rb
50
51
  - install.rb
51
52
  - samples
data/a.rb DELETED
@@ -1,33 +0,0 @@
1
- require 'main'
2
-
3
- ENV['BARFOO'] = 'true,false,false'
4
- ARGV.replace %w( 42 bar=40 bar=2 --foobar=a )
5
-
6
- Main {
7
- argument('foo'){
8
- cast :int
9
- }
10
-
11
- keyword('bar'){
12
- arity 2
13
- cast :float
14
- defaults 0.0, 1.0
15
- }
16
-
17
- option('foobar'){
18
- argument :optional
19
- description 'the foobar option is very handy'
20
- }
21
-
22
- environment('BARFOO'){
23
- cast :list_of_bool
24
- synopsis 'export barfoo=value'
25
- }
26
-
27
- def run
28
- p params['foo'].value
29
- p params['bar'].values
30
- p params['foobar'].value
31
- p params['BARFOO'].value
32
- end
33
- }
data/main-0.0.1.gem DELETED
File without changes