Module: Dry::CLI::Registry

Defined in:
lib/dry/cli/registry.rb

Overview

Registry mixin

Since:

  • 0.1.0

Defined Under Namespace

Classes: Prefix

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



13
14
15
16
17
18
# File 'lib/dry/cli/registry.rb', line 13

def self.extended(base)
  base.class_eval do
    @_mutex = Mutex.new
    @commands = CommandRegistry.new
  end
end

Instance Method Details

#after(command_name, callback = nil, &blk) ⇒ Object

Register an after callback.

Examples:

require "dry/cli"

module Foo
  module Commands
    extend Dry::CLI::Registry

    class Hello < Dry::CLI::Command
      def call(*)
        puts "hello"
      end
    end

    register "hello", Hello
    after "hello", -> { puts "world" }
  end
end

Register an object as callback

require "dry/cli"

module Callbacks
  class World
    def call(*)
      puts "world"
    end
  end
end

module Foo
  module Commands
    extend Dry::CLI::Registry

    class Hello < Dry::CLI::Command
      def call(*)
        puts "hello"
      end
    end

    register "hello", Hello
    after "hello", Callbacks::World.new
  end
end

Register a class as callback

require "dry/cli"

module Callbacks
  class World
    def call(*)
      puts "world"
    end
  end
end

module Foo
  module Commands
    extend Dry::CLI::Registry

    class Hello < Dry::CLI::Command
      def call(*)
        puts "hello"
      end
    end

    register "hello", Hello
    after "hello", Callbacks::World
  end
end

Parameters:

  • command_name (String)

    the name used for command registration

  • callback (Class, #call) (defaults to: nil)

    the callback object. If a class is given, it MUST respond to ‘#call`.

  • blk (Proc)

    the callback espressed as a block

Raises:

Since:

  • 0.2.0



261
262
263
264
265
# File 'lib/dry/cli/registry.rb', line 261

def after(command_name, callback = nil, &blk)
  @_mutex.synchronize do
    command(command_name).after_callbacks.append(&_callback(callback, blk))
  end
end

#before(command_name, callback = nil, &blk) ⇒ Object

Register a before callback.

Examples:

require "dry/cli"

module Foo
  module Commands
    extend Dry::CLI::Registry

    class Hello < Dry::CLI::Command
      def call(*)
        puts "hello"
      end
    end

    register "hello", Hello
    before "hello", -> { puts "I'm about to say.." }
  end
end

Register an object as callback

require "dry/cli"

module Callbacks
  class Hello
    def call(*)
      puts "world"
    end
  end
end

module Foo
  module Commands
    extend Dry::CLI::Registry

    class Hello < Dry::CLI::Command
      def call(*)
        puts "I'm about to say.."
      end
    end

    register "hello", Hello
    before "hello", Callbacks::Hello.new
  end
end

Register a class as callback

require "dry/cli"

module Callbacks
  class Hello
    def call(*)
      puts "world"
    end
  end
end

module Foo
  module Commands
    extend Dry::CLI::Registry

    class Hello < Dry::CLI::Command
      def call(*)
        puts "I'm about to say.."
      end
    end

    register "hello", Hello
    before "hello", Callbacks::Hello
  end
end

Parameters:

  • command_name (String)

    the name used for command registration

  • callback (Class, #call) (defaults to: nil)

    the callback object. If a class is given, it MUST respond to ‘#call`.

  • blk (Proc)

    the callback espressed as a block

Raises:

Since:

  • 0.2.0



173
174
175
176
177
# File 'lib/dry/cli/registry.rb', line 173

def before(command_name, callback = nil, &blk)
  @_mutex.synchronize do
    command(command_name).before_callbacks.append(&_callback(callback, blk))
  end
end

#get(arguments) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



269
270
271
# File 'lib/dry/cli/registry.rb', line 269

def get(arguments)
  @commands.get(arguments)
end

#register(name, command = nil, aliases: [], hidden: false, &block) ⇒ Object

Register a command

Examples:

Register a command

require "dry/cli"

module Foo
  module Commands
    extend Dry::CLI::Registry

    class Hello < Dry::CLI::Command
    end

    register "hi", Hello
  end
end

Register a command with aliases

require "dry/cli"

module Foo
  module Commands
    extend Dry::CLI::Registry

    class Hello < Dry::CLI::Command
    end

    register "hello", Hello, aliases: ["hi", "ciao"]
  end
end

Register a group of commands

require "dry/cli"

module Foo
  module Commands
    extend Dry::CLI::Registry

    module Generate
      class App < Dry::CLI::Command
      end

      class Action < Dry::CLI::Command
      end
    end

    register "generate", aliases: ["g"] do |prefix|
      prefix.register "app",    Generate::App
      prefix.register "action", Generate::Action
    end
  end
end

Parameters:

  • name (String)

    the command name

  • command (NilClass, Dry::CLI::Command) (defaults to: nil)

    the optional command

  • aliases (Array<String>) (defaults to: [])

    an optional list of aliases

  • options (Hash)

    a set of options

Since:

  • 0.1.0



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dry/cli/registry.rb', line 78

def register(name, command = nil, aliases: [], hidden: false, &block)
  @commands.set(name, command, aliases, hidden)

  if block_given?
    prefix = Prefix.new(@commands, name, aliases, hidden)
    if block.arity.zero?
      prefix.instance_eval(&block)
    else
      yield(prefix)
    end
  end
end