module CommandKit::Commands::ClassMethods

Class-level methods.

Public Instance Methods

command(name=nil, command_class, **kwargs) click to toggle source

Mounts a command as a sub-command.

@param [#to_s] name

The optional name to mount the command as. Defaults to the command's
{CommandName::ClassMethods#command_name command_name}.

@param [Class#main] command_class

The sub-command class.

@param [Hash{Symbol => Object}] kwargs

Keyword arguments.

@option kwargs [String, nil] summary

A short summary for the subcommand. Defaults to the first sentence
of the command.

@option kwags [Array<String>] aliases

Optional alias names for the subcommand.

@return [Subcommand]

The registered sub-command class.

@example

command Foo

@example

command 'foo-bar', FooBar

@api public

# File lib/command_kit/commands.rb, line 137
def command(name=nil, command_class, **kwargs)
  name = if name then name.to_s
         else         command_class.command_name
         end

  subcommand = Subcommand.new(command_class,**kwargs)

  commands[name] = subcommand

  subcommand.aliases.each do |command_alias|
    command_aliases[command_alias] = name
  end

  return subcommand
end
command_aliases() click to toggle source

The registered command aliases.

@return [Hash{String => String}]

The Hash of command aliases to primary command names.

@api semipublic

# File lib/command_kit/commands.rb, line 98
def command_aliases
  @command_aliases ||= if superclass.kind_of?(ClassMethods)
                         superclass.command_aliases.dup
                       else
                         {}
                       end
end
commands() click to toggle source

The registered sub-commands.

@return [Hash{String => Subcommand}]

The Hash of sub-command names and command classes.

@api semipublic

# File lib/command_kit/commands.rb, line 82
def commands
  @commands ||= if superclass.kind_of?(ClassMethods)
                  superclass.commands.dup
                else
                  {}
                end
end
get_command(name) click to toggle source

Gets the command.

@param [String] name

The command name.

@return [Class#main, nil]

The command class or `nil` if no command could be found.

@api private

# File lib/command_kit/commands.rb, line 164
def get_command(name)
  name = name.to_s
  name = command_aliases.fetch(name,name)

  if (subcommand = commands[name])
    subcommand.command
  end
end