Module: Main

Defined in:
lib/main/getoptlong.rb,
lib/main.rb,
lib/main/cast.rb,
lib/main/mode.rb,
lib/main/test.rb,
lib/main/util.rb,
lib/main/usage.rb,
lib/main/daemon.rb,
lib/main/logger.rb,
lib/main/program.rb,
lib/main/factories.rb,
lib/main/parameter.rb,
lib/main/softspoken.rb,
lib/main/program/class_methods.rb,
lib/main/program/instance_methods.rb

Overview

Parse command line options just like GNU getopt_long().

Defined Under Namespace

Modules: Cast, Softspoken, Util Classes: Daemon, GetoptLong, Logger, Mode, Parameter, Program, Usage

Constant Summary collapse

VERSION =

top level constants

'6.2.3'
LIBDIR =
File.join(File.dirname(File.expand_path(__FILE__)), self.name.downcase, '')
EXIT_SUCCESS =
0
EXIT_FAILURE =
1
EXIT_WARN =
42
EXIT_WARNING =
42

Class Method Summary collapse

Class Method Details

.create(&block) ⇒ Object


2
3
4
# File 'lib/main/factories.rb', line 2

def Main.create(&block)
  factory(&block)
end

.dependenciesObject

deps


22
23
24
25
26
27
28
29
# File 'lib/main.rb', line 22

def Main.dependencies
  {
    'chronic'     => [ 'chronic', '~> 0.6', '>= 0.6.2' ] ,
    'fattr'       => [ 'fattr', '~> 2.2', '>= 2.2.0' ] ,
    'arrayfields' => [ 'arrayfields', '~> 4.7', '>= 4.7.4' ] ,
    'map'         => [ 'map', '~> 6.1', '>= 6.1.0' ] ,
  }
end

.descriptionObject


8
9
10
# File 'lib/main.rb', line 8

def Main.description
  'a class factory and dsl for generating command line programs real quick'
end

.factory(&block) ⇒ Object


6
7
8
# File 'lib/main/factories.rb', line 6

def Main.factory(&block)
  Program.factory(&block)
end

.libdir(*args, &block) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/main.rb', line 31

def Main.libdir(*args, &block)
  @libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
  args.empty? ? @libdir : File.join(@libdir, *args)
ensure
  if block
    begin
      $LOAD_PATH.unshift(@libdir)
      block.call()
    ensure
      $LOAD_PATH.shift()
    end
  end
end

.load(*libs) ⇒ Object


45
46
47
48
# File 'lib/main.rb', line 45

def Main.load(*libs)
  libs = libs.join(' ').scan(/[^\s+]+/)
  Main.libdir{ libs.each{|lib| Kernel.load(lib) } }
end

.new(*args, &block) ⇒ Object


10
11
12
13
# File 'lib/main/factories.rb', line 10

def Main.new(*args, &block)
  program = factory(&block).build(*args)
  program.new()
end

.pop_ios!Object


76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/main/test.rb', line 76

def pop_ios!
  @ios ||= []
  (@ios.pop||{}).each do |const, dup|
    dst = eval(const.to_s)
    begin
      dst.reopen(dup)
    rescue
      ::Object.send(:remove_const, const)
      ::Object.send(:const_set, const, dup)
    end
  end
end

.push_ios!Object


69
70
71
72
73
74
# File 'lib/main/test.rb', line 69

def push_ios!
  @ios ||= []
  @ios.push({
    :STDIN => STDIN.dup, :STDOUT => STDOUT.dup, :STDERR => STDERR.dup
  })
end

.run(*args, &block) ⇒ Object


15
16
17
18
19
# File 'lib/main/factories.rb', line 15

def Main.run(*args, &block)
  program = factory(&block).build(*args)
  main = program.new()
  main.run()
end

.test(options = {}, &block) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/main/test.rb', line 4

def test(options = {}, &block)
#    at_exit{ exit! }
  opts = {}
  options.each do |key, val|
    opts[key.to_s.to_sym] = val
  end
  options.replace(opts)

  argv = options[:argv]
  env = options[:env]
  #logger = options[:logger]
  #stdin = options[:stdin]
  #stdout = options[:stdout]
  #stderr = options[:stderr]

  Main.push_ios!

  factory = Main.factory(&block)

  program = factory.build(argv, env)

  main = program.new

  program.evaluate do
    define_method :handle_exception do |exception|
      if exception.respond_to?(:status)
        main.status = exception.status
      else
        raise(exception)
      end
    end

    define_method :handle_exit do |status|
      main.status = status
    end
  end

  keys = [:logger, :stdin, :stdout, :stderr]
  keys.each do |key|
    if options.has_key?(key)
      val = options[key]
      main.send("#{ key }=", val) if options.has_key?(key)
    end
  end

  run = main.method(:run)

  singleton_class =
    class << main
      self
    end

  singleton_class.module_eval do
    define_method(:run) do
      begin
        run.call()
      ensure
        Main.pop_ios!
      end
    end
  end

  main
end

.versionObject


6
# File 'lib/main.rb', line 6

def Main.version() Main::VERSION end