rubythor

Command Aliasing in Thor


Is it possible to create aliases for commands in Thor?

Much like command aliasing in Commander. https://github.com/tj/commander#command-aliasing

I am able to find aliases for options, but not for the command itself.

Using the example from Thor,

#!/usr/bin/env ruby
require 'thor'

# cli.rb
class MyCLI < Thor
  desc "hello NAME", "say hello to NAME"
  def hello(name)
    puts "Hello #{name}"
  end
end

MyCLI.start(ARGV)

I should be able to run

$ ./cli.rb hello John
Hello John

I would like to alias the command "hello" to "hi" as well.


Solution

  • You can use map for this:

    http://www.rubydoc.info/github/wycats/thor/master/Thor#map-class_method

    #!/usr/bin/env ruby
    require 'thor'
    
    # cli.rb
    class MyCLI < Thor
    
      desc "hello NAME", "say hello to NAME"
      def hello(name)
        puts "Hello #{name}"
      end
    
      map 'hi' => :hello
    end
    
    MyCLI.start(ARGV)