Class: KnapsackSolver::CliOptionParser
- Inherits:
-
Object
- Object
- KnapsackSolver::CliOptionParser
- 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
-
.parse(arguments) ⇒ Hash
Parses command-line arguments and removes them from the array of arguments.
-
.process_help_and_version_opts(options, arguments, usage_msg) ⇒ Object
rubocop:enable Metrics/AbcSize, Metric/MethodLength, Metric/BlockLength.
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
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) = {} parser = OptionParser.new do |opts| opts. = USAGE_MESSAGE opts.on('-b', '--branch-and-bound', 'Use branch and boung method of solving') do [:branch_and_bound] = true end opts.on('-d', '--dynamic-programming', 'Use dynamic programming for solving') do [:dynamic_programming] = true end opts.on('-f', '--fptas', 'Use FPTAS for solving') do [:fptas] = true end opts.on('-r', '--heuristic', 'Use brute force method of solving') do [:heuristic] = true end opts.on('-e', '--fptas-epsilon EPS', 'Relative error for FPTAS from range (0,1)') do |eps| [:fptas_epsilon] = eps end opts.on('-o', '--output DIR', 'Directory for output log files') do |dir| [:output_dir] = dir end opts.on('-g', '--graphs DIR', 'Directory for graphs') do |dir| [:graphs_dir] = dir end opts.on('-v', '--version', 'Show program version') do [:version] = true end opts.on_tail('-h', '--help', 'Show this help message') do [:help] = true end end parser.parse!(arguments) process_help_and_version_opts(, 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(, arguments, usage_msg) if ![:help] && ![:version] CliOptionChecker.check(, arguments) return end if [:help] puts usage_msg elsif [:version] puts "knapsack_solver #{KnapsackSolver::VERSION}" end nil end |