Class: Arli::CLI::App

Inherits:
Object
  • Object
show all
Includes:
Helpers::Output
Defined in:
lib/arli/cli/app.rb

Constant Summary

Constants included from Helpers::Output

Helpers::Output::CHAR_FAILURE, Helpers::Output::CHAR_SUCCESS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Output

#___, #__p, #__pf, #__pt, #abort?, #action_fail, #action_ok, #backup?, #cursor, #debug, #debug?, disable!, enable!, enabled?, #error, #fuck, #header, #hr, #indent_cursor, #info, #ok, #overwrite?, #print_action_failure, #print_action_starting, #print_action_success, #print_target_dir, #quiet?, #raise_invalid_arli_command!, #report_exception, #verbose?

Constructor Details

#initialize(argv, config: Arli.config) ⇒ App

Returns a new instance of App.



18
19
20
21
22
23
24
# File 'lib/arli/cli/app.rb', line 18

def initialize(argv, config: Arli.config)
  self.argv                      = argv
  self.config                    = config
  self.config.runtime.argv       = argv
  self.config.runtime.pwd        = ::Dir.pwd
  Arli.config.libraries.temp_dir = ::Dir.mktmpdir
end

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



16
17
18
# File 'lib/arli/cli/app.rb', line 16

def argv
  @argv
end

#commandObject

Returns the value of attribute command.



16
17
18
# File 'lib/arli/cli/app.rb', line 16

def command
  @command
end

#configObject

Returns the value of attribute config.



16
17
18
# File 'lib/arli/cli/app.rb', line 16

def config
  @config
end

Instance Method Details

#execute!Object



68
69
70
71
72
73
74
75
# File 'lib/arli/cli/app.rb', line 68

def execute!
  if command
    header(command: command) unless config.quiet
    command.run
  else
    factory.default_help
  end
end

#factoryObject



77
78
79
# File 'lib/arli/cli/app.rb', line 77

def factory
  Arli::CLI::ParserFactory
end

#parse_global_flagsObject



61
62
63
64
65
66
# File 'lib/arli/cli/app.rb', line 61

def parse_global_flags
  if argv.first && argv.first.start_with?('-')
    parser = factory.global_parser
    factory.parse_argv(parser, argv)
  end
end

#startObject



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
# File 'lib/arli/cli/app.rb', line 26

def start
  if argv.empty?
    factory.default_help
    factory.global_parser.print_version_copyright
    return
  end

  parse_global_flags

  return if Arli.config.help

  finder = CommandFinder.new(argv, config: config)
  finder.parse!
  return if Arli.config.help

  if finder.command
    self.command = finder.command
    execute!
  else
    factory.default_help
  end
rescue OptionParser::InvalidOption => e
  report_exception(e, 'Invalid flags or options')
rescue Arli::Errors::InvalidCommandError => e
  report_exception(e, 'Unknown command')
rescue Arli::Errors::InvalidSyntaxError => e
  report_exception(e, 'Incorrect command usage')
rescue Exception => e
  report_exception(e)
ensure
  d = Arli.config.libraries.temp_dir
  FileUtils.rm_rf(d) if Dir.exist?(d) rescue nil
  __pt
end