Class: RockBooks::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/rock_books/cmd_line/main.rb

Instance Method Summary collapse

Instance Method Details

#callObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rock_books/cmd_line/main.rb', line 84

def call
  begin
    ARGV << '-h' if ARGV.empty?
    run_options = parse_command_line
    CommandLineInterface.new(run_options).call
  rescue => error
    $stderr.puts  \
    <<~HEREDOC
    #{error.backtrace.join("\n")}

    #{'-' * 79}
    #{error}
    #{'-' * 79}

    HEREDOC

    exit(-1)
    binding.pry
    raise error
  end

end

#options_with_defaultsObject



15
16
17
18
19
20
21
22
# File 'lib/rock_books/cmd_line/main.rb', line 15

def options_with_defaults
  options = OpenStruct.new
  options.input_dir   = DEFAULT_INPUT_DIR
  options.output_dir  = DEFAULT_OUTPUT_DIR
  options.receipt_dir = DEFAULT_RECEIPT_DIR
  options.do_receipts = true
  options
end

#parse_command_lineObject

Parses the command line with Ruby’s internal ‘optparse’. OptionParser#parse! removes what it processes from ARGV, which simplifies our command parsing.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rock_books/cmd_line/main.rb', line 36

def parse_command_line
  prepend_environment_options
  options = options_with_defaults

  OptionParser.new do |parser|

    parser.on("-h", "--help", "Show help") do |_help_requested|
      ARGV << 'h' # pass on the request to the command processor
      options.suppress_command_line_validation = true
    end

    parser.on('-i', '--input_dir DIR',
        "Input directory containing source data files, default: '#{DEFAULT_INPUT_DIR}'") do |v|
      options.input_dir = File.expand_path(v)
    end

    parser.on('-o', '--output_dir DIR',
        "Output directory to which report files will be written, default: '#{DEFAULT_OUTPUT_DIR}'") do |v|
      options.output_dir = File.expand_path(v)
    end

    parser.on('-r', '--receipt_dir DIR',
        "Directory root from which to find receipt filespecs, default: '#{DEFAULT_RECEIPT_DIR}'") do |v|
      options.receipt_dir = File.expand_path(v)
    end

    parser.on('-s', '--shell', 'Start interactive shell') do |v|
      options.interactive_mode = true
    end

    parser.on('-v', '--[no-]verbose', 'Verbose mode') do |v|
      options.verbose_mode = v
    end

    parser.on('', '--[no-]receipts', 'Include report on existing and missing receipts.') do |v|
      options.do_receipts = v
    end
  end.parse!

  if options.verbose_mode
    puts "Run Options:"
    ap options.to_h
  end

  options
end

#prepend_environment_optionsObject



25
26
27
28
29
30
31
# File 'lib/rock_books/cmd_line/main.rb', line 25

def prepend_environment_options
  env_opt_string = ENV['ROCKBOOKS_OPTIONS']
  if env_opt_string
    args_to_prepend = Shellwords.shellsplit(env_opt_string)
    ARGV.unshift(args_to_prepend).flatten!
  end
end