Class: Veewee::Command::GroupBase

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions, Helpers
Defined in:
lib/veewee/command/group_base.rb

Overview

A GroupBase is the superclass which should be used if you’re creating a CLI command which has subcommands such as ‘veewee box`, which has subcommands such as `add`, `remove`, `list`. If you’re creating a simple command which has no subcommands, such as ‘veewee up`, then use Base instead.

Unlike Base, where all public methods are executed, in a GroupBase, each public method defines a separate task which can be invoked. The best way to get examples of how to create a GroupBase command is to look at the built-in commands, such as BoxCommand.

# Defining a New Command

To define a new command with subcommands, create a new class which inherits from this class, then call GroupBase.register to register the command. That’s it! When the command is invoked, the method matching the subcommand is invoked. An example is shown below:

class SayCommand < Veewee::Command::GroupBase
  register "say", "Say hello or goodbye"

  desc "hello", "say hello"
  def hello
    env.ui.info "Hello"
  end

  desc "goodbye", "say goodbye"
  def goodbye
    env.ui.info "Goodbye"
  end
end

In this case, the above class is invokable via ‘veewee say hello` or `veewee say goodbye`. To give it a try yourself, just copy and paste the above into a Veeweefile somewhere, and run `veewee` from within that directory. You should see the new command!

Also notice that in the above, each task follows a ‘desc` call. This call is used to provide usage and description for each task, and is required.

## Defining Command-line Options

### Arguments

To define arguments to your commands, such as ‘veewee say hello mitchell`, then you simply define them as arguments to the method implementing the task. An example is shown below (only the method, to keep things brief):

def hello(name)
  env.ui.info "Hello, #{name}"
end

Then, if ‘veewee say hello mitchell` was called, then the output would be “Hello, mitchell”

### Switches or Other Options

TODO

Direct Known Subclasses

Fusion, Kvm, Parallels, Vbox

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#initialize_environment

Constructor Details

#initialize(*args) ⇒ GroupBase

Returns a new instance of GroupBase.



94
95
96
97
# File 'lib/veewee/command/group_base.rb', line 94

def initialize(*args)
  super
  initialize_environment(*args)
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



69
70
71
# File 'lib/veewee/command/group_base.rb', line 69

def env
  @env
end

Class Method Details

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

Register the command with the main Veewee CLI under the given usage. The usage will be used for accessing it from the CLI, so if you give it a usage of ‘lamp [subcommand]`, then the command to invoke this will be `veewee lamp` (with a subcommand).

The description is used when a listing of the commands is given and is meant to be a brief (one sentence) description of what this command does.

Some additional options may be passed in as the last parameter:

  • ‘:alias` - If given as an array or string, these will be aliases

for the same command. For example, `veewee version` is also
`veewee --version` and `veewee -v`

Parameters:

  • usage (String)
  • description (String)
  • opts (Hash) (defaults to: nil)


89
90
91
92
# File 'lib/veewee/command/group_base.rb', line 89

def self.register(usage, description, opts=nil)
  @_name = Base.extract_name_from_usage(usage)
  CLI.register(self, @_name, usage, description, opts)
end