Class: Vagrant::Easy::CommandAPI

Inherits:
Operations show all
Defined in:
lib/vagrant/easy/command_api.rb

Overview

This is the API that easy commands have access to. It is a subclass of Operations so it has access to all those methods as well.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Operations

#download, #local, #run, #sudo, #upload

Constructor Details

- (CommandAPI) initialize(vm, argv)

Returns a new instance of CommandAPI



15
16
17
18
19
20
21
# File 'lib/vagrant/easy/command_api.rb', line 15

def initialize(vm, argv)
  super(Operations.new(vm))

  @logger = Log4r::Logger.new("vagrant::easy::command_api")
  @argv   = argv
  @vm     = vm
end

Instance Attribute Details

- (Object) argv (readonly)

Returns the value of attribute argv



13
14
15
# File 'lib/vagrant/easy/command_api.rb', line 13

def argv
  @argv
end

Instance Method Details

- (String) arg(*names)

Gets the value of an argument from the command line. Many arguments can be given as a parameter and the first matching one will be returned.

Returns:

  • (String)


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
# File 'lib/vagrant/easy/command_api.rb', line 27

def arg(*names)
  @logger.info("reading args: #{names.inspect}")

  # Mangle the names a bit to add "=VALUE" to every flag.
  names = names.map do |name|
    "#{name}=VALUE"
  end

  # Create a basic option parser
  parser = OptionParser.new

  # Add on a matcher for this thing
  result = nil
  parser.on(*names) do |value|
    result = value
  end

  begin
    # The `dup` is required in @argv because the OptionParser
    # modifies it in place as it finds matching arguments.
    parser.parse!(@argv.dup)
  rescue OptionParser::MissingArgument
    # Missing argument means the argument existed but had no data,
    # so we mark it as an empty string
    result = ""
  rescue OptionParser::InvalidOption
    # Ignore!
  end

  # Return the results
  result
end

- (String) arg_extra

Returns any extra arguments that are past a "--" on the command line.

Returns:

  • (String)


63
64
65
66
67
68
69
70
# File 'lib/vagrant/easy/command_api.rb', line 63

def arg_extra
  # Split the arguments and remove the "--"
  remaining = @argv.drop_while { |v| v != "--" }
  remaining.shift

  # Return the remaining arguments
  remaining.join(" ")
end

- (Object) error(message)

Outputs an error message to the UI.

Parameters:

  • message (String)

    Message to send.



75
76
77
# File 'lib/vagrant/easy/command_api.rb', line 75

def error(message)
  @vm.ui.error(message)
end

- (Object) info(message)

Outputs a normal message to the UI. Use this for any standard-level messages.

Parameters:

  • message (String)

    Message to send.



83
84
85
# File 'lib/vagrant/easy/command_api.rb', line 83

def info(message)
  @vm.ui.info(message)
end

- (Object) success(message)

Outputs a success message to the UI.

Parameters:

  • message (String)

    Message to send.



90
91
92
# File 'lib/vagrant/easy/command_api.rb', line 90

def success(message)
  @vm.ui.success(message)
end