Extract option building to standalone method. Update help to use method.
This commit is contained in:
parent
11df6c7ae1
commit
dc64d85adf
@ -7,17 +7,9 @@ module VagrantPlugins
|
||||
end
|
||||
|
||||
def help(*args)
|
||||
plugin_name = args.last.metadata["plugin_name"]
|
||||
plugin = Vagrant::Plugin::V2::Plugin.manager.commands[plugin_name.to_sym].to_a.first
|
||||
if !plugin
|
||||
raise "Failed to locate command plugin for: #{plugin_name}"
|
||||
end
|
||||
$stashed_opts = nil
|
||||
klass = Class.new(plugin.call)
|
||||
klass.class_eval { def parse_options(opts); $stashed_opts = opts; nil; end };
|
||||
klass.new(['-h'], {}).execute
|
||||
options = command_options_for(args.last.metadata["plugin_name"])
|
||||
Hashicorp::Vagrant::Sdk::Command::HelpResp.new(
|
||||
help: $stashed_opts.help()
|
||||
help: options.help
|
||||
)
|
||||
end
|
||||
|
||||
@ -42,31 +34,10 @@ module VagrantPlugins
|
||||
end
|
||||
|
||||
def flags(*args)
|
||||
plugin_name = args.last.metadata["plugin_name"]
|
||||
plugin = Vagrant::Plugin::V2::Plugin.manager.commands[plugin_name.to_sym].to_a.first
|
||||
if !plugin
|
||||
raise "Failed to locate command plugin for: #{plugin_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
|
||||
|
||||
# Execute the command to populate our options
|
||||
klass.new([], {}).execute
|
||||
options = command_options_for(args.last.metadata["plugin_name"])
|
||||
|
||||
# Now we can build our list of flags
|
||||
flags = Thread.current.thread_variable_get(:command_options).top.list.find_all { |o|
|
||||
flags = options.top.list.find_all { |o|
|
||||
o.is_a?(OptionParser::Switch)
|
||||
}.map { |o|
|
||||
Hashicorp::Vagrant::Sdk::Command::Flag.new(
|
||||
@ -86,6 +57,38 @@ module VagrantPlugins
|
||||
flags: flags
|
||||
)
|
||||
end
|
||||
|
||||
def command_options_for(name)
|
||||
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
|
||||
|
||||
# Execute the command to populate our options
|
||||
klass.new([], {}).execute
|
||||
|
||||
options = Thread.current.thread_variable_get(:command_options)
|
||||
|
||||
# Clean our option data out of the thread
|
||||
Thread.current.thread_variable_set(:command_options, nil)
|
||||
|
||||
# Send the options back
|
||||
options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user