Class: Escort::App

Inherits:
Object
  • Object
show all
Defined in:
lib/escort/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_string, &block) ⇒ App

Returns a new instance of App.



13
14
15
16
17
# File 'lib/escort/app.rb', line 13

def initialize(option_string, &block)
  @option_string = option_string
  @setup = nil
  @block = block
end

Instance Attribute Details

#option_stringObject (readonly)

Returns the value of attribute option_string.



11
12
13
# File 'lib/escort/app.rb', line 11

def option_string
  @option_string
end

#setupObject (readonly)

Returns the value of attribute setup.



11
12
13
# File 'lib/escort/app.rb', line 11

def setup
  @setup
end

Class Method Details

.create(option_string = '', &block) ⇒ Object



5
6
7
8
# File 'lib/escort/app.rb', line 5

def create(option_string = '', &block)
  self.new(option_string, &block).setup_application.execute
  exit(0)
end

Instance Method Details

#executeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/escort/app.rb', line 33

def execute
  begin
    AutoOptions.augment(setup)

    cli_options = current_cli_options(option_string)

    auto_options = Escort::GlobalPreParser.new(setup).parse(cli_options.dup)
    Escort::Logger.setup_error_logger(auto_options)

    #now we can start doing error logging everything above here has to be rock solid

    configuration = Escort::Setup::Configuration::Loader.new(setup, auto_options).configuration

    invoked_options, arguments = Escort::OptionParser.new(configuration, setup).parse(cli_options)
    context = context_from_options(invoked_options[:global])
    action = setup.action_for(context)
    current_command = context.empty? ? :global : context.last
    raise Escort::ClientError.new("No action defined for command '#{current_command}'") unless action
    actual_arguments = Escort::Arguments.read(arguments, setup.arguments_required_for(context))
  rescue => e
    handle_escort_error(e)
  end
  execute_action(action, invoked_options, actual_arguments, configuration.user)
end

#setup_applicationObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/escort/app.rb', line 19

def setup_application
  old_error_sev_threshold = error_logger.sev_threshold
  begin
    error_logger.sev_threshold = ::Logger::DEBUG
    cli_app_configuration = Escort::Setup::Dsl::Global.new(&@block)
    @setup = Escort::SetupAccessor.new(cli_app_configuration)
    self
  rescue => e
    handle_escort_error(e)
  ensure
    error_logger.sev_threshold = old_error_sev_threshold
  end
end