Module: CommandKit::Commands::ClassMethods

Defined in:
lib/command_kit/commands.rb

Overview

Class-level methods.

Instance Method Summary collapse

Instance Method Details

#command(name = nil, command_class, **kwargs) ⇒ Subcommand

Mounts a command as a sub-command.

Examples:

command Foo
command 'foo-bar', FooBar

Parameters:

  • name (#to_s) (defaults to: nil)

    The optional name to mount the command as. Defaults to the command’s command_name.

  • command_class (Class#main)

    The sub-command class.

  • kwargs (Hash{Symbol => Object})

    Keyword arguments.

  • kwags (Hash)

    a customizable set of options

Options Hash (**kwargs):

  • summary (String, nil)

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

Returns:

  • (Subcommand)

    The registered sub-command class.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/command_kit/commands.rb', line 144

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 |alias_name|
    command_aliases[alias_name] = name
  end

  return subcommand
end

#command_aliasesHash{String => String}

The registered command aliases.

Returns:

  • (Hash{String => String})

    The Hash of command aliases to primary command names.



105
106
107
108
109
110
111
# File 'lib/command_kit/commands.rb', line 105

def command_aliases
  @command_aliases ||= if superclass.kind_of?(ClassMethods)
                         superclass.command_aliases.dup
                       else
                         {}
                       end
end

#commandsHash{String => Subcommand}

The registered sub-commands.

Returns:

  • (Hash{String => Subcommand})

    The Hash of sub-command names and command classes.



89
90
91
92
93
94
95
# File 'lib/command_kit/commands.rb', line 89

def commands
  @commands ||= if superclass.kind_of?(ClassMethods)
                  superclass.commands.dup
                else
                  {}
                end
end

#get_command(name) ⇒ Class#main?

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.

Gets the command.

Parameters:

  • name (String)

    The command name.

Returns:

  • (Class#main, nil)

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



170
171
172
173
174
175
176
177
# File 'lib/command_kit/commands.rb', line 170

def get_command(name)
  name = name.to_s
  name = command_aliases.fetch(name,name)

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