Support for aliasing commands
This commit is contained in:
parent
83b2872ddb
commit
98087243a7
|
@ -7,7 +7,9 @@ module Vagrant
|
|||
class CLI < Thor
|
||||
# Registers the given class with the CLI so it can be accessed.
|
||||
# The class must be a subclass of either {Command} or {GroupCommand}.
|
||||
def self.register(klass, name, usage, description)
|
||||
def self.register(klass, name, usage, description, opts=nil)
|
||||
opts ||= {}
|
||||
|
||||
if klass <= Command::GroupBase
|
||||
# A subclass of GroupBase is a subcommand, since it contains
|
||||
# many smaller commands within it.
|
||||
|
@ -19,6 +21,12 @@ module Vagrant
|
|||
desc usage, description
|
||||
define_method(name) { |*args| invoke klass, args }
|
||||
end
|
||||
|
||||
if opts[:alias]
|
||||
# Alises are defined for this command, so properly alias the
|
||||
# newly defined method/subcommand:
|
||||
map opts[:alias] => name
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,8 +29,8 @@ module Vagrant
|
|||
#
|
||||
# The description added to the class via the `desc` method will be
|
||||
# used as a description for the command.
|
||||
def self.register(usage)
|
||||
CLI.register(self, extract_name_from_usage(usage), usage, desc)
|
||||
def self.register(usage, opts=nil)
|
||||
CLI.register(self, extract_name_from_usage(usage), usage, desc, opts)
|
||||
end
|
||||
|
||||
# Extracts the name of the command from a usage string. Example:
|
||||
|
|
|
@ -21,8 +21,8 @@ module Vagrant
|
|||
#
|
||||
# Additionally, unlike {Base}, a description must be specified to
|
||||
# this register command, since there is no class-wide description.
|
||||
def self.register(usage, description)
|
||||
CLI.register(self, Base.extract_name_from_usage(usage), usage, description)
|
||||
def self.register(usage, description, opts=nil)
|
||||
CLI.register(self, Base.extract_name_from_usage(usage), usage, description, opts)
|
||||
end
|
||||
|
||||
def initialize(args=[], options={}, config={})
|
||||
|
|
|
@ -19,5 +19,17 @@ class CLITest < Test::Unit::TestCase
|
|||
@klass.register(base, name, name, "A description")
|
||||
assert @klass.subcommands.include?(name)
|
||||
end
|
||||
|
||||
should "alias methods if the alias option is given" do
|
||||
base = Class.new(Vagrant::Command::Base) do
|
||||
def execute
|
||||
raise "WORKED"
|
||||
end
|
||||
end
|
||||
|
||||
name = "__test_registering_with_alias"
|
||||
@klass.register(base, name, name, "A description", :alias => "--ALIAS")
|
||||
assert_raises(RuntimeError) { @klass.start(["--ALIAS"], :env => mock_environment) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue