Sha256: b8a64adc17f0eaf656b4d8375df15dff1f1158809c7901c2c2b7fa681c32dce8

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require "optparse"
require "refinements/hashes"

module Rubysmith
  module CLI
    # Represents the Command Line Interface (CLI) for this gem.
    class Shell
      using Refinements::Hashes

      PROCESSORS = {
        config: Processors::Config.new,
        build_minimum: Processors::Build.new(builders: Processors::Build::MINIMUM),
        build_maximum: Processors::Build.new
      }.freeze

      def initialize parser: Parsers::Assembler.new, processors: PROCESSORS
        @parser = parser
        @processors = processors
      end

      def call arguments = []
        parse arguments

        # rubocop:disable Style/MethodCallWithArgsParentheses
        case options
          in config: action, **remainder then config action
          in build_minimum: true, **remainder then build(:build_minimum, options)
          in build:, **remainder then build(:build_maximum, options)
          in version:, **remainder then puts version
          in help:, **remainder then usage
          else usage
        end
        # rubocop:enable Style/MethodCallWithArgsParentheses
      end

      private

      attr_reader :parser, :processors, :exceptions

      def parse arguments = []
        parser.call arguments
      rescue StandardError => error
        puts error.message
      end

      def config action
        processors.fetch(__method__).call action
      end

      def build kind, settings
        processors.fetch(kind).call settings.rekey(build: :project_name).merge(now: Time.now)
      end

      def options
        parser.to_h
      end

      def usage
        puts parser.to_s
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubysmith-0.5.0 lib/rubysmith/cli/shell.rb
rubysmith-0.4.0 lib/rubysmith/cli/shell.rb
rubysmith-0.3.0 lib/rubysmith/cli/shell.rb