Class: CheckPlease::CLI::Runner

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

Instance Method Summary collapse

Constructor Details

#initialize(exe_file_name) ⇒ Runner

Returns a new instance of Runner.



5
6
7
# File 'lib/check_please/cli/runner.rb', line 5

def initialize(exe_file_name)
  @parser = Parser.new(exe_file_name)
end

Instance Method Details

#run(*args) ⇒ Object

NOTE: unusually for me, I’m using Ruby’s ‘or` keyword in this method. `or` short circuits just like `||`, but has lower precedence, which enables some shenanigans…



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/check_please/cli/runner.rb', line 12

def run(*args)
  args.flatten!
  print_help_and_exit if args.empty?

  begin
    flags = @parser.flags_from_args!(args)
  rescue InvalidFlag => e
    print_help_and_exit e.message
  end

  # The reference MUST be the first arg...
  reference = \
    read_file(args.shift) \
    or print_help_and_exit "Missing <reference> argument"

  # The candidate MAY be the second arg, or it might have been piped in...
  candidate = \
    read_file(args.shift) \
    || read_piped_stdin \
    or print_help_and_exit "Missing <candidate> argument, AND nothing was piped in"

  # Looks like we're good to go!
  diff_view = CheckPlease.render_diff(reference, candidate, flags)
  puts diff_view
end