Sha256: 3321087229690db2a8bcb0ed814a280d5ee1a27ff644a845f33572f8b116b472

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require_relative 'version'

module Gnomika
  ##
  # Class used to parse command line arguments
  class ArgParser

    ##
    # Contains the application options
    class GnomikaOptions
      attr_accessor :custom_output_dir_set, :custom_output_dir_value, :single_file, :single_file_name, :list_only

      def initialize
        self.custom_output_dir_set = false
        self.custom_output_dir_value = ""
        self.single_file = false
        self.single_file_name = ""
      end
    end

    ##
    # Parses the given option into a struct
    # @param arguments Array of parameters given to the program
    # @return A GnomikaOptions object
    def self.parse(arguments)
      options = GnomikaOptions.new
      parser = create_parser(options)
      parser.parse(arguments)
      options
    end

    class << self
      private

      ##
      # Creates a parser that places values in the given object
      # @param options The object to place the values in
      def create_parser(options)
        parser = OptionParser.new

        parser.on "-o DIR", "--output-dir DIR", "Specify custom output directory" do |value|
          options.custom_output_dir_set = true
          options.custom_output_dir_value = value
        end
        parser.on "-s FILENAME", "--single-file FILENAME", "Output all quotes in a single file" do |name|
          options.single_file = true
          options.single_file_name = name
        end
        parser.on "-l", "--list-categories", "List all available categories" do
          options.list_only = true
        end

        parser.on "-h", "--help", "Print this help text" do
          puts parser
          exit
        end

        parser.on "-v", "--version", "Print program version" do
          puts "gnomikologikon-fortune #{Gnomika::Ruby::VERSION}"
          exit
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gnomikologikon-fortune-1.0.3 lib/gnomikologikon/arg_parser.rb
gnomikologikon-fortune-1.0.2 lib/gnomikologikon/arg_parser.rb
gnomikologikon-fortune-1.0.1 lib/gnomikologikon/arg_parser.rb