Sha256: 314bcb9b96a6bcb58542670ef7c60c9d61ede52d12e1f5040cf25fbd7f744ce1

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require "optparse"

module AlacrittyThemes
  # Arguments parser
  module Parser
    class << self
      def from(argv)
        options = {}

        options[:parser] = parser
        add_banner
        add_create_option(options)
        add_version_option(options)
        add_help_option(options)
        parser.parse!(argv)

        options
      rescue OptionParser::ParseError => e
        { command: :error, message: e.message }
      end

      def parser
        @parser ||= OptionParser.new
      end

      def add_create_option(options)
        parser.on("-c", "--create", "Creates file") do
          options[:command] = :create
          options[:message] = "Creating Alacritty file"
        end
      end

      def add_version_option(options)
        parser.on("-v", "--version", "Shows version") do
          options[:command] = :version
          options[:message] = "Alacritty Themes v#{AlacrittyThemes::VERSION}"
        end
      end

      def add_help_option(options)
        parser.on("-h", "--help", "Shows help") do
          options[:command] = :help
          options[:message] = parser.help
        end
      end

      def add_banner
        parser.banner = "Usage: alacritty_themes [options]"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alacritty_themes-0.1.3 lib/alacritty_themes/parser.rb
alacritty_themes-0.1.2 lib/alacritty_themes/parser.rb