Class: Applix

Inherits:
Object
  • Object
show all
Defined in:
lib/applix.rb,
lib/applix/version.rb

Overview

gem version is extracted automatically from this class during the build process

Constant Summary collapse

VERSION =
'0.4.14'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.main(argv, app_defaults = {}, &blk) ⇒ Object

prints primitve usage in case of error,



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/applix.rb', line 10

def self.main argv, app_defaults = {}, &blk
  self.main!(argv, app_defaults, &blk)

rescue => e
  puts <<-TXT

 ## #{e}

usage: #{$0} <args...>

  TXT
end

.main!(argv, app_defaults = {}, &blk) ⇒ Object

raises exception on error

dumps callstack in case of error when --debug is enabled


26
27
28
29
30
31
32
33
34
35
# File 'lib/applix.rb', line 26

def self.main! argv, app_defaults = {}, &blk
  app = Applix.new(app_defaults.merge(Hash.from_argv argv))
  app.instance_eval(&blk)
  app.run(argv, app_defaults, &blk)

rescue => e
  #app.debug? and (puts %[ !! #{e}:\n#{e.backtrace.join "\n"}])
  (puts %[ !! #{e}:\n#{e.backtrace.join "\n"}]) if app.debug? 
  raise
end

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/applix.rb', line 37

def debug?
  @options[:debug] == true
end

#run(argv, defaults = {}) ⇒ Object



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/applix.rb', line 41

def run argv, defaults = {}
  # run defaults are overloaded with argv command line options
  run_options = defaults.merge(Hash.from_argv argv)
  args = (run_options.delete :args)

  # pre handle, can modify args & options
  @prolog_cb.call(args, run_options) unless @prolog_cb.nil?

  # logic table for dispatching the command line onto an action
  #
  # id | name  exits? any | action
  # -- | -----------------+--------------
  #  1 |  -            -  | error: no any, mapped to #3 with name == :any
  #  2 |  -            x  | -> any
  #  3 |  x     -      -  | error: no any
  #  4 |  x     -      x  | -> any
  #  5 |  x     x      -  | -> task
  #  6 |  x     x      x  | -> task
  #
  # having name with no task is the same as no name with no any task..
  name = (args.shift || :any).to_sym
  # shoose existing task or :any
  task = tasks[name] || tasks[:any]
  task or (raise "no such task: '#{name}'")

  # case #4: we must un-shift the name back into the args list to let :any
  # still sees it as first argument,
  (args.unshift name.to_s) if(name != :any && task[:name] == :any)

  # cluster for nesting or direct calling?
  if task[:cluster]
    #rc = Applix.main(args, options, &task[:code])
    cluster_task = task[:name].to_sym
    cluster_options = run_options.merge(run_options[cluster_task] || {})
    cluster_options.delete(cluster_task)
    cluster_options.merge!(Hash.from_argv argv)
    rc = Applix.main(args, cluster_options, &task[:code])
  else
    rc = task[:code].call(*args, run_options)
  end

  # post handle
  unless @epilog_cb.nil?
    rc = @epilog_cb.call(rc, args, run_options)
  end

  rc # return result code from handle callbacks, not the epilog_cb
end