Class: Vagrant::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/cli.rb

Overview

Manages the command line interface to Vagrant.

Instance Method Summary collapse

Constructor Details

#initialize(argv, env) ⇒ CLI

Returns a new instance of CLI.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vagrant/cli.rb', line 10

def initialize(argv, env)
  super

  @logger = Log4r::Logger.new("vagrant::cli")
  @main_args, @sub_command, @sub_args = split_main_and_subcommand(argv)

  if Vagrant::Util::Experimental.feature_enabled?("typed_triggers")
    ui = Vagrant::UI::Prefixed.new(env.ui, "vagrant")
    @triggers = Vagrant::Plugin::V2::Trigger.new(env, env.vagrantfile.config.trigger, nil, ui)
  end

  Util::CheckpointClient.instance.setup(env).check
  @logger.info("CLI: #{@main_args.inspect} #{@sub_command.inspect} #{@sub_args.inspect}")
end

Instance Method Details

#executeObject


25
26
27
28
29
30
31
32
33
34
35
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vagrant/cli.rb', line 25

def execute
  if @main_args.include?("-h") || @main_args.include?("--help")
    # Help is next in short-circuiting everything. Print
    # the help and exit.
    help
    return 0
  end

  if @sub_command == "login"
    $stderr.puts "WARNING: This command has been deprecated and aliased to `vagrant cloud auth login`"
  end

  # If we reached this far then we must have a subcommand. If not,
  # then we also just print the help and exit.
  command_plugin = nil
  if @sub_command
    command_plugin = Vagrant.plugin("2").manager.commands[@sub_command.to_sym]

    if !command_plugin
      alias_command = Alias.new(@env).commands[@sub_command.to_sym]

      if alias_command
        return alias_command.call(@sub_args)
      end
    end
  end

  if !command_plugin || !@sub_command
    help
    return 1
  end

  command_class = command_plugin[0].call
  @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")

  Util::CheckpointClient.instance.display

  # Initialize and execute the command class, returning the exit status.
  result = 0
  begin
    @triggers.fire(@sub_command, :before, nil, :command) if
      Vagrant::Util::Experimental.feature_enabled?("typed_triggers")
    result = command_class.new(@sub_args, @env).execute
    @triggers.fire(@sub_command, :after, nil, :command) if
      Vagrant::Util::Experimental.feature_enabled?("typed_triggers")
  rescue Interrupt
    @env.ui.info(I18n.t("vagrant.cli_interrupt"))
    result = 1
  end

  result = 0 if !result.is_a?(Integer)
  return result
end

#helpObject

This prints out the help for the CLI.


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vagrant/cli.rb', line 80

def help
  # We use the optionparser for this. Its just easier. We don't use
  # an optionparser above because I don't think the performance hits
  # of creating a whole object are worth checking only a couple flags.
  opts = OptionParser.new do |o|
    o.banner = "Usage: vagrant [options] <command> [<args>]"
    o.separator ""
    o.on("-v", "--version", "Print the version and exit.")
    o.on("-h", "--help", "Print this help.")
    o.separator ""
    o.separator "Common commands:"

    # Add the available subcommands as separators in order to print them
    # out as well.
    commands = {}
    longest = 0
    Vagrant.plugin("2").manager.commands.each do |key, data|
      # Skip non-primary commands. These only show up in extended
      # help output.
      next if !data[1][:primary]

      key           = key.to_s
      klass         = data[0].call
      commands[key] = klass.synopsis
      longest       = key.length if key.length > longest
    end

    commands.keys.sort.each do |key|
      o.separator "     #{key.ljust(longest+2)} #{commands[key]}"
      @env.ui.machine("cli-command", key.dup)
    end

    o.separator ""
    o.separator "For help on any individual command run `vagrant COMMAND -h`"
    o.separator ""
    o.separator "Additional subcommands are available, but are either more advanced"
    o.separator "or not commonly used. To see all subcommands, run the command"
    o.separator "`vagrant list-commands`."
  end

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