diff --git a/Gemfile b/Gemfile index 565ca7959..cca3f25c8 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,5 @@ if File.exist?(File.expand_path("../../vagrant-spec", __FILE__)) else gem 'vagrant-spec', git: "https://github.com/hashicorp/vagrant-spec.git", branch: :main end + +gem 'pry-byebug' diff --git a/get_help.rb b/get_help.rb new file mode 100644 index 000000000..d6c57a476 --- /dev/null +++ b/get_help.rb @@ -0,0 +1,63 @@ +require "vagrant" + +def command_help_for(name, subcommand) + plugin = Vagrant::Plugin::V2::Plugin.manager.commands[name.to_sym].to_a.first + if !plugin + raise "Failed to locate command plugin for: #{name}" + end + # Create a new anonymous class based on the command class + # so we can modify the setup behavior + klass = Class.new(plugin.call) + + # Update the option parsing to store the provided options, and then return + # a nil value. The nil return will force the command to call help and not + # actually execute anything. + klass.class_eval do + def parse_options(opts) + Thread.current.thread_variable_set(:command_options, opts) + nil + end + end + + Vagrant::UI::Silent.class_eval do + def info(message, *opts) + Thread.current.thread_variable_set(:command_info, message) + nil + end + end + env = Vagrant::Environment.new(ui: Vagrant::UI::Silent.new) + # Execute the command to populate our options + cmd_plg = klass.new(subcommand, env) + + begin + cmd_plg.execute + rescue Vagrant::Errors::CLIInvalidUsage + # This is expected since no args are being provided + end + + options = Thread.current.thread_variable_get(:command_options) + msg = Thread.current.thread_variable_get(:command_info) + + # require "pry-byebug"; binding.pry + # Clean our option data out of the thread + Thread.current.thread_variable_set(:command_options, nil) + Thread.current.thread_variable_set(:command_info, nil) + + if !options.nil? + return options + elsif !msg.nil? + return msg + end +end + +puts "getting help for status" +hlp = command_help_for("status", []) +puts hlp + +puts "\ngetting help for box" +hlp = command_help_for("box", []) +puts hlp + +# puts "\ngetting help for box add" +# hlp = command_help_for("box", ["add"]) +# puts hlp diff --git a/plugins/commands/autocomplete/command/root.rb b/plugins/commands/autocomplete/command/root.rb index 3ffa66642..fbbb10060 100644 --- a/plugins/commands/autocomplete/command/root.rb +++ b/plugins/commands/autocomplete/command/root.rb @@ -57,7 +57,6 @@ module VagrantPlugins end @env.ui.info(opts.help, prefix: false) - opts.help end end end diff --git a/plugins/commands/box/command/root.rb b/plugins/commands/box/command/root.rb index eb5e5421d..79930ffb9 100644 --- a/plugins/commands/box/command/root.rb +++ b/plugins/commands/box/command/root.rb @@ -87,7 +87,6 @@ module VagrantPlugins end @env.ui.info(opts.help, prefix: false) - opts.help end end end diff --git a/plugins/commands/cloud/auth/root.rb b/plugins/commands/cloud/auth/root.rb index 336d1b0b7..423fe3eea 100644 --- a/plugins/commands/cloud/auth/root.rb +++ b/plugins/commands/cloud/auth/root.rb @@ -65,7 +65,6 @@ module VagrantPlugins end @env.ui.info(opts.help, prefix: false) - opts.help end end end diff --git a/plugins/commands/cloud/box/root.rb b/plugins/commands/cloud/box/root.rb index 22b87e510..e687e5016 100644 --- a/plugins/commands/cloud/box/root.rb +++ b/plugins/commands/cloud/box/root.rb @@ -69,7 +69,6 @@ module VagrantPlugins end @env.ui.info(opts.help, prefix: false) - opts.help end end end diff --git a/plugins/commands/cloud/provider/root.rb b/plugins/commands/cloud/provider/root.rb index 17077f055..3f09d4266 100644 --- a/plugins/commands/cloud/provider/root.rb +++ b/plugins/commands/cloud/provider/root.rb @@ -69,7 +69,6 @@ module VagrantPlugins end @env.ui.info(opts.help, prefix: false) - opts.help end end end diff --git a/plugins/commands/cloud/root.rb b/plugins/commands/cloud/root.rb index 9dfb1e72d..08007934b 100644 --- a/plugins/commands/cloud/root.rb +++ b/plugins/commands/cloud/root.rb @@ -97,7 +97,6 @@ module VagrantPlugins end @env.ui.info(opts.help, prefix: false) - opts.help end end end diff --git a/plugins/commands/cloud/version/root.rb b/plugins/commands/cloud/version/root.rb index 0132490b5..d58d483dd 100644 --- a/plugins/commands/cloud/version/root.rb +++ b/plugins/commands/cloud/version/root.rb @@ -73,7 +73,6 @@ module VagrantPlugins end @env.ui.info(opts.help, prefix: false) - opts.help end end end diff --git a/plugins/commands/plugin/command/root.rb b/plugins/commands/plugin/command/root.rb index 14e8e8220..6953eb495 100644 --- a/plugins/commands/plugin/command/root.rb +++ b/plugins/commands/plugin/command/root.rb @@ -87,7 +87,6 @@ module VagrantPlugins end @env.ui.info(opts.help, prefix: false) - opts.help end end end