diff --git a/lib/vagrant/easy.rb b/lib/vagrant/easy.rb index 2e3a4caf8..c9410dd24 100644 --- a/lib/vagrant/easy.rb +++ b/lib/vagrant/easy.rb @@ -1,6 +1,7 @@ module Vagrant module Easy autoload :CommandBase, "vagrant/easy/command_base" + autoload :CommandAPI, "vagrant/easy/command_api" autoload :Operations, "vagrant/easy/operations" # This creates a new easy command. This typically is not called diff --git a/lib/vagrant/easy/command_api.rb b/lib/vagrant/easy/command_api.rb new file mode 100644 index 000000000..38b2cd571 --- /dev/null +++ b/lib/vagrant/easy/command_api.rb @@ -0,0 +1,42 @@ +require "delegate" + +require "vagrant/easy/operations" + +module Vagrant + module Easy + # 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. + class CommandAPI < DelegateClass(Operations) + attr_reader :argv + + def initialize(vm, argv) + super(Operations.new(vm)) + + @argv = argv + @vm = vm + end + + # Outputs an error message to the UI. + # + # @param [String] message Message to send. + def error(message) + @vm.ui.error(message) + end + + # Outputs a normal message to the UI. Use this for any standard-level + # messages. + # + # @param [String] message Message to send. + def info(message) + @vm.ui.info(message) + end + + # Outputs a success message to the UI. + # + # @param [String] message Message to send. + def success(message) + @vm.ui.success(message) + end + end + end +end diff --git a/lib/vagrant/easy/command_base.rb b/lib/vagrant/easy/command_base.rb index ab558afda..85a19ba22 100644 --- a/lib/vagrant/easy/command_base.rb +++ b/lib/vagrant/easy/command_base.rb @@ -44,14 +44,26 @@ module Vagrant end # Parse the options - argv = parse_options(opts) - return if !argv + argv = nil + begin + argv = parse_options(opts) + rescue Errors::CLIInvalidOptions + # This means that an invalid flag such as "--foo" was passed. + # We usually show the help at this point (in built-in commands), + # but since we don't know what our implementation does, we just + # pass the flags through now. + argv = @argv.dup + end + + # If argv is nil then `parse_options` halted execution and we + # halt our own execution here. + return 0 if !argv # Run the action for each VM. @logger.info("Running easy command: #{@command}") - with_target_vms(argv) do |vm| + with_target_vms do |vm| @logger.debug("Running easy command for VM: #{vm.name}") - @runner.call(Operations.new(vm)) + @runner.call(CommandAPI.new(vm, argv)) end # Exit status 0 every time for now