Class: VagrantPlugins::Solidus::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-solidus/command.rb

Direct Known Subclasses

Site::Command, SolidusBox::Command

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, env) ⇒ Command

Returns a new instance of Command.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant-solidus/command.rb', line 16

def initialize(argv, env)
  super

  @main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)

  @subcommands = Vagrant::Registry.new
  subcommands.each do |subcommand|
    @subcommands.register(subcommand.to_sym) do
      require_relative "#{command}/#{subcommand}"
      eval((self.class.to_s.split('::')[0...-1] + [subcommand.capitalize]).join('::'))
    end
  end
end

Class Method Details

.synopsisObject



4
5
6
# File 'lib/vagrant-solidus/command.rb', line 4

def self.synopsis
  'command description'
end

Instance Method Details

#commandObject



8
9
10
# File 'lib/vagrant-solidus/command.rb', line 8

def command
  'the-command'
end

#executeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-solidus/command.rb', line 30

def execute
  if @main_args.include?("-h") || @main_args.include?("--help")
    # Print the help for all the site commands.
    return help
  end

  # If we reached this far then we must have a subcommand. If not,
  # then we also just print the help and exit.
  command_class = @subcommands.get(@sub_command.to_sym) if @sub_command
  return help if !command_class || !@sub_command
  @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")

  # Initialize and execute the command class
  command_class.new(@sub_args, @env).execute
end

#helpObject

Prints the help out for this command



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vagrant-solidus/command.rb', line 47

def help
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: vagrant #{command} SUBCOMMAND [OPTIONS]"
    opts.separator ""
    opts.separator "Available subcommands:"

    # Add the available subcommands as separators in order to print them
    # out as well.
    keys = []
    @subcommands.each { |key, value| keys << key.to_s }

    keys.sort.each do |key|
      opts.separator "     #{key}"
    end

    opts.separator ""
    opts.separator "For help on any individual subcommand run `vagrant #{command} SUBCOMMAND -h`"
  end

  @env.ui.info(opts.help, :prefix => false)
end

#subcommandsObject



12
13
14
# File 'lib/vagrant-solidus/command.rb', line 12

def subcommands
  %w[the subcommands]
end