Class: FlatKit::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/flat_kit/cli.rb

Overview

Public: The the main entry point for the command line interface

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



12
13
14
# File 'lib/flat_kit/cli.rb', line 12

def initialize
  @parser = nil
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/flat_kit/cli.rb', line 10

def options
  @options
end

Class Method Details

.commands_bannerObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/flat_kit/cli.rb', line 46

def self.commands_banner
  sorted_commands = FlatKit::Command.children.sort_by(&:name)
  left_width = sorted_commands.map { |c| c.name.length }.max
  banner = StringIO.new
  banner.puts
  banner.puts "Commands:"
  banner.puts

  sorted_commands.each do |command|
    banner.puts "  #{command.name.ljust(left_width)}    #{command.description}"
  end
  banner.string
end

Instance Method Details

#parserObject



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
# File 'lib/flat_kit/cli.rb', line 16

def parser
  @parser ||= ::Optimist::Parser.new do
    version ::FlatKit::VERSION

    banner "fk v#{version}"

    banner <<~USAGE

      Usage:
        fk <command> [<args>...]
        fk [options]
    USAGE

    banner <<~OPTIONS

      Options:

    OPTIONS

    opt :verbose, "Force debug. Output lots of informtion to standard error", default: false
    opt :list, "List all the commands", default: false, short: :none
    opt :log, "Set the logger output location", default: "<stderr>", short: :none
    opt :help, "Show help message", short: :h
    opt :version, "Print version and exit", short: :none

    stop_on FlatKit::Command.names
    banner Cli.commands_banner
  end
end

#run(argv: ARGV, env: ENV) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/flat_kit/cli.rb', line 60

def run(argv: ARGV, env: ENV)
  opts = parse_opts(argv)
  init_logging(opts)
  ::FlatKit.logger.debug(argv)

  command_name = argv.shift
  exit_if_help(command_name)

  command_klass = command_klass_or_exit(command_name)
  command = command_klass.new(argv: argv, logger: ::FlatKit.logger, env: env)
  command.call
end