abstract class Marten::CLI::Manage::Command::Base

Overview

Management abstract command.

This class should be subclassed in order to implement per-app management commands. Subclasses will be automatically registered to the management commands registry, and they will be made available through the manage CLI.

Included Modules

Direct Known Subclasses

Defined in:

marten/cli/manage/command/base.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(options : Array(String), stdin : IO = STDIN, stdout : IO = STDOUT, stderr : IO = STDERR, main_command_name : String = Marten::CLI::DEFAULT_COMMAND_NAME) #

[View source]

Class Method Detail

def self.command_aliases #

Returns the aliases of the command.


[View source]
def self.command_aliases(*aliases : String | Symbol) #

Allows to configure aliases for the command.


[View source]
def self.command_name(name : String | Symbol) #

Allows to set the name of the command.

The value set using this method will be used by users when they invoke the command through the use of the manage CLI.


[View source]
def self.command_name #

Returns the name of the considered command.


[View source]
def self.help(help : String) #

Allows to set the help description of the command.


[View source]
def self.help #

Returns the help description of the command.


[View source]

Instance Method Detail

def handle #

Setups the command and runs it.

This method will call the #setup method, configure the arguments / options parser and then execute the command through the use of the #run method.


[View source]
def on_argument(name : String | Symbol, description : String, &block : String -> ) #

Allows to configure a specific command argument.

This method will configure a command argument. It expects a name, a description, and it yields a block to let the command properly assign the argument value to the command object:

class MyCommand < Marten::CLI::Command
  def setup
    on_argument(:arg, "The name of the argument") do |value|
      @arg_var = value
    end
  end
end

[View source]
def on_option(short_flag : String | Symbol, long_flag : String | Symbol, description : String, &block : String -> ) #

Allows to configure a specific command option.

This method will configure a command option (eg. --option). It expects a flag name, a short flag name, a description, and it yields a block to let the command properly assign the option value to the command object:

class MyCommand < Marten::CLI::Command
  def setup
    on_option("o", "option", "The name of the option") do
      @option_var = true
    end
  end
end

Note that the -- must not be included in the option name.


[View source]
def on_option(flag : String | Symbol, description : String, &block : String -> ) #

Allows to configure a specific command option.

This method will configure a command option (eg. --option). It expects a flag name, a description, and it yields a block to let the command properly assign the option value to the command object:

class MyCommand < Marten::CLI::Command
  def setup
    on_option(:option, "The name of the option") do
      @option_var = true
    end
  end
end

Note that the -- must not be included in the option name.


[View source]
def on_option_with_arg(short_flag : String | Symbol, long_flag : String | Symbol, arg : String | Symbol, description : String, &block : String -> ) #

Allows to configure a specific command option with an associated argument.

This method will configure a command option (eg. --option) and an associated argument. It expects a flag name, a short flag name, an argument name, a description, and it yields a block to let the command properly assign the option value to the command object:

class MyCommand < Marten::CLI::Command
  def setup
    on_option_with_arg("o", "option", "arg", "The name of the option") do |arg|
      @arg = arg
    end
  end
end

Note that the -- must not be included in the option name.


[View source]
def on_option_with_arg(flag : String | Symbol, arg : String | Symbol, description : String, &block : String -> ) #

Allows to configure a specific command option with an associated argument.

This method will configure a command option (eg. --option) and an associated argument. It expects a flag name, an argument name, a description, and it yields a block to let the command properly assign the option value to the command object:

class MyCommand < Marten::CLI::Command
  def setup
    on_option_with_arg(:option, :arg, "The name of the option") do |arg|
      @arg = arg
    end
  end
end

Note that the -- must not be included in the option name.


[View source]
def print(msg, ending = "\n") #

Allows to print a message to the output file descriptor.

This method will print a textual value to the output file descriptor, and it allows to optionally specify the ending character (which defaults to a newline):

print("This is a message")
print("This is a message without newline", ending = "")

[View source]
def print_error(msg) #

Allows to print a message to the error file descriptor.


[View source]
def print_error_and_exit(msg, exit_code = 1) #

Allows to print a message to the error file descriptor and to exit the execution of the command.

The code used to exit the execution of the command can be specified using the exit_code argument (defaults to 1).


[View source]
def run #

Runs the command.

This method should be overridden by subclasses in order to implement the execution logic of the considered command.


[View source]
def setup #

Setups the command.

This method should be overridden by subclasses in order to configure the command arguments and options through the use of the #on_argument and #on_option methods.


[View source]
def stderr : IO #

Returns the IO object that should be used by the command as the main error file descriptor.


[View source]
def stdin : IO #

Returns the IO object that should be used by the command as the main input file descriptor.


[View source]
def stdout : IO #

Returns the IO object that should be used by the command as the main output file descriptor.


[View source]
def style(msg, fore = nil, back = nil, mode = nil) #

Allows to apply a style to a specific text value.

This method can be used to apply fore, back, and mode styles to a specific text values. This method is likely to be used in conjunction with the #print method when outputting messages:

print(style("This is a text", fore: :light_blue, back: :green, mode: :bold))

[View source]