Sha256: 312c2a74c0f19fc779ac96eb0766a41536dca309e2d750b5b0ebf8ded9aded08

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Mcoin
  module Command
    # :nodoc:
    class Base < OptionParser
      class << self
        attr_reader :options, :commands

        def description(description = nil)
          return @description if description.nil?
          @description = description
        end

        def option(mode, name, *args)
          @options ||= []
          @options.push(Option.new(mode, name, *args))
        end
      end

      attr_reader :option

      def initialize
        super

        prepare
        self.banner = '=== Mcoin : BTC Monitor Tool ==='
        self.class.options&.each { |option| option.register(self) }
      end

      def prepare
        @option = OpenStruct.new
      end

      def parse!
        super
        self
      end

      def execute
        raise NotImplementedError
      end

      # Option Register
      class Option
        def initialize(mode, name, *args)
          @mode = mode
          @name = name.to_sym
          @args = args
        end

        def register(command)
          case @mode
          when :single then register_as_single(command)
          when :multiple then register_as_multiple(command)
          end
        end

        def register_as_single(command)
          command.on(*@args, ->(value) { command.option[@name] = value })
        end

        def register_as_multiple(command)
          command.option[@name] ||= []
          command.on(*@args, ->(value) { command.option[@name].push(value) })
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mcoin-0.6.1 lib/mcoin/command/base.rb
mcoin-0.6.0 lib/mcoin/command/base.rb
mcoin-0.5.2 lib/mcoin/command/base.rb
mcoin-0.5.1 lib/mcoin/command/base.rb
mcoin-0.5.0 lib/mcoin/command/base.rb
mcoin-0.4.0 lib/mcoin/command/base.rb
mcoin-0.3.0 lib/mcoin/command/base.rb
mcoin-0.2.1 lib/mcoin/command/base.rb
mcoin-0.2.0 lib/mcoin/command/base.rb