Class: TechnoGate::TgCli::Main

Inherits:
Thor
  • Object
show all
Defined in:
lib/tg_cli/main.rb

Overview

Entrypoint for the Vagrant CLI. This class should never be initialized directly (like a typical Thor class). Instead, use Environment#cli to invoke the CLI.

# Defining Custom CLI Commands

If you’re looking to define custom CLI commands, then look at one of the two following classes:

  • Command::Base - Implementing a single command such as ‘vagrant up`, e.g. one without subcommands. Also take a look at Command::NamedBase.

  • Command::GroupBase - Implementing a command with subcommands, such as ‘vagrant box`, which has the `list`, `add`, etc. subcommands.

The above linked classes contain the main documentation for each type of command.

Class Method Summary collapse

Class Method Details

.register(klass, name, usage, description, opts = nil) ⇒ Object

Registers the given class with the CLI so it can be accessed. The class must be a subclass of either Command::Base or Command::GroupBase. Don’t call this method directly, instead call the Command::Base.register or Command::GroupBase.register methods.

Parameters:

  • klass (Class)

    Command class

  • name (String)

    Command name, accessed at ‘vagrant NAME`

  • usage (String)

    Command usage, such as “vagrant NAME [–option]”

  • description (String)

    Description of the command shown during the command listing.

  • opts (Hash) (defaults to: nil)

    Other options (not gone into detail here, look at the source instead).



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tg_cli/main.rb', line 36

def self.register(klass, name, usage, description, opts=nil)
  opts ||= {}

  if klass <= GroupBase
    # A subclass of GroupBase is a subcommand, since it contains
    # many smaller commands within it.
    desc usage, description, opts
    subcommand name, klass
  elsif klass <= Base
    # A subclass of Base is a single command, since it
    # is invoked as a whole (as Thor::Group)
    desc usage, description, opts
    define_method(name) { |*args| invoke klass, args }
  end

  if opts[:alias]
    # Alises are defined for this command, so properly alias the
    # newly defined method/subcommand:
    map opts[:alias] => name
  end

  if opts[:default]
    # This command is to be set as the default task.
    default_task name
  end
end