Class: KnapsackSolver::CliOptionChecker
- Inherits:
-
Object
- Object
- KnapsackSolver::CliOptionChecker
- Defined in:
- lib/knapsack_solver/cli_option_checker.rb
Overview
This class checks command line arguments provided to the knapsack_solver binary.
Class Method Summary collapse
-
.check(opts, args) ⇒ Object
Checks command-line options, their arguments and positional arguments provided to the CLI.
-
.check_directories(opts) ⇒ Object
Checks directory for result and statistic output logs, and directory for graph files.
-
.check_fptas_options(opts) ⇒ Object
Checks command-line options and arguments used by FPTAS solving method.
-
.check_incomplete_fptas_options(opts) ⇒ Object
Checks command-line options and arguments used by FPTAS solving method.
-
.check_input_file(path) ⇒ Object
Checks if an input file exist and is readable.
-
.check_output_directory(path) ⇒ Object
Checks if an output directory exist and is writable.
-
.check_positional_arguments(args) ⇒ Object
Checks if at least one dataset input file was provided and if the input files are readable.
Class Method Details
.check(opts, args) ⇒ Object
Checks command-line options, their arguments and positional arguments provided to the CLI.
12 13 14 15 16 17 18 19 20 |
# File 'lib/knapsack_solver/cli_option_checker.rb', line 12 def self.check(opts, args) if !opts[:branch_and_bound] && !opts[:dynamic_programming] && !opts[:fptas] && !opts[:heuristic] raise StandardError, 'At least one method of solving must be requested' end (opts) check_directories(opts) check_positional_arguments(args) end |
.check_directories(opts) ⇒ Object
Checks directory for result and statistic output logs, and directory for graph files.
51 52 53 54 |
# File 'lib/knapsack_solver/cli_option_checker.rb', line 51 def self.check_directories(opts) check_output_directory(opts[:output_dir]) if opts[:output_dir] check_output_directory(opts[:graphs_dir]) if opts[:graphs_dir] end |
.check_fptas_options(opts) ⇒ Object
Checks command-line options and arguments used by FPTAS solving method.
25 26 27 28 29 30 31 32 |
# File 'lib/knapsack_solver/cli_option_checker.rb', line 25 def self.(opts) return if !opts[:fptas] && !opts.key?(:fptas_epsilon) (opts) eps = opts[:fptas_epsilon].to_f return unless eps <= 0 || eps >= 1 || eps.to_s != opts[:fptas_epsilon] raise StandardError, 'FPTAS epsilon must be number from range (0,1)' end |
.check_incomplete_fptas_options(opts) ⇒ Object
Checks command-line options and arguments used by FPTAS solving method. Recignizes cases when mandatory FPTAS epsilon constant is missing or when it the constant is provided and FPTAS method is not requested.
40 41 42 43 44 45 |
# File 'lib/knapsack_solver/cli_option_checker.rb', line 40 def self.(opts) raise StandardError, 'Missing FPTAS epsilon constant' if opts[:fptas] && !opts.key?(:fptas_epsilon) return unless !opts[:fptas] && opts.key?(:fptas_epsilon) raise StandardError, 'epsilon constant must not be provided when FPTAS is not selected' end |
.check_input_file(path) ⇒ Object
Checks if an input file exist and is readable.
77 78 79 80 81 |
# File 'lib/knapsack_solver/cli_option_checker.rb', line 77 def self.check_input_file(path) raise StandardError, "File '#{path}' does not exists" unless File.exist?(path) raise StandardError, "'#{path}' is not a regular file" unless File.file?(path) raise StandardError, "File '#{path}' is not readable" unless File.readable?(path) end |
.check_output_directory(path) ⇒ Object
Checks if an output directory exist and is writable.
68 69 70 71 72 |
# File 'lib/knapsack_solver/cli_option_checker.rb', line 68 def self.check_output_directory(path) raise StandardError, "Directory '#{path}' does not exists" unless File.exist?(path) raise StandardError, "'#{path}' is not a directory" unless File.directory?(path) raise StandardError, "Directory '#{path}' is not writable" unless File.writable?(path) end |
.check_positional_arguments(args) ⇒ Object
Checks if at least one dataset input file was provided and if the input files are readable.
60 61 62 63 |
# File 'lib/knapsack_solver/cli_option_checker.rb', line 60 def self.check_positional_arguments(args) raise StandardError, 'Missing datset file(s)' if args.empty? args.each { |f| check_input_file(f) } end |