Class: BigFiles::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/bigfiles/option_parser.rb

Overview

Parse options passed to bigfiles command

Instance Method Summary collapse

Constructor Details

#initialize(source_finder_option_parser:, option_parser_class: ::OptionParser, io_class: Kernel, exiter: Kernel) ⇒ OptionParser

Returns a new instance of OptionParser.



8
9
10
11
12
13
14
15
# File 'lib/bigfiles/option_parser.rb', line 8

def initialize(source_finder_option_parser:, option_parser_class: ::OptionParser,
               io_class: Kernel,
               exiter: Kernel)
  @option_parser_class = option_parser_class
  @io_class = io_class
  @exiter = exiter
  @source_finder_option_parser = source_finder_option_parser
end

Instance Method Details

#add_help_option(opts, options) ⇒ Object



35
36
37
# File 'lib/bigfiles/option_parser.rb', line 35

def add_help_option(opts, options)
  opts.on('-h', '--help', 'This message') { |_| options[:help] = true }
end

#add_num_files_option(opts, options) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/bigfiles/option_parser.rb', line 26

def add_num_files_option(opts, options)
  opts.on('-n', '--num-files number-here',
          Integer,
          "Top number of files to show--" \
          "default #{Config::NUM_FILES_DEFAULT}") do |n|
    options[:num_files] = n
  end
end

#parse_options(args) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/bigfiles/option_parser.rb', line 39

def parse_options(args)
  options = nil
  @option_parser_class.new do |opts|
    options = setup_options(opts)
    @option_parser = opts
  end.parse!(args)
  options
end

#setup_options(opts) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/bigfiles/option_parser.rb', line 17

def setup_options(opts)
  options = {}
  opts.banner = 'Usage: bigfiles [options]'
  @source_finder_option_parser.add_options(opts, options)
  add_help_option(opts, options)
  add_num_files_option(opts, options)
  options
end

#usageObject



48
49
50
51
# File 'lib/bigfiles/option_parser.rb', line 48

def usage
  @io_class.puts @option_parser
  @exiter.exit(1)
end