From 98087243a7774cb5584c1d1087c79a26606d786f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Aug 2010 16:09:51 -0700 Subject: [PATCH] Support for aliasing commands --- lib/vagrant/cli.rb | 10 +++++++++- lib/vagrant/command/base.rb | 4 ++-- lib/vagrant/command/group_base.rb | 4 ++-- test/vagrant/cli_test.rb | 12 ++++++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/cli.rb b/lib/vagrant/cli.rb index 0ef75f9b7..d1765dcce 100644 --- a/lib/vagrant/cli.rb +++ b/lib/vagrant/cli.rb @@ -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 diff --git a/lib/vagrant/command/base.rb b/lib/vagrant/command/base.rb index 7b199d039..c01e5c7c4 100644 --- a/lib/vagrant/command/base.rb +++ b/lib/vagrant/command/base.rb @@ -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: diff --git a/lib/vagrant/command/group_base.rb b/lib/vagrant/command/group_base.rb index 5c355d0e0..cfaafc10f 100644 --- a/lib/vagrant/command/group_base.rb +++ b/lib/vagrant/command/group_base.rb @@ -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={}) diff --git a/test/vagrant/cli_test.rb b/test/vagrant/cli_test.rb index c7826875b..fc165258e 100644 --- a/test/vagrant/cli_test.rb +++ b/test/vagrant/cli_test.rb @@ -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