From 2c6028a163f4ceb0f9cda2b8f493624be49e664d Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Fri, 7 May 2010 10:03:42 -0700 Subject: [PATCH] [CLI] Enable command abbreviations. --- doc-src/content/CHANGELOG.markdown | 5 +++++ lib/compass/commands/registry.rb | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/doc-src/content/CHANGELOG.markdown b/doc-src/content/CHANGELOG.markdown index 5da006f1..052263b9 100644 --- a/doc-src/content/CHANGELOG.markdown +++ b/doc-src/content/CHANGELOG.markdown @@ -12,6 +12,11 @@ COMPASS CHANGELOG * [Compass Core] The configuration constant `$firefox2-ellipsis` has been renamed to `$use-mozilla-ellipsis-binding` to reflect the fact that it must be used for any version of mozilla less than 3.6. +* [CLI] The the new Sub-command based CLI will now recognize abbreviated + commands as long as the abbreviation is unambiguous. + For instance, `compass w` is a shortcut for `compass watch` and + `compass com` for `compass compile` but `compas co` will not work + for compile because it also matches `compass config`. 0.10.0.rc5 (May 2, 2010) ------------------------ diff --git a/lib/compass/commands/registry.rb b/lib/compass/commands/registry.rb index ac3654d9..767cc652 100644 --- a/lib/compass/commands/registry.rb +++ b/lib/compass/commands/registry.rb @@ -6,11 +6,24 @@ module Compass::Commands end def get(name) @commands ||= Hash.new - @commands[name.to_sym] + @commands[name.to_sym] || @commands[abbreviation_of(name)] + end + def abbreviation_of(name) + re = /^#{Regexp.escape(name)}/ + matching = @commands.keys.select{|k| k.to_s =~ re} + if matching.size == 1 + matching.first + else + raise Compass::Error, "Ambiguous abbreviation '#{name}'. Did you mean one of: #{matching.join(", ")}" + end + end + def abbreviation?(name) + re = /^#{Regexp.escape(name)}/ + @commands.keys.detect{|k| k.to_s =~ re} end def command_exists?(name) @commands ||= Hash.new - name && @commands.has_key?(name.to_sym) + name && (@commands.has_key?(name.to_sym) || abbreviation?(name)) end def all @commands.keys