main 2.8.0 → 2.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/gemspec.rb CHANGED
@@ -21,11 +21,10 @@ Gem::Specification::new do |spec|
21
21
  spec.executables = shiteless[Dir::glob("bin/*")].map{|exe| File::basename exe}
22
22
 
23
23
  spec.require_path = "lib"
24
- spec.autorequire = lib
25
24
 
26
25
  spec.has_rdoc = File::exist? "doc"
27
26
  spec.test_suite_file = "test/#{ lib }.rb" if File::directory? "test"
28
- spec.add_dependency 'attributes', '>= 5.0.0'
27
+ spec.add_dependency 'fattr', '>= 1.0.3'
29
28
  spec.add_dependency 'arrayfields', '>= 4.5.0'
30
29
 
31
30
  spec.extensions << "extconf.rb" if File::exists? "extconf.rb"
@@ -33,4 +32,5 @@ Gem::Specification::new do |spec|
33
32
  spec.author = "Ara T. Howard"
34
33
  spec.email = "ara.t.howard@gmail.com"
35
34
  spec.homepage = "http://codeforpeople.com/lib/ruby/#{ lib }/"
35
+ spec.rubyforge_project = 'codeforpeople'
36
36
  end
data/lib/main.rb CHANGED
@@ -28,12 +28,12 @@ module Main
28
28
  42
29
29
  end
30
30
 
31
- require 'attributes'
31
+ require 'fattr'
32
32
  begin
33
- version = Attributes.version
34
- raise unless version[%r/^5\./]
33
+ version = Fattr.version
34
+ raise unless version[%r/^1\./]
35
35
  rescue
36
- abort "main requires attributes >= 5.0.0 - gem install attributes"
36
+ abort "main requires fattrs >= 1.0.3 - gem install fattr"
37
37
  end
38
38
 
39
39
  require 'arrayfields'
data/lib/main/base.rb CHANGED
@@ -49,9 +49,9 @@ module Main
49
49
  end
50
50
  end
51
51
 
52
- def self.inheritable_attribute name, &block
52
+ def self.inheritable_fattr name, &block
53
53
  block ||= lambda{}
54
- attribute( name ){
54
+ fattr( name ){
55
55
  catch :value do
56
56
  if parent?
57
57
  value = parent.send name
@@ -68,34 +68,34 @@ module Main
68
68
  }
69
69
  end
70
70
 
71
- # attributes
72
- attribute( 'name' ){ File.basename $0 }
73
- attribute( 'synopsis' ){ Usage.default_synopsis(self) }
74
- attribute( 'description' )
75
- attribute( 'usage' ){ Usage.default_usage self }
76
- attribute( 'modes' ){ Mode.list }
77
- attribute( 'mode_definitions' ){ Array.new }
78
- attribute( 'mode_name' ){ 'main' }
79
- attribute( 'parent' ){ nil }
80
- attribute( 'children' ){ Set.new }
81
-
82
- attribute( 'program' ){ File.basename $0 }
83
- attribute( 'author' )
84
- attribute( 'version' )
85
- attribute( 'stdin' ){ $stdin }
86
- attribute( 'stdout' ){ $stdout }
87
- attribute( 'stderr' ){ $stderr }
88
- attribute( 'logger' ){ stderr }
89
- attribute( 'logger_level' ){ Logger::INFO }
90
- attribute( 'exit_status' ){ EXIT_SUCCESS }
91
- attribute( 'exit_success' ){ EXIT_SUCCESS }
92
- attribute( 'exit_failure' ){ EXIT_FAILURE }
93
- attribute( 'exit_warn' ){ EXIT_WARN }
94
- inheritable_attribute( 'parameters' ){ Parameter::List[] }
95
- inheritable_attribute( 'can_has_hash' ){ Hash.new }
96
- inheritable_attribute( 'mixin_table' ){ Hash.new }
97
-
98
- # override a few attributes
71
+ # fattrs
72
+ fattr( 'name' ){ File.basename $0 }
73
+ fattr( 'synopsis' ){ Usage.default_synopsis(self) }
74
+ fattr( 'description' )
75
+ fattr( 'usage' ){ Usage.default_usage self }
76
+ fattr( 'modes' ){ Mode.list }
77
+ fattr( 'mode_definitions' ){ Array.new }
78
+ fattr( 'mode_name' ){ 'main' }
79
+ fattr( 'parent' ){ nil }
80
+ fattr( 'children' ){ Set.new }
81
+
82
+ fattr( 'program' ){ File.basename $0 }
83
+ fattr( 'author' )
84
+ fattr( 'version' )
85
+ fattr( 'stdin' ){ $stdin }
86
+ fattr( 'stdout' ){ $stdout }
87
+ fattr( 'stderr' ){ $stderr }
88
+ fattr( 'logger' ){ stderr }
89
+ fattr( 'logger_level' ){ Logger::INFO }
90
+ fattr( 'exit_status' ){ EXIT_SUCCESS }
91
+ fattr( 'exit_success' ){ EXIT_SUCCESS }
92
+ fattr( 'exit_failure' ){ EXIT_FAILURE }
93
+ fattr( 'exit_warn' ){ EXIT_WARN }
94
+ inheritable_fattr( 'parameters' ){ Parameter::List[] }
95
+ inheritable_fattr( 'can_has_hash' ){ Hash.new }
96
+ inheritable_fattr( 'mixin_table' ){ Hash.new }
97
+
98
+ # override a few fattrs
99
99
  def mode_name=(value)
100
100
  @mode_name = Mode.new value
101
101
  end
@@ -263,20 +263,20 @@ module Main
263
263
  end
264
264
  extend DSL
265
265
 
266
- attribute 'argv'
267
- attribute 'env'
268
- attribute 'params'
269
- attribute 'logger'
270
- attribute 'stdin'
271
- attribute 'stdout'
272
- attribute 'stderr'
266
+ fattr 'argv'
267
+ fattr 'env'
268
+ fattr 'params'
269
+ fattr 'logger'
270
+ fattr 'stdin'
271
+ fattr 'stdout'
272
+ fattr 'stderr'
273
273
 
274
274
  %w(
275
275
  program name synopsis description author version
276
276
  exit_status exit_success exit_failure exit_warn
277
277
  logger_level
278
278
  usage
279
- ).each{|a| attribute(a){ self.class.send a}}
279
+ ).each{|a| fattr(a){ self.class.send a}}
280
280
 
281
281
  %w( parameters param ).each do |dst|
282
282
  alias_method "#{ dst }", "params"
@@ -438,7 +438,7 @@ module Main
438
438
  def modes
439
439
  self.class.modes
440
440
  end
441
- attribute 'mode'
441
+ fattr 'mode'
442
442
 
443
443
  def help! status = 0
444
444
  print usage.to_s
@@ -2,7 +2,7 @@ module Main
2
2
  class Parameter
3
3
  class Error < StandardError
4
4
  include Softspoken
5
- attribute 'wrapped'
5
+ fattr 'wrapped'
6
6
  end
7
7
  class Arity < Error; end
8
8
  class NotGiven < Arity; end
@@ -54,25 +54,25 @@ module Main
54
54
  end
55
55
  end
56
56
 
57
- attribute 'type'
58
- attribute 'names'
59
- attribute 'abbreviations'
60
- attribute 'argument'
61
- attribute 'given'
62
- attribute 'cast'
63
- attribute 'validate'
64
- attribute 'description'
65
- attribute 'synopsis'
66
- attribute('values'){ [] }
67
- attribute('defaults'){ [] }
68
- attribute('examples'){ [] }
69
-
70
- attribute 'arity' => 1
71
- attribute 'required' => false
72
-
73
- attribute 'error_handler_before'
74
- attribute 'error_handler_instead'
75
- attribute 'error_handler_after'
57
+ fattr 'type'
58
+ fattr 'names'
59
+ fattr 'abbreviations'
60
+ fattr 'argument'
61
+ fattr 'given'
62
+ fattr 'cast'
63
+ fattr 'validate'
64
+ fattr 'description'
65
+ fattr 'synopsis'
66
+ fattr('values'){ [] }
67
+ fattr('defaults'){ [] }
68
+ fattr('examples'){ [] }
69
+
70
+ fattr 'arity' => 1
71
+ fattr 'required' => false
72
+
73
+ fattr 'error_handler_before'
74
+ fattr 'error_handler_instead'
75
+ fattr 'error_handler_after'
76
76
 
77
77
  def initialize name, *names, &block
78
78
  @names = Cast.list_of_string name, *names
@@ -248,9 +248,9 @@ puts
248
248
  end
249
249
 
250
250
  class Argument < Parameter
251
- attribute 'required' => true
251
+ fattr 'required' => true
252
252
 
253
- attribute 'synopsis' do
253
+ fattr 'synopsis' do
254
254
  label = name
255
255
  op = required ? "->" : "~>"
256
256
  value = defaults.size > 0 ? "#{ name }=#{ defaults.join ',' }" : name
@@ -260,10 +260,10 @@ puts
260
260
  end
261
261
 
262
262
  class Option < Parameter
263
- attribute 'required' => false
264
- attribute 'arity' => 0
263
+ fattr 'required' => false
264
+ fattr 'arity' => 0
265
265
 
266
- attribute 'synopsis' do
266
+ fattr 'synopsis' do
267
267
  long, *short = names
268
268
  value = cast || name
269
269
  rhs = argument ? (argument == :required ? "=#{ name }" : "=[#{ name }]") : nil
@@ -280,10 +280,10 @@ puts
280
280
  end
281
281
 
282
282
  class Keyword < Parameter
283
- attribute 'required' => false
284
- attribute 'argument' => :required
283
+ fattr 'required' => false
284
+ fattr 'argument' => :required
285
285
 
286
- attribute 'synopsis' do
286
+ fattr 'synopsis' do
287
287
  label = "#{ name }=#{ name }"
288
288
  op = required ? "->" : "~>"
289
289
  value = defaults.size > 0 ? "#{ name }=#{ defaults.join ',' }" : name
@@ -293,9 +293,9 @@ puts
293
293
  end
294
294
 
295
295
  class Environment < Parameter
296
- attribute 'argument' => :required
296
+ fattr 'argument' => :required
297
297
 
298
- attribute 'synopsis' do
298
+ fattr 'synopsis' do
299
299
  label = "env[#{ name }]=#{ name }"
300
300
  op = required ? "->" : "~>"
301
301
  value = defaults.size > 0 ? "#{ name }=#{ defaults.join ',' }" : name
@@ -523,21 +523,21 @@ puts
523
523
  @param = param
524
524
  end
525
525
 
526
- def attribute a = nil, &block
526
+ def fattr a = nil, &block
527
527
  name = param.name
528
528
  a ||= name
529
- b = attribute_block_for name, &block
530
- Main.current.module_eval{ attribute *a, &b }
529
+ b = fattr_block_for name, &block
530
+ Main.current.module_eval{ fattr *a, &b }
531
531
  end
532
+ alias_method 'attribute', 'fattr'
532
533
 
533
- def attribute_block_for name, &block
534
- #block ||= lambda{|param| param.arity == 1 ? param.value : param.values}
534
+ def fattr_block_for name, &block
535
535
  block ||= lambda{|param| param.arity.abs <= 1 ? param.value : param.values }
536
536
  lambda{ block.call self.param[name] }
537
537
  end
538
538
 
539
539
  def attr *a, &b
540
- attribute *a, &b
540
+ fattr *a, &b
541
541
  end
542
542
 
543
543
  def example *list
@@ -663,6 +663,12 @@ puts
663
663
  self.fields = []
664
664
  extend BoundsCheck
665
665
  end
666
+
667
+ def to_options
668
+ (hash = self.to_hash ).keys.each { |key| hash[key] = hash[key].value }
669
+ return hash
670
+ end
671
+
666
672
  module BoundsCheck
667
673
  def [] *a, &b
668
674
  p = super
@@ -1,7 +1,7 @@
1
1
  module Main
2
2
  module Softspoken
3
3
  class << self
4
- attribute 'softspoken' => true
4
+ fattr 'softspoken' => true
5
5
  def on!() softspoken(true) end
6
6
  def off!() softspoken(false) end
7
7
  def === other
data/lib/main/stdext.rb CHANGED
@@ -13,7 +13,7 @@ module SaneAbort
13
13
  def abort message = nil
14
14
  if message
15
15
  message = message.to_s
16
- message.singleton_class{ attribute 'abort' => true }
16
+ message.singleton_class{ fattr 'abort' => true }
17
17
  STDERR.puts message
18
18
  end
19
19
  exit 1
@@ -23,7 +23,7 @@ end
23
23
  def abort message = nil
24
24
  if message
25
25
  message = message.to_s
26
- message.singleton_class{ attribute 'abort' => true }
26
+ message.singleton_class{ fattr 'abort' => true }
27
27
  STDERR.puts message
28
28
  end
29
29
  exit 1
@@ -31,7 +31,7 @@ end
31
31
  def Process.abort message = nil
32
32
  if message
33
33
  message = message.to_s
34
- message.singleton_class{ attribute 'abort' => true }
34
+ message.singleton_class{ fattr 'abort' => true }
35
35
  STDERR.puts message
36
36
  end
37
37
  exit 1
data/lib/main/usage.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module Main
2
2
  class Usage < ::Array
3
- attribute 'chunkname'
4
- attribute 'upcase'
5
- attribute 'eos'
3
+ fattr 'chunkname'
4
+ fattr 'upcase'
5
+ fattr 'eos'
6
6
 
7
7
  def initialize opts={}
8
8
  self.fields=[]
@@ -182,7 +182,7 @@ module Main
182
182
  alias_method "default", "default_usage"
183
183
  end
184
184
 
185
- attribute "main"
185
+ fattr "main"
186
186
 
187
187
  def set_defaults!
188
188
  usage = self
data/main-2.8.1.gem ADDED
File without changes
data/samples/g.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'main'
2
+
3
+ ARGV.replace %w( 42 ) if ARGV.empty?
4
+
5
+ Main {
6
+ argument( 'foo' )
7
+ option( 'bar' )
8
+
9
+ run { puts "This is what to_options produces: #{params.to_options.inspect}" }
10
+ }
data/test/main.rb CHANGED
@@ -8,9 +8,9 @@ require 'test/unit'
8
8
  require 'main'
9
9
 
10
10
  class T < Test::Unit::TestCase
11
- attribute 'status'
12
- attribute 'logger'
13
- attribute 'error'
11
+ fattr 'status'
12
+ fattr 'logger'
13
+ fattr 'error'
14
14
 
15
15
  def setup
16
16
  @status = nil
metadata CHANGED
@@ -1,35 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.4
3
- specification_version: 1
4
2
  name: main
5
3
  version: !ruby/object:Gem::Version
6
- version: 2.8.0
7
- date: 2007-12-23 00:00:00 -07:00
8
- summary: main
9
- require_paths:
10
- - lib
11
- email: ara.t.howard@gmail.com
12
- homepage: http://codeforpeople.com/lib/ruby/main/
13
- rubyforge_project:
14
- description:
15
- autorequire: main
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: false
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 2.8.1
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - Ara T. Howard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-11 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: fattr
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.3
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: arrayfields
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 4.5.0
32
+ version:
33
+ description:
34
+ email: ara.t.howard@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
31
41
  files:
32
- - a.rb
33
42
  - gemspec.rb
34
43
  - gen_readme.rb
35
44
  - install.rb
@@ -47,6 +56,7 @@ files:
47
56
  - lib/main/usage.rb
48
57
  - lib/main/util.rb
49
58
  - lib/main.rb
59
+ - main-2.8.1.gem
50
60
  - README
51
61
  - samples
52
62
  - samples/a.rb
@@ -55,38 +65,35 @@ files:
55
65
  - samples/d.rb
56
66
  - samples/e.rb
57
67
  - samples/f.rb
68
+ - samples/g.rb
58
69
  - test
59
70
  - test/main.rb
60
- - test/main.rb.bak
61
71
  - TODO
62
- test_files:
63
- - test/main.rb
72
+ has_rdoc: false
73
+ homepage: http://codeforpeople.com/lib/ruby/main/
74
+ post_install_message:
64
75
  rdoc_options: []
65
76
 
66
- extra_rdoc_files: []
67
-
68
- executables: []
69
-
70
- extensions: []
71
-
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
72
91
  requirements: []
73
92
 
74
- dependencies:
75
- - !ruby/object:Gem::Dependency
76
- name: attributes
77
- version_requirement:
78
- version_requirements: !ruby/object:Gem::Version::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: 5.0.0
83
- version:
84
- - !ruby/object:Gem::Dependency
85
- name: arrayfields
86
- version_requirement:
87
- version_requirements: !ruby/object:Gem::Version::Requirement
88
- requirements:
89
- - - ">="
90
- - !ruby/object:Gem::Version
91
- version: 4.5.0
92
- version:
93
+ rubyforge_project: codeforpeople
94
+ rubygems_version: 1.0.1
95
+ signing_key:
96
+ specification_version: 2
97
+ summary: main
98
+ test_files:
99
+ - test/main.rb
data/a.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'main'
2
-
3
- Main {
4
- option('foo'){
5
- examples
6
- }
7
- }
data/test/main.rb.bak DELETED
@@ -1,767 +0,0 @@
1
- $:.unshift '.'
2
- $:.unshift './lib'
3
- $:.unshift '..'
4
- $:.unshift '../lib'
5
-
6
- require 'stringio'
7
- require 'test/unit'
8
- require 'main'
9
-
10
- class T < Test::Unit::TestCase
11
- attribute 'status'
12
- attribute 'logger'
13
- attribute 'error'
14
-
15
- def setup
16
- @status = nil
17
- @logger = Logger.new StringIO.new
18
- @error = nil
19
- end
20
-
21
- def teardown
22
- end
23
-
24
- def main argv=[], env={}, &b
25
- at_exit{ exit! }
26
-
27
- $VERBOSE=nil
28
- ARGV.replace argv
29
- ENV.clear
30
- env.each{|k,v| ENV[k.to_s]=v.to_s}
31
-
32
- this = self
33
-
34
- klass = ::Main.create do
35
- module_eval &b if b
36
-
37
- define_method :handle_exception do |e|
38
- if e.respond_to? :status
39
- this.status = e.status
40
- else
41
- raise
42
- end
43
- end
44
-
45
- define_method :handle_throw do |*a|
46
- end
47
- end
48
-
49
- main = klass.new argv, env
50
-
51
- main.logger = @logger
52
-
53
- begin
54
- main.run
55
- ensure
56
- this.status ||= main.exit_status
57
- end
58
-
59
- main
60
- end
61
-
62
- #
63
- # basic test
64
- #
65
- def test_0000
66
- assert_nothing_raised{
67
- main{
68
- def run() end
69
- }
70
- }
71
- end
72
- def test_0010
73
- x = nil
74
- assert_nothing_raised{
75
- main{
76
- define_method(:run){ x = 42 }
77
- }
78
- }
79
- assert x == 42
80
- end
81
- #
82
- # exit status
83
- #
84
- def test_0020
85
- assert_nothing_raised{
86
- main{
87
- def run() end
88
- }
89
- }
90
- assert status == 0
91
- end
92
- def test_0030
93
- assert_nothing_raised{
94
- main{
95
- def run() exit 42 end
96
- }
97
- }
98
- assert status == 42
99
- end
100
- def test_0040
101
- assert_nothing_raised{
102
- fork{
103
- main{
104
- def run() exit! 42 end
105
- }
106
- }
107
- Process.wait
108
- assert $?.exitstatus == 42
109
- }
110
- end
111
- def test_0050
112
- assert_nothing_raised{
113
- main{
114
- def run() exit 42 end
115
- }
116
- }
117
- assert status == 42
118
- end
119
- def test_0060
120
- assert_nothing_raised{
121
- main{
122
- def run() raise ArgumentError end
123
- }
124
- }
125
- assert status == 1
126
- end
127
- def test_0060
128
- assert_raises(RuntimeError){
129
- main{
130
- def run() exit_status 42; raise end
131
- }
132
- }
133
- assert status == 42
134
- end
135
- def test_0070
136
- assert_raises(ArgumentError){
137
- main{
138
- def run() exit_status 42; raise ArgumentError end
139
- }
140
- }
141
- assert status == 42
142
- end
143
- #
144
- # parameter parsing
145
- #
146
- def test_0080
147
- p = nil
148
- assert_raises(Main::Parameter::NotGiven){
149
- main(){
150
- argument 'foo'
151
- define_method('run'){ }
152
- }
153
- }
154
- end
155
- def test_0090
156
- p = nil
157
- m = nil
158
- argv = %w[ 42 ]
159
- given = nil
160
- assert_nothing_raised{
161
- main(argv.dup){
162
- argument 'foo'
163
- define_method('run'){ m = self; p = param['foo'] }
164
- }
165
- }
166
- assert p.value == argv.first
167
- assert p.values == argv
168
- assert p.given?
169
- assert m.argv.empty?
170
- end
171
- def test_0100
172
- p = nil
173
- argv = %w[]
174
- given = nil
175
- assert_nothing_raised{
176
- main(argv){
177
- p = argument('foo'){ optional }
178
- define_method('run'){ p = param['foo'] }
179
- }
180
- }
181
- assert p.optional?
182
- assert !p.required?
183
- assert p.value == nil
184
- assert p.values == []
185
- assert !p.given?
186
- end
187
- def test_0101
188
- p = nil
189
- argv = %w[]
190
- given = nil
191
- assert_nothing_raised{
192
- main(argv){
193
- p = argument('foo'){ required false }
194
- define_method('run'){ p = param['foo'] }
195
- }
196
- }
197
- assert p.optional?
198
- assert !p.required?
199
- assert p.value == nil
200
- assert p.values == []
201
- assert !p.given?
202
- end
203
- def test_0110
204
- p = nil
205
- argv = %w[ --foo ]
206
- assert_nothing_raised{
207
- main(argv){
208
- option('foo'){ required }
209
- define_method('run'){ p = param['foo'] }
210
- }
211
- }
212
- assert p.value == true
213
- assert p.values ==[true]
214
- assert p.given?
215
- end
216
- def test_0120
217
- p = nil
218
- argv = []
219
- assert_nothing_raised{
220
- main(argv){
221
- option 'foo'
222
- define_method('run'){ p = param['foo'] }
223
- }
224
- }
225
- assert p.value == nil
226
- assert p.values == []
227
- assert !p.given?
228
- end
229
- def test_0130
230
- p = nil
231
- assert_nothing_raised{
232
- main(%w[--foo=42]){
233
- option('foo'){ required; argument_required }
234
- define_method('run'){ p = param['foo']}
235
- }
236
- }
237
- assert p.required?
238
- assert p.argument_required?
239
- assert !p.optional?
240
- end
241
- def test_0131
242
- assert_raises(Main::Parameter::NotGiven){
243
- main(){
244
- option('foo'){ required; argument_required }
245
- define_method('run'){}
246
- }
247
- }
248
- end
249
- def test_0140
250
- assert_raises(Main::Parameter::MissingArgument){
251
- main(['--foo']){
252
- option('foo'){ required; argument_required }
253
- define_method('run'){}
254
- }
255
- }
256
- end
257
- def test_0150
258
- param = nil
259
- assert_nothing_raised{
260
- main(%w[--foo=42 --bar=42.0 --foobar=true --barfoo=false --uri=http://foo --x=s]){
261
- option('foo'){
262
- required
263
- argument_required
264
- cast :int
265
- }
266
- option('bar'){
267
- argument_required
268
- cast :float
269
- }
270
- option('foobar'){
271
- argument_required
272
- cast :bool
273
- }
274
- option('barfoo'){
275
- argument_required
276
- cast :string
277
- }
278
- option('uri'){
279
- argument_required
280
- cast :uri
281
- }
282
- option('x'){
283
- argument_required
284
- cast{|x| x.to_s.upcase}
285
- }
286
- define_method('run'){ param = params }
287
- }
288
- }
289
- assert param['foo'].value == 42
290
- assert param['bar'].value == 42.0
291
- assert param['foobar'].value == true
292
- assert param['barfoo'].value == 'false'
293
- assert param['uri'].value == URI.parse('http://foo')
294
- assert param['x'].value == 'S'
295
- end
296
- def test_0160
297
- p = nil
298
- assert_nothing_raised{
299
- main(%w[--foo=42]){
300
- option('foo'){
301
- required
302
- argument_required
303
- cast :int
304
- validate{|x| x == 42}
305
- }
306
- define_method('run'){ p = param['foo']}
307
- }
308
- }
309
- assert p.value == 42
310
- assert p.required?
311
- assert p.argument_required?
312
- assert !p.optional?
313
- end
314
- def test_0170
315
- assert_raises(Main::Parameter::InValid){
316
- main(%w[--foo=40]){
317
- option('foo'){
318
- required
319
- argument_required
320
- cast :int
321
- validate{|x| x == 42}
322
- }
323
- define_method('run'){ }
324
- }
325
- }
326
- end
327
- def test_0180
328
- assert_nothing_raised{
329
- main(%w[--foo=42]){
330
- option('--foo=foo'){
331
- required
332
- # argument_required
333
- cast :int
334
- validate{|x| x == 42}
335
- }
336
- define_method('run'){ }
337
- }
338
- }
339
- end
340
- def test_0190
341
- assert_raises(Main::Parameter::MissingArgument){
342
- main(%w[--foo]){
343
- option('--foo=foo'){ }
344
- define_method('run'){ }
345
- }
346
- }
347
- end
348
- def test_0200
349
- p = nil
350
- assert_nothing_raised{
351
- main(%w[--foo]){
352
- option('--foo=[foo]'){ }
353
- define_method('run'){ p = param['foo'] }
354
- }
355
- }
356
- assert p.value == true
357
- end
358
- def test_0210
359
- p = nil
360
- assert_nothing_raised{
361
- main(%w[--foo=42]){
362
- option('--foo=[foo]'){
363
- cast :int
364
- validate{|x| x == 42}
365
- }
366
- define_method('run'){ p = param['foo'] }
367
- }
368
- }
369
- assert p.value == 42
370
- end
371
- def test_0220
372
- p = nil
373
- assert_nothing_raised{
374
- main(%w[--foo=40 --foo=2]){
375
- option('--foo=foo'){
376
- arity 2
377
- cast :int
378
- validate{|x| x == 40 or x == 2}
379
- }
380
- define_method('run'){ p = param['foo'] }
381
- }
382
- }
383
- assert p.value == 40
384
- assert p.values == [40,2]
385
- end
386
- def test_0230
387
- p = nil
388
- assert_nothing_raised{
389
- main(%w[foo=42]){
390
- keyword('foo'){
391
- cast :int
392
- validate{|x| x == 42}
393
- }
394
- define_method('run'){ p = param['foo'] }
395
- }
396
- }
397
- assert p.value == 42
398
- end
399
- def test_0240
400
- foo = nil
401
- bar = nil
402
- assert_nothing_raised{
403
- main(%w[foo= bar]){
404
- keyword 'foo'
405
- keyword 'bar'
406
- define_method('run'){
407
- foo = param['foo']
408
- bar = param['bar']
409
- }
410
- }
411
- }
412
- assert foo.value == ''
413
- assert bar.value == nil
414
- end
415
- def test_0250
416
- foo = nil
417
- bar = nil
418
- assert_nothing_raised{
419
- main(%w[foo=40 bar=2]){
420
- keyword('foo'){
421
- cast :int
422
- }
423
- keyword('bar'){
424
- cast :int
425
- }
426
- define_method('run'){
427
- foo = param['foo']
428
- bar = param['bar']
429
- }
430
- }
431
- }
432
- assert foo.value == 40
433
- assert bar.value == 2
434
- end
435
- def test_0260
436
- foo = nil
437
- bar = nil
438
- foobar = nil
439
- assert_nothing_raised{
440
- main(%w[foo=40 --bar=2 foobar foo=42]){
441
- kw('foo'){ cast :int; arity 2 }
442
- opt('bar='){ cast :int }
443
- arg 'foobar'
444
-
445
- define_method('run'){
446
- foo = param['foo']
447
- bar = param['bar']
448
- foobar = param['foobar']
449
- }
450
- }
451
- }
452
- assert foo.value == 40
453
- assert foo.values == [40, 42]
454
- assert bar.value == 2
455
- assert foobar.value == 'foobar'
456
- end
457
- def test_0270
458
- foo = nil
459
- assert_nothing_raised{
460
- main([], 'foo' => '42'){
461
- env('foo'){ cast :int }
462
- define_method('run'){
463
- foo = param['foo']
464
- }
465
- }
466
- }
467
- assert foo.value == 42
468
- end
469
- #
470
- # usage
471
- #
472
- def test_0280
473
- assert_nothing_raised{
474
- u = Main::Usage.new
475
- }
476
- end
477
- def test_0290
478
- assert_nothing_raised{
479
- u = Main::Usage.default Main.create
480
- }
481
- end
482
- def test_0300
483
- assert_nothing_raised{
484
- chunk = <<-txt
485
- a
486
- b
487
- c
488
- txt
489
- assert Main::Util.unindent(chunk) == "a\nb\nc"
490
- chunk = <<-txt
491
- a
492
- b
493
- c
494
- txt
495
- assert Main::Util.unindent(chunk) == "a\n b\n c"
496
- }
497
- end
498
- def test_0310
499
- assert_nothing_raised{
500
- u = Main::Usage.new
501
- u[:name] = 'foobar'
502
- assert u[:name] = 'foobar'
503
- assert u['name'] = 'foobar'
504
- }
505
- end
506
- def test_0320
507
- assert_nothing_raised{
508
- u = Main::Usage.new
509
- u[:name] = 'foobar'
510
- assert u[:name] == 'foobar'
511
- assert u['name'] == 'foobar'
512
- u[:name2] = 'barfoo'
513
- assert u[:name] == 'foobar'
514
- assert u['name'] == 'foobar'
515
- assert u[:name2] == 'barfoo'
516
- assert u['name2'] == 'barfoo'
517
- u.delete_at :name
518
- assert u[:name] == nil
519
- assert u['name'] == nil
520
- assert u[:name2] == 'barfoo'
521
- assert u['name2'] == 'barfoo'
522
- u.delete_at :name2
523
- assert u[:name] == nil
524
- assert u['name'] == nil
525
- assert u[:name2] == nil
526
- assert u['name2'] == nil
527
- }
528
- end
529
- #
530
- # io redirection
531
- #
532
- class ::Object
533
- require 'tempfile'
534
- def infile buf
535
- t = Tempfile.new rand.to_s
536
- t << buf
537
- t.close
538
- open t.path, 'r+'
539
- end
540
- def outfile
541
- t = Tempfile.new rand.to_s
542
- t.close
543
- open t.path, 'w+'
544
- end
545
- end
546
- def test_0330
547
- s = "foo\nbar\n"
548
- sio = StringIO.new s
549
- $buf = nil
550
- assert_nothing_raised{
551
- main{
552
- stdin sio
553
- def run
554
- $buf = STDIN.read
555
- end
556
- }
557
- }
558
- assert $buf == s
559
- end
560
- def test_0340
561
- s = "foo\nbar\n"
562
- $sio = StringIO.new s
563
- $buf = nil
564
- assert_nothing_raised{
565
- main{
566
- def run
567
- self.stdin = $sio
568
- $buf = STDIN.read
569
- end
570
- }
571
- }
572
- assert $buf == s
573
- end
574
- def test_0350
575
- s = "foo\nbar\n"
576
- $buf = nil
577
- assert_nothing_raised{
578
- main{
579
- stdin infile(s)
580
- def run
581
- $buf = STDIN.read
582
- end
583
- }
584
- }
585
- assert $buf == s
586
- end
587
- def test_0360
588
- sout = outfile
589
- assert_nothing_raised{
590
- main{
591
- stdout sout
592
- def run
593
- puts 42
594
- end
595
- }
596
- }
597
- assert test(?e, sout.path)
598
- assert IO.read(sout.path) == "42\n"
599
- end
600
- def test_0370
601
- m = nil
602
- assert_nothing_raised{
603
- m = main{
604
- stdout StringIO.new
605
- def run
606
- puts 42
607
- end
608
- }
609
- }
610
- assert m
611
- assert_nothing_raised{ m.stdout.rewind }
612
- assert m.stdout.read == "42\n"
613
- end
614
- #
615
- # main ctor
616
- #
617
- def test_0380
618
- argv = %w( a b c )
619
- $argv = nil
620
- assert_nothing_raised{
621
- main(argv){
622
- def run
623
- $argv = @argv
624
- end
625
- }
626
- }
627
- assert argv == $argv
628
- end
629
- def test_0390
630
- argv = %w( a b c )
631
- env = {'key' => 'val', 'foo' => 'bar'}
632
- $argv = nil
633
- $env = nil
634
- assert_nothing_raised{
635
- main(argv, env){
636
- def run
637
- $argv = @argv
638
- $env = @env
639
- end
640
- }
641
- }
642
- assert argv == $argv
643
- end
644
-
645
- #
646
- # negative/globbing arity
647
- #
648
- def test_4000
649
- m = nil
650
- argv = %w( a b c )
651
- assert_nothing_raised{
652
- main(argv.dup) {
653
- argument('zero_or_more'){ optional; arity -1 }
654
- run{ m = self }
655
- }
656
- }
657
- assert m.param['zero_or_more'].values == argv
658
- end
659
- def test_4010
660
- m = nil
661
- argv = %w( a b c )
662
- assert_nothing_raised{
663
- main(argv.dup) {
664
- argument('zero_or_more'){ optional; arity '*' }
665
- run{ m = self }
666
- }
667
- }
668
- assert m.param['zero_or_more'].values == argv
669
- end
670
- def test_4020
671
- m = nil
672
- argv = %w( a b c )
673
- assert_nothing_raised{
674
- main(argv.dup) {
675
- argument('one_or_more'){ arity -2 }
676
- run{ m = self }
677
- }
678
- }
679
- assert m.param['one_or_more'].values == argv
680
- end
681
- def test_4030
682
- m = nil
683
- argv = %w( a b c )
684
- assert_nothing_raised{
685
- main(argv.dup) {
686
- argument('two_or_more'){ arity -3 }
687
- run{ m = self }
688
- }
689
- }
690
- assert m.param['two_or_more'].values == argv
691
- end
692
- def test_4040
693
- m = nil
694
- argv = %w()
695
- assert_nothing_raised{
696
- main(argv.dup) {
697
- argument('zero_or_more'){ optional; arity -1 }
698
- run{ m = self }
699
- }
700
- }
701
- assert m.param['zero_or_more'].values == argv
702
- end
703
- def test_4050
704
- m = nil
705
- argv = %w()
706
- assert_raises(Main::Parameter::NotGiven){
707
- main(argv.dup) {
708
- argument('one_or_more'){ arity -2 }
709
- run{ m = self }
710
- }
711
- }
712
- end
713
- def test_4060
714
- m = nil
715
- argv = %w( a )
716
- assert_raises(Main::Parameter::Arity){
717
- main(argv.dup) {
718
- argument('two_or_more'){ arity -3 }
719
- run{ m = self }
720
- }
721
- }
722
- end
723
- def test_4070
724
- m = nil
725
- argv = %w( a )
726
- assert_raises(Main::Parameter::Arity){
727
- main(argv.dup) {
728
- argument('two_or_more'){ arity -4 }
729
- run{ m = self }
730
- }
731
- }
732
- end
733
- #
734
- # sub-command/mode functionality
735
- #
736
- def test_4080
737
- m = nil
738
- argv = %w( a b )
739
- assert_nothing_raised{
740
- main(argv.dup) {
741
- mode 'a' do
742
- argument 'b'
743
- run{ m = self }
744
- end
745
- }
746
- }
747
- assert m.param['b'].value == 'b'
748
- end
749
- def test_4090
750
- m = nil
751
- argv = %w( a b c )
752
- assert_nothing_raised{
753
- main(argv.dup) {
754
- mode 'a' do
755
- mode 'b' do
756
- argument 'c'
757
- run{ m = self }
758
- end
759
- end
760
- }
761
- }
762
- assert m.param['c'].value == 'c'
763
- end
764
-
765
- end
766
-
767
-