Command Aliasing in Thor
Asked Answered
A

2

5

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.

Amersham answered 13/2, 2015 at 16:37 Comment(0)
M
11

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)
Mcclish answered 13/2, 2015 at 16:52 Comment(2)
Seems that new versions of Thor expect the mapping parameter to be a string or an array and value a symbol. See rubydoc.info/github/wycats/thor/master/Thor#map-class_methodIchthyo
In this case it would be map "hi" => :helloUjiji
R
-3

Use method_option for aliases.

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

    # cli.rb
    class MyCLI < Thor
      desc "hello NAME", "say hello to NAME"
      method_option :hello , :aliases => "-hello" , :desc => "Hello Command" 
      def hello(name)
        puts "Hello #{name}"
      end
    end

    MyCLI.start(ARGV)
Rebatement answered 14/2, 2015 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.