main 2.8.1 → 2.8.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -364,14 +364,77 @@ SAMPLES
364
364
 
365
365
 
366
366
 
367
+ <========< samples/g.rb >========>
368
+
369
+ ~ > cat samples/g.rb
370
+
371
+ require 'main'
372
+
373
+ ARGV.replace %w( 42 ) if ARGV.empty?
374
+
375
+ Main {
376
+ argument( 'foo' )
377
+ option( 'bar' )
378
+
379
+ run { puts "This is what to_options produces: #{params.to_options.inspect}" }
380
+ }
381
+
382
+ ~ > ruby samples/g.rb
383
+
384
+ This is what to_options produces: {"help"=>nil, "foo"=>"42", "bar"=>nil}
385
+
386
+ ~ > ruby samples/g.rb --help
387
+
388
+ NAME
389
+ g.rb
390
+
391
+ SYNOPSIS
392
+ g.rb foo [options]+
393
+
394
+ PARAMETERS
395
+ foo (1 -> foo)
396
+ --bar
397
+ --help, -h
398
+
399
+
400
+
367
401
  DOCS
368
402
  test/main.rb
369
403
 
370
- find ./lib |xargs vim -o
404
+ vim -p lib/main.rb lib/main/*rb
371
405
 
372
406
  API section below
373
407
 
374
408
  HISTORY
409
+ 2.8.2
410
+ - fixes and tests for negative arity/attr arguments, options, eg
411
+
412
+ argument(:foo){
413
+ arity -1
414
+ }
415
+
416
+ def run # ARGV == %w( a b c )
417
+ p foo #=> %w( a b c )
418
+ end
419
+
420
+ thanks nathan
421
+
422
+ 2.8.1
423
+ - move from attributes.rb to fattr.rb
424
+
425
+ 2.8.0
426
+ - added 'to_options' method for Parameter::Table. this allows you to convert
427
+ all the parameters to a simple hash.
428
+ for example
429
+
430
+ Main {
431
+ option 'foo'
432
+ argument 'baz'
433
+
434
+ run { puts params.to_options.inspect }
435
+
436
+ }
437
+
375
438
  2.7.0
376
439
  - removed bundled arrayfields and attributes. these are now dependancies
377
440
  mananged by rubygems. a.k.a. you must have rubygems installed for main
data/lib/main.rb CHANGED
@@ -2,7 +2,7 @@ module Main
2
2
  #
3
3
  # top level constants
4
4
  #
5
- Main::VERSION = '2.8.0' unless
5
+ Main::VERSION = '2.8.2' unless
6
6
  defined? Main::VERSION
7
7
  def self.version() Main::VERSION end
8
8
 
@@ -516,9 +516,9 @@ puts
516
516
  new(param).evaluate(&block)
517
517
  end
518
518
 
519
- # attr 'p'
520
519
  attr 'param'
521
520
  alias_method 'evaluate', 'instance_eval'
521
+
522
522
  def initialize param
523
523
  @param = param
524
524
  end
@@ -532,7 +532,7 @@ puts
532
532
  alias_method 'attribute', 'fattr'
533
533
 
534
534
  def fattr_block_for name, &block
535
- block ||= lambda{|param| param.arity.abs <= 1 ? param.value : param.values }
535
+ block ||= lambda{|param| [0,1].include?(param.arity) ? param.value : param.values }
536
536
  lambda{ block.call self.param[name] }
537
537
  end
538
538
 
@@ -642,11 +642,15 @@ puts
642
642
  end
643
643
 
644
644
  def arity value
645
+ raise Arity if value.nil?
645
646
  value = -1 if value.to_s == '*'
646
- param.arity = Integer value
647
+ value = Integer value
648
+ raise Arity if value.zero?
649
+ param.arity = value
647
650
  if param.arity == -1
648
651
  optional true
649
652
  end
653
+ value
650
654
  end
651
655
  def arity?
652
656
  param.arity?
data/test/main.rb CHANGED
@@ -7,6 +7,7 @@ require 'stringio'
7
7
  require 'test/unit'
8
8
  require 'main'
9
9
 
10
+
10
11
  class T < Test::Unit::TestCase
11
12
  fattr 'status'
12
13
  fattr 'logger'
@@ -59,7 +60,7 @@ class T < Test::Unit::TestCase
59
60
  main
60
61
  end
61
62
 
62
- #
63
+
63
64
  # basic test
64
65
  #
65
66
  def test_0000
@@ -78,7 +79,7 @@ class T < Test::Unit::TestCase
78
79
  }
79
80
  assert x == 42
80
81
  end
81
- #
82
+
82
83
  # exit status
83
84
  #
84
85
  def test_0020
@@ -140,7 +141,7 @@ class T < Test::Unit::TestCase
140
141
  }
141
142
  assert status == 42
142
143
  end
143
- #
144
+
144
145
  # parameter parsing
145
146
  #
146
147
  def test_0080
@@ -466,7 +467,7 @@ class T < Test::Unit::TestCase
466
467
  }
467
468
  assert foo.value == 42
468
469
  end
469
- #
470
+
470
471
  # usage
471
472
  #
472
473
  def test_0280
@@ -526,7 +527,7 @@ class T < Test::Unit::TestCase
526
527
  assert u['name2'] == nil
527
528
  }
528
529
  end
529
- #
530
+
530
531
  # io redirection
531
532
  #
532
533
  class ::Object
@@ -611,7 +612,7 @@ class T < Test::Unit::TestCase
611
612
  assert_nothing_raised{ m.stdout.rewind }
612
613
  assert m.stdout.read == "42\n"
613
614
  end
614
- #
615
+
615
616
  # main ctor
616
617
  #
617
618
  def test_0380
@@ -642,10 +643,10 @@ class T < Test::Unit::TestCase
642
643
  assert argv == $argv
643
644
  end
644
645
 
645
- #
646
+
646
647
  # negative/globbing arity
647
648
  #
648
- def test_4000
649
+ def test_0400
649
650
  m = nil
650
651
  argv = %w( a b c )
651
652
  assert_nothing_raised{
@@ -656,7 +657,18 @@ class T < Test::Unit::TestCase
656
657
  }
657
658
  assert m.param['zero_or_more'].values == argv
658
659
  end
659
- def test_4010
660
+ def test_0401
661
+ m = nil
662
+ argv = %w( a b c )
663
+ assert_nothing_raised{
664
+ main(argv.dup) {
665
+ argument('zero_or_more'){ arity -1 }
666
+ run{ m = self }
667
+ }
668
+ }
669
+ assert m.param['zero_or_more'].values == argv
670
+ end
671
+ def test_0410
660
672
  m = nil
661
673
  argv = %w( a b c )
662
674
  assert_nothing_raised{
@@ -667,7 +679,7 @@ class T < Test::Unit::TestCase
667
679
  }
668
680
  assert m.param['zero_or_more'].values == argv
669
681
  end
670
- def test_4020
682
+ def test_0420
671
683
  m = nil
672
684
  argv = %w( a b c )
673
685
  assert_nothing_raised{
@@ -678,7 +690,7 @@ class T < Test::Unit::TestCase
678
690
  }
679
691
  assert m.param['one_or_more'].values == argv
680
692
  end
681
- def test_4030
693
+ def test_0430
682
694
  m = nil
683
695
  argv = %w( a b c )
684
696
  assert_nothing_raised{
@@ -689,7 +701,7 @@ class T < Test::Unit::TestCase
689
701
  }
690
702
  assert m.param['two_or_more'].values == argv
691
703
  end
692
- def test_4040
704
+ def test_0440
693
705
  m = nil
694
706
  argv = %w()
695
707
  assert_nothing_raised{
@@ -700,7 +712,7 @@ class T < Test::Unit::TestCase
700
712
  }
701
713
  assert m.param['zero_or_more'].values == argv
702
714
  end
703
- def test_4050
715
+ def test_0450
704
716
  m = nil
705
717
  argv = %w()
706
718
  assert_raises(Main::Parameter::NotGiven){
@@ -710,7 +722,7 @@ class T < Test::Unit::TestCase
710
722
  }
711
723
  }
712
724
  end
713
- def test_4060
725
+ def test_0460
714
726
  m = nil
715
727
  argv = %w( a )
716
728
  assert_raises(Main::Parameter::Arity){
@@ -720,7 +732,7 @@ class T < Test::Unit::TestCase
720
732
  }
721
733
  }
722
734
  end
723
- def test_4070
735
+ def test_0470
724
736
  m = nil
725
737
  argv = %w( a )
726
738
  assert_raises(Main::Parameter::Arity){
@@ -730,10 +742,10 @@ class T < Test::Unit::TestCase
730
742
  }
731
743
  }
732
744
  end
733
- #
745
+
734
746
  # sub-command/mode functionality
735
747
  #
736
- def test_4080
748
+ def test_0480
737
749
  m = nil
738
750
  argv = %w( a b )
739
751
  assert_nothing_raised{
@@ -746,7 +758,7 @@ class T < Test::Unit::TestCase
746
758
  }
747
759
  assert m.param['b'].value == 'b'
748
760
  end
749
- def test_4090
761
+ def test_0490
750
762
  m = nil
751
763
  argv = %w( a b c )
752
764
  assert_nothing_raised{
@@ -762,6 +774,77 @@ class T < Test::Unit::TestCase
762
774
  assert m.param['c'].value == 'c'
763
775
  end
764
776
 
777
+
778
+ # parameter attr/fattr/attribute
779
+ #
780
+ def test_0500
781
+ name = 'arity_zero_paramter_attr'
782
+ m = nil
783
+ argv = %w( )
784
+ assert_raises(Main::Parameter::Arity){
785
+ main(argv.dup) {
786
+ argument(name){ arity 0 }
787
+ run{ m = self }
788
+ }
789
+ }
790
+ end
791
+ def test_0510
792
+ name = 'arity_one_paramter_attr'
793
+ m = nil
794
+ argv = %w( a )
795
+ assert_nothing_raised{
796
+ main(argv.dup) {
797
+ argument(name){ arity 1; attr }
798
+ run{ m = send(name) }
799
+ }
800
+ }
801
+ assert m == argv.first
802
+ end
803
+ def test_0520
804
+ name = 'arity_more_than_one_paramter_attr'
805
+ m = nil
806
+ argv = %w( a b c d)
807
+ [2, 3, 4].each do |a|
808
+ assert_nothing_raised{
809
+ main(argv.dup) {
810
+ argument(name){ arity a; attr }
811
+ run{ m = send(name) }
812
+ }
813
+ }
814
+ assert m == argv.first(a)
815
+ end
816
+ end
817
+ def test_0530
818
+ name = 'arity_negative_one_paramter_attr'
819
+ m = nil
820
+ argvs = %w( ), %w( a ), %w( a b ), %w( a b c )
821
+ argvs.each do |argv|
822
+ assert_nothing_raised{
823
+ main(argv.dup) {
824
+ argument(name){ arity -1; attr }
825
+ run{ m = send(name) }
826
+ }
827
+ }
828
+ assert m == argv
829
+ end
830
+ end
831
+ def test_0540
832
+ name = 'arity_negative_more_than_one_paramter_attr'
833
+ m = nil
834
+ argvs = %w(a b), %w( a b c), %w( a b c d )
835
+ argvs.each do |argv|
836
+ [-2, -3].each do |a|
837
+ assert_nothing_raised{
838
+ main(argv.dup) {
839
+ argument(name){ arity a; attr }
840
+ run{ m = send(name) }
841
+ }
842
+ }
843
+ assert m == argv
844
+ end
845
+ end
846
+ end
847
+
765
848
  end
766
849
 
767
850
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: main
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.1
4
+ version: 2.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ara T. Howard
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-11 00:00:00 -06:00
12
+ date: 2008-07-13 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -56,7 +56,6 @@ files:
56
56
  - lib/main/usage.rb
57
57
  - lib/main/util.rb
58
58
  - lib/main.rb
59
- - main-2.8.1.gem
60
59
  - README
61
60
  - samples
62
61
  - samples/a.rb
data/main-2.8.1.gem DELETED
File without changes