Class: Recheck::Cli

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

Constant Summary collapse

EXIT_CODE =
{
  no_errors: 0,     # all checks passed
  any_errors: 1,    # any check returns fail or threw an exception
  load_error: 2,    # error loading checker/reporter
  recheck_error: 3  # recheck itself encountered an error
}.freeze
COMMANDS =
{
  reporters: "List available reporters",
  run: "Run checks",
  setup: "Set up a new check suite in the current directory"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(argv: []) ⇒ Cli

Returns a new instance of Cli.



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

def initialize(argv: [])
  @argv = argv
end

Instance Method Details

#runObject



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
45
# File 'lib/recheck/cli.rb', line 20

def run
  global_options = Recheck::Optimist.options(@argv) do
    version "recheck v#{Recheck::VERSION}"

    banner "Usage:"
    banner "  recheck [global options] [<command> [options]]"

    banner "\nGlobal options:"
    opt :version, "Print version and exit", short: :v
    opt :help, "Print help", short: :h
    stop_on COMMANDS.keys.map(&:to_s)

    banner "\nCommands:"
    COMMANDS.each { |c, desc| banner format("  %-10s %s", c, desc) }
  end

  command = global_options[:_leftovers].shift&.to_sym || :help
  Recheck::Optimist.die "unknown command '#{command}'" unless COMMANDS.include? command

  Recheck::Command.const_get(command.to_s.split("_").map(&:capitalize).join("")).new(argv: global_options[:_leftovers]).run

  exit EXIT_CODE[:no_errors]
rescue Interrupt
  puts "\nOperation cancelled by user."
  exit EXIT_CODE[:recheck_error]
end