Class: KnapsackSolver::CliOptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/knapsack_solver/cli_option_parser.rb

Overview

This class parses command line arguments provided to the knapsack_solver binary.

Constant Summary collapse

USAGE_MESSAGE =

Message that describes how to use this CLI utility.

'Usage: knapsack_solver OPTIONS DATASET_FILE...'.freeze

Class Method Summary collapse

Class Method Details

.parse(arguments) ⇒ Hash

Parses command-line arguments and removes them from the array of arguments.

rubocop:disable Metrics/AbcSize, Metric/MethodLength, Metric/BlockLength

Parameters:

  • arguments (Array)

    the command-line arguments.

Returns:

  • (Hash)

    hash of recognized options.



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
45
46
47
48
49
50
51
52
# File 'lib/knapsack_solver/cli_option_parser.rb', line 18

def self.parse(arguments)
  options = {}
  parser = OptionParser.new do |opts|
    opts.banner = USAGE_MESSAGE
    opts.on('-b', '--branch-and-bound', 'Use branch and boung method of solving') do
      options[:branch_and_bound] = true
    end
    opts.on('-d', '--dynamic-programming', 'Use dynamic programming for solving') do
      options[:dynamic_programming] = true
    end
    opts.on('-f', '--fptas', 'Use FPTAS for solving') do
      options[:fptas] = true
    end
    opts.on('-r', '--heuristic', 'Use brute force method of solving') do
      options[:heuristic] = true
    end
    opts.on('-e', '--fptas-epsilon EPS', 'Relative error for FPTAS from range (0,1)') do |eps|
      options[:fptas_epsilon] = eps
    end
    opts.on('-o', '--output DIR', 'Directory for output log files') do |dir|
      options[:output_dir] = dir
    end
    opts.on('-g', '--graphs DIR', 'Directory for graphs') do |dir|
      options[:graphs_dir] = dir
    end
    opts.on('-v', '--version', 'Show program version') do
      options[:version] = true
    end
    opts.on_tail('-h', '--help', 'Show this help message') do
      options[:help] = true
    end
  end
  parser.parse!(arguments)
  process_help_and_version_opts(options, arguments, parser.to_s)
end

.process_help_and_version_opts(options, arguments, usage_msg) ⇒ Object

rubocop:enable Metrics/AbcSize, Metric/MethodLength, Metric/BlockLength



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/knapsack_solver/cli_option_parser.rb', line 55

def self.process_help_and_version_opts(options, arguments, usage_msg)
  if !options[:help] && !options[:version]
    CliOptionChecker.check(options, arguments)
    return options
  end
  if options[:help]
    puts usage_msg
  elsif options[:version]
    puts "knapsack_solver #{KnapsackSolver::VERSION}"
  end
  nil
end