Sha256: 03804cc409fb0d0db2cfbd0d22ccf8910655a189902752d4c2d1e6ca002bb5d5

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require 'forwardable'
require 'cl/ctx'
require 'cl/helper'

class Cl
  module Runner
    class Default
      extend Forwardable
      include Merge

      def_delegators :ctx, :abort

      attr_reader :ctx, :const, :args, :opts

      def initialize(ctx, args)
        @ctx = ctx
        @const, @args = lookup(args)
      end

      def run
        cmd.help? ? help.run : cmd.run
      end

      def cmd
        @cmd ||= const.new(ctx, args)
      end

      def help
        Help.new(ctx, [cmd.registry_key])
      end

      private

        # stopping at any arg that starts with a dash, find the command
        # with the key matching the most args when joined with ":", and
        # remove these used args from the array
        def lookup(args)
          keys = args.take_while { |key| !key.start_with?('-') }

          keys = keys.inject([[], []]) do |keys, key|
            keys[1] << key
            keys[0] << [Cmd[keys[1].join(':')], keys[1].dup] if Cmd.registered?(keys[1].join(':'))
            keys
          end

          cmd, keys = keys[0].last
          keys.each { |key| args.delete_at(args.index(key)) }
          [cmd, args]
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cl-0.1.12 lib/cl/runner/default.rb