Module: FindSharedLines::CommandLineInterface

Defined in:
lib/find_shared_lines/command_line_interface.rb

Defined Under Namespace

Modules: OPTION

Class Method Summary collapse

Class Method Details

.executeObject



51
52
53
54
55
56
57
58
59
# File 'lib/find_shared_lines/command_line_interface.rb', line 51

def self.execute
  option = parse_options

  if ARGV.empty?
    warn 'At least 1 file should be given.'
  else
    $stdout.puts result(option).sort
  end
end

.parse_optionsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/find_shared_lines/command_line_interface.rb', line 14

def self.parse_options
  banner = "USAGE: #{File.basename($PROGRAM_NAME)} [OPTION] [Files]..."
  option = OPTION::SHARED

  OptionParser.new(banner) do |opt|
    opt.on('-e', '--exclude-shared-lines',
           'Exclude lines commonly included in given files') do
      option = OPTION::EXCLUDE
    end

    opt.on('-s', '--shared-lines',
           'Collect lines shared in given files') do
      option = OPTION::SHARED
    end

    opt.on('-j', '--join-lines',
           'Collect all lines in given files') do
      option = OPTION::JOIN
    end

    opt.parse!
  end

  option
end

.result(option) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/find_shared_lines/command_line_interface.rb', line 40

def self.result(option)
  case option
  when OPTION::SHARED
    FindSharedLines.shared_lines(ARGV)
  when OPTION::EXCLUDE
    FindSharedLines.exclude_shared_lines(ARGV)
  when OPTION::JOIN
    FindSharedLines.join(ARGV)
  end
end