main 4.0.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -78,8 +78,8 @@ task :gemspec do
78
78
 
79
79
  spec.has_rdoc = #{ has_rdoc.inspect }
80
80
  spec.test_files = #{ test_files.inspect }
81
- spec.add_dependency 'fattr', '>= 1.0.3'
82
- spec.add_dependency 'arrayfields', '>= 4.5.0'
81
+ spec.add_dependency 'fattr', '>= 2.1.0'
82
+ spec.add_dependency 'arrayfields', '>= 4.7.4'
83
83
 
84
84
  spec.extensions.push(*#{ extensions.inspect })
85
85
 
data/a.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'main'
2
+
3
+ Main {
4
+ mode( :foo ) {
5
+ argument( :bar ) {
6
+ required
7
+ default 42
8
+ }
9
+ run {
10
+ puts "Doing something with #{params['bar'].value}"
11
+ }
12
+ }
13
+ }
14
+
data/lib/main.rb CHANGED
@@ -2,7 +2,7 @@ module Main
2
2
  #
3
3
  # top level constants
4
4
  #
5
- Main::VERSION = '4.0.0' unless
5
+ Main::VERSION = '4.2.0' unless
6
6
  defined? Main::VERSION
7
7
  def self.version() Main::VERSION end
8
8
 
@@ -44,25 +44,4 @@ module Main
44
44
  require libdir + 'mode'
45
45
  require libdir + 'program'
46
46
  require libdir + 'factories'
47
-
48
- class << Main
49
- def push_ios!
50
- @ios ||= []
51
- @ios.push({
52
- :STDIN => STDIN.dup, :STDOUT => STDOUT.dup, :STDERR => STDERR.dup
53
- })
54
- end
55
-
56
- def pop_ios!
57
- @ios ||= []
58
- (@ios.pop||{}).each do |const, dup|
59
- dst = eval(const.to_s)
60
- begin
61
- dst.reopen(dup)
62
- rescue
63
- ::Object.const_set(const, dup)
64
- end
65
- end
66
- end
67
- end
68
47
  end
@@ -1,20 +1,20 @@
1
1
  module Main
2
- def Main.factory(&block)
3
- Program.factory(&block)
4
- end
5
-
6
2
  def Main.create(&block)
7
3
  factory(&block)
8
4
  end
9
5
 
6
+ def Main.factory(&block)
7
+ Program.factory(&block)
8
+ end
9
+
10
10
  def Main.new(*args, &block)
11
- main_class = factory(&block).build(*args)
12
- main_class.new()
11
+ program = factory(&block).build(*args)
12
+ program.new()
13
13
  end
14
14
 
15
15
  def Main.run(*args, &block)
16
- main_class = factory(&block).build(*args)
17
- main = main_class.new()
16
+ program = factory(&block).build(*args)
17
+ main = program.new()
18
18
  main.run()
19
19
  end
20
20
  end
@@ -322,6 +322,8 @@ puts
322
322
  (argv.size - stop).times{ argv.pop }
323
323
  end
324
324
 
325
+ argv.push "--#{ argv.shift }" if argv.first == 'help'
326
+
325
327
  parse_options argv
326
328
 
327
329
  return 'help' if detect{|p| p.name.to_s == 'help' and p.given?}
@@ -7,6 +7,8 @@ module Main
7
7
  fattr('description')
8
8
  fattr('usage'){ Main::Usage.default_usage(self) }
9
9
  fattr('modes'){ Main::Mode.list }
10
+ fattr('depth_first_modes'){ Main::Mode.list }
11
+ fattr('breadth_first_modes'){ Main::Mode.list }
10
12
 
11
13
  fattr('author')
12
14
  fattr('version')
@@ -95,16 +97,18 @@ module Main
95
97
  # extend the class based on modules given in argv
96
98
  #
97
99
  def dynamically_extend_via_commandline_modes!
100
+ self.breadth_first_modes = modes.dup
98
101
  size = modes.size
99
- depth_first_modes = Array.fields
100
102
 
101
103
  loop do
102
104
  modes.each do |mode|
103
105
  arg = argv.first && %r/^#{ argv.first }/
104
106
  if arg and mode.name =~ arg
105
107
  argv.shift
106
- modes.clear
108
+ modes.clear()
109
+ breadth_first_modes.clear()
107
110
  evaluate(&mode)
111
+ self.breadth_first_modes = modes.dup
108
112
  depth_first_modes[mode.name] = mode
109
113
  break
110
114
  end
@@ -118,7 +122,7 @@ module Main
118
122
  break unless more_modes
119
123
  end
120
124
 
121
- self.modes = depth_first_modes
125
+ self.modes = depth_first_modes.dup
122
126
  end
123
127
 
124
128
  # wrap up users run method to handle errors, etc
@@ -134,7 +138,7 @@ module Main
134
138
  parse_parameters
135
139
 
136
140
  if help?
137
- puts(usage)
141
+ puts(usage.to_s)
138
142
  exit
139
143
  end
140
144
 
@@ -160,12 +164,12 @@ module Main
160
164
 
161
165
  # TODO
162
166
  def fully_qualified_mode
163
- modes.map{|mode| mode.name}.join(' ')
167
+ modes.map{|mode| mode.name}
164
168
  end
165
169
 
166
170
  def mode_name
167
171
  return 'main' if modes.empty?
168
- fully_qualified_mode
172
+ fully_qualified_mode.join(' ')
169
173
  end
170
174
 
171
175
  undef_method 'usage'
@@ -203,8 +207,9 @@ module Main
203
207
 
204
208
  def mode(name, &block)
205
209
  name = name.to_s
206
- modes[name] = block
207
210
  block.fattr(:name => name)
211
+ modes[name] = block
212
+ breadth_first_modes[name] = block
208
213
  block
209
214
  end
210
215
 
@@ -112,7 +112,8 @@ module Main
112
112
  STDIN.reopen(@stdin)
113
113
  rescue
114
114
  $stdin = @stdin
115
- ::Object.const_set('STDIN', @stdin)
115
+ ::Object.send(:remove_const, 'STDIN')
116
+ ::Object.send(:const_set, 'STDIN', @stdin)
116
117
  end
117
118
  end
118
119
  end
@@ -132,7 +133,8 @@ module Main
132
133
  STDOUT.reopen(@stdout)
133
134
  rescue
134
135
  $stdout = @stdout
135
- ::Object.const_set('STDOUT', @stdout)
136
+ ::Object.send(:remove_const, 'STDOUT')
137
+ ::Object.send(:const_set, 'STDOUT', @stdout)
136
138
  end
137
139
  end
138
140
  end
@@ -152,7 +154,8 @@ module Main
152
154
  STDERR.reopen(@stderr)
153
155
  rescue
154
156
  $stderr = @stderr
155
- ::Object.const_set('STDERR', @stderr)
157
+ ::Object.send(:remove_const, 'STDERR')
158
+ ::Object.send(:const_set, 'STDERR', @stderr)
156
159
  end
157
160
  end
158
161
  end
data/lib/main/test.rb ADDED
@@ -0,0 +1,89 @@
1
+
2
+ module Main
3
+ class << Main
4
+ def test(options = {}, &block)
5
+ # at_exit{ exit! }
6
+ opts = {}
7
+ options.each do |key, val|
8
+ opts[key.to_s.to_sym] = val
9
+ end
10
+ options.replace(opts)
11
+
12
+ argv = options[:argv]
13
+ env = options[:env]
14
+ logger = options[:logger]
15
+ stdin = options[:stdin]
16
+ stdout = options[:stdout]
17
+ stderr = options[:stderr]
18
+
19
+ Main.push_ios!
20
+
21
+ factory = Main.factory(&block)
22
+
23
+ program = factory.build(argv, env)
24
+
25
+ main = program.new
26
+
27
+ program.evaluate do
28
+ define_method :handle_exception do |exception|
29
+ if exception.respond_to?(:status)
30
+ main.status = exception.status
31
+ else
32
+ raise(exception)
33
+ end
34
+ end
35
+
36
+ define_method :handle_exit do |status|
37
+ main.status = status
38
+ end
39
+ end
40
+
41
+ keys = [:logger, :stdin, :stdout, :stderr]
42
+ keys.each do |key|
43
+ if options.has_key?(key)
44
+ val = options[key]
45
+ main.send("#{ key }=", val) if options.has_key?(key)
46
+ end
47
+ end
48
+
49
+ run = main.method(:run)
50
+
51
+ singleton_class =
52
+ class << main
53
+ self
54
+ end
55
+
56
+ singleton_class.module_eval do
57
+ define_method(:run) do
58
+ begin
59
+ run.call()
60
+ ensure
61
+ Main.pop_ios!
62
+ end
63
+ end
64
+ end
65
+
66
+ main
67
+ end
68
+
69
+ def push_ios!
70
+ @ios ||= []
71
+ @ios.push({
72
+ :STDIN => STDIN.dup, :STDOUT => STDOUT.dup, :STDERR => STDERR.dup
73
+ })
74
+ end
75
+
76
+ def pop_ios!
77
+ @ios ||= []
78
+ (@ios.pop||{}).each do |const, dup|
79
+ dst = eval(const.to_s)
80
+ begin
81
+ dst.reopen(dup)
82
+ rescue
83
+ ::Object.send(:remove_const, const)
84
+ ::Object.send(:const_set, const, dup)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
data/lib/main/usage.rb CHANGED
@@ -31,8 +31,12 @@ module Main
31
31
  s << " #{ main.fully_qualified_mode.join ' ' }"
32
32
  end
33
33
 
34
- unless main.modes.empty?
35
- modes = main.modes.keys.join('|')
34
+ #p 'main.breadth_first_modes' => main.breadth_first_modes.map{|m| m.name}
35
+ #p 'main.depth_first_modes' => main.depth_first_modes.map{|m| m.name}
36
+ #p 'main.modes' => main.modes.map{|m| m.name}
37
+
38
+ unless main.breadth_first_modes.empty?
39
+ modes = main.breadth_first_modes.map{|mode| mode.name}.join('|')
36
40
  s << " (#{ modes })"
37
41
  end
38
42
 
data/main.gemspec CHANGED
@@ -4,11 +4,11 @@
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "main"
6
6
  spec.description = 'a class factory and dsl for generating command line programs real quick'
7
- spec.version = "4.0.0"
7
+ spec.version = "4.2.0"
8
8
  spec.platform = Gem::Platform::RUBY
9
9
  spec.summary = "main"
10
10
 
11
- spec.files = ["lib", "lib/main", "lib/main/cast.rb", "lib/main/dsl.rb", "lib/main/factories.rb", "lib/main/getoptlong.rb", "lib/main/logger.rb", "lib/main/mode.rb", "lib/main/parameter.rb", "lib/main/program", "lib/main/program/class_methods.rb", "lib/main/program/instance_methods.rb", "lib/main/program.rb", "lib/main/softspoken.rb", "lib/main/stdext.rb", "lib/main/usage.rb", "lib/main/util.rb", "lib/main.rb", "main.gemspec", "Rakefile", "README", "README.erb", "samples", "samples/a.rb", "samples/b.rb", "samples/c.rb", "samples/d.rb", "samples/e.rb", "samples/f.rb", "samples/g.rb", "samples/h.rb", "test", "test/main.rb", "TODO"]
11
+ spec.files = ["a.rb", "lib", "lib/main", "lib/main/cast.rb", "lib/main/dsl.rb", "lib/main/factories.rb", "lib/main/getoptlong.rb", "lib/main/logger.rb", "lib/main/mode.rb", "lib/main/parameter.rb", "lib/main/program", "lib/main/program/class_methods.rb", "lib/main/program/instance_methods.rb", "lib/main/program.rb", "lib/main/softspoken.rb", "lib/main/stdext.rb", "lib/main/test.rb", "lib/main/usage.rb", "lib/main/util.rb", "lib/main.rb", "main.gemspec", "Rakefile", "README", "README.erb", "samples", "samples/a.rb", "samples/b.rb", "samples/c.rb", "samples/d.rb", "samples/e.rb", "samples/f.rb", "samples/g.rb", "samples/h.rb", "samples/j.rb", "test", "test/main.rb", "TODO"]
12
12
  spec.executables = []
13
13
 
14
14
 
@@ -17,8 +17,8 @@ Gem::Specification::new do |spec|
17
17
 
18
18
  spec.has_rdoc = true
19
19
  spec.test_files = "test/main.rb"
20
- spec.add_dependency 'fattr', '>= 1.0.3'
21
- spec.add_dependency 'arrayfields', '>= 4.5.0'
20
+ spec.add_dependency 'fattr', '>= 2.1.0'
21
+ spec.add_dependency 'arrayfields', '>= 4.7.4'
22
22
 
23
23
  spec.extensions.push(*[])
24
24
 
data/samples/j.rb ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ require 'main'
3
+
4
+ Main {
5
+ mode( :foo ) {
6
+ argument( :bar ) {
7
+ required
8
+ default 42
9
+ }
10
+ run {
11
+ puts "Doing something with #{params['bar'].value}"
12
+ }
13
+ }
14
+ }
data/test/main.rb CHANGED
@@ -6,6 +6,7 @@ $:.unshift('../lib')
6
6
  require('stringio')
7
7
  require('test/unit')
8
8
  require('main')
9
+ require('main/test')
9
10
 
10
11
 
11
12
  class T < Test::Unit::TestCase
@@ -23,45 +24,19 @@ class T < Test::Unit::TestCase
23
24
  end
24
25
 
25
26
  def main(argv=[], env={}, &block)
26
- at_exit{ exit! }
27
+ options = {}
28
+ options[:argv] = argv
29
+ options[:env] = env
30
+ options[:logger] = logger
27
31
 
28
- $VERBOSE=nil
29
- ARGV.replace argv
30
- ENV.clear
31
- env.each{|k,v| ENV[k.to_s]=v.to_s}
32
-
33
- Main.push_ios!
32
+ main = Main.test(options, &block)
34
33
 
35
34
  test = self
36
-
37
- factory = Main.factory(&block)
38
-
39
- program = factory.build(argv, env)
40
-
41
- main = program.new
42
-
43
- program.evaluate do
44
- define_method :handle_exception do |exception|
45
- if exception.respond_to?(:status)
46
- test.status = main.status = exception.status
47
- else
48
- test.status = main.status
49
- raise(exception)
50
- end
51
- end
52
- define_method :handle_exit do |status|
53
- test.status = main.status = status
54
- end
35
+ begin
36
+ main.run()
37
+ ensure
38
+ test.status = main.status
55
39
  end
56
-
57
- main.logger = logger
58
-
59
- main.run
60
-
61
- test.status ||= main.exit_status
62
-
63
- Main.pop_ios!
64
-
65
40
  main
66
41
  end
67
42
 
@@ -884,6 +859,20 @@ class T < Test::Unit::TestCase
884
859
  end
885
860
  end
886
861
 
862
+ def test_0550
863
+ name = 'mode_argument_with_help_parameter_outputs_help'
864
+ p = nil
865
+ argv = %w( foo help )
866
+ assert_nothing_raised{
867
+ main(argv){
868
+ mode( 'foo' ) {
869
+ argument 'bar'
870
+ define_method('run'){ p = param['bar'] }
871
+ }
872
+ }
873
+ }
874
+ assert( p.nil?, "p should not be set, help should have run" )
875
+ end
887
876
  end
888
877
 
889
878
 
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: 4.0.0
4
+ version: 4.2.0
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: 2009-10-20 00:00:00 -06:00
12
+ date: 2009-11-02 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.0.3
23
+ version: 2.1.0
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: arrayfields
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 4.5.0
33
+ version: 4.7.4
34
34
  version:
35
35
  description: a class factory and dsl for generating command line programs real quick
36
36
  email: ara.t.howard@gmail.com
@@ -41,6 +41,7 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
 
43
43
  files:
44
+ - a.rb
44
45
  - lib/main/cast.rb
45
46
  - lib/main/dsl.rb
46
47
  - lib/main/factories.rb
@@ -53,6 +54,7 @@ files:
53
54
  - lib/main/program.rb
54
55
  - lib/main/softspoken.rb
55
56
  - lib/main/stdext.rb
57
+ - lib/main/test.rb
56
58
  - lib/main/usage.rb
57
59
  - lib/main/util.rb
58
60
  - lib/main.rb
@@ -68,6 +70,7 @@ files:
68
70
  - samples/f.rb
69
71
  - samples/g.rb
70
72
  - samples/h.rb
73
+ - samples/j.rb
71
74
  - test/main.rb
72
75
  - TODO
73
76
  has_rdoc: true