Class: WordCountRecursive::ArgumentsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/wcr/arguments_parser.rb

Instance Method Summary collapse

Instance Method Details

#parseObject



5
6
7
8
9
10
11
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
37
38
39
40
41
42
43
44
# File 'lib/wcr/arguments_parser.rb', line 5

def parse
  options = {command_opts: "", hidden_files: "| grep -v '/\\.'"}

  o = ::OptionParser.new do |opts|
    opts.banner = "usage: wc [-clhmw] [file ...]"
    
    opts.on("-c") do |c|
      options[:command_opts] += " -c"
    end

    opts.on("-h") do |h|
      options[:hidden_files] = ""
    end

    opts.on("-l") do |l|
      options[:command_opts] += " -l"
    end

    opts.on("-m") do |m|
      options[:command_opts] += " -m"
    end

    opts.on("-w") do |w|
      options[:command_opts] += " -w"
    end

    opts.on("-v", "--version") do |v|
      options[:version] = "wcr - v#{::WordCountRecursive::VERSION}"
    end
  end
begin
  o.parse!
rescue ::OptionParser::InvalidOption => e
  $stderr.puts e
  $stderr.puts o.banner
  exit
end
# TODO: ugly code to refactor :(
  [(ARGV.length > 0 ? ARGV.join(' ') : nil), options]
end