Skip to content

Commit

Permalink
enable shortcuts for subcommands
Browse files Browse the repository at this point in the history
examples:
% bundle up  #=> bundle update
% bundle e   #=> bundle exec
  • Loading branch information
amatsuda authored and indirect committed Jun 12, 2011
1 parent 8b1af1a commit b27c2fb
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/bundler/vendor/thor.rb
Expand Up @@ -311,10 +311,33 @@ def retrieve_task_name(args) #:nodoc:
# Receives a task name (can be nil), and try to get a map from it.
# If a map can't be found use the sent name or the default task.
def normalize_task_name(meth) #:nodoc:
meth = map[meth.to_s] || meth || default_task
meth = map[meth.to_s] || find_subcommand_and_update_argv(meth) || meth || default_task
meth.to_s.gsub('-','_') # treat foo-bar > foo_bar
end

# terrible hack that overwrites ARGV
def find_subcommand_and_update_argv(subcmd_name) #:nodoc:
cmd = find_subcommand(subcmd_name)
ARGV[0] = cmd if cmd
cmd
end

def find_subcommand(subcmd_name)
possibilities = find_subcommand_possibilities subcmd_name
if possibilities.size > 1
raise "Ambiguous subcommand #{subcmd_name} matches [#{possibilities.join(', ')}]"
elsif possibilities.size < 1
return nil
end

possibilities.first
end

def find_subcommand_possibilities(subcmd_name)
len = subcmd_name.length
all_tasks.map {|t| t.first}.select { |n| subcmd_name == n[0, len] }
end

def subcommand_help(cmd)
desc "help [COMMAND]", "Describe subcommands or one specific subcommand"
class_eval <<-RUBY
Expand Down

0 comments on commit b27c2fb

Please sign in to comment.