简体   繁体   English

Thor中的命令别名

[英]Command Aliasing in Thor

Is it possible to create aliases for commands in Thor? 可以在Thor中为命令创建别名吗?

Much like command aliasing in Commander. 就像Commander中的命令别名一样。 https://github.com/tj/commander#command-aliasing 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, 使用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. 我想将命令“hello”别名为“hi”。

You can use map for this: 您可以使用地图:

http://www.rubydoc.info/github/wycats/thor/master/Thor#map-class_method 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)

Use method_option for aliases. 使用method_option表示别名。

#!/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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM