Sha256: 9702ef0016a2486d0b793f24a6123f0a394a4415a2bcd642b2c225e2195027da

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

require 'forwardable'
require 'cl/ctx'
require 'cl/parser'
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)
        # @opts, @args = parse(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

        def lookup(args)
          keys = expand(args) & Cmd.registry.keys.map(&:to_s)
          cmd = Cmd[keys.last] || abort("Unknown command: #{args.join(' ')}")
          [cmd, args - keys(cmd)]
        end

        def name
          const.registry_key
        end

        def keys(cmd)
          keys = cmd.registry_key.to_s.split(':')
          keys.concat(expand(keys)).uniq
        end

        def expand(strs)
          # strs = strs.reject { |str| str.start_with?('-') }
          strs = strs.take_while { |str| !str.start_with?('-') }
          strs.inject([]) { |strs, str| strs << [strs.last, str].compact.join(':') }
        end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cl-0.1.9 lib/cl/runner/default.rb
cl-0.1.8 lib/cl/runner/default.rb
cl-0.1.7 lib/cl/runner/default.rb
cl-0.1.6 lib/cl/runner/default.rb
cl-0.1.5 lib/cl/runner/default.rb
cl-0.1.4 lib/cl/runner/default.rb
cl-0.1.3 lib/cl/runner/default.rb
cl-0.1.2 lib/cl/runner/default.rb
cl-0.1.1 lib/cl/runner/default.rb
cl-0.1.0 lib/cl/runner/default.rb