Class: Opsup::CLI

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/opsup/cli.rb

Defined Under Namespace

Classes: OptionBuilder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app:, option_builder:) ⇒ CLI

Returns a new instance of CLI.



19
20
21
22
# File 'lib/opsup/cli.rb', line 19

def initialize(app:, option_builder:)
  @app = T.let(app, Opsup::App)
  @option_builder = T.let(option_builder, Opsup::CLI::OptionBuilder)
end

Class Method Details

.createObject



11
12
13
14
15
16
# File 'lib/opsup/cli.rb', line 11

def self.create
  new(
    app: Opsup::App.create,
    option_builder: Opsup::CLI::OptionBuilder.create,
  )
end

Instance Method Details

#run(argv) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/opsup/cli.rb', line 25

def run(argv)
  parser = create_parser
  @option_builder.define_options(parser)

  options = {}
  begin
    # It automatically exits with a help message if necessary.
    commands = parser.parse(argv, into: options)
  rescue OptionParser::MissingArgument => e
    puts e.message
    return false
  end

  begin
    config = @option_builder.generate_config(options)
    @app.run(commands, config)
  rescue Opsup::Error => e
    puts "Error: #{e.message}"
    return false
  end

  true
end