【问题标题】:Make Thor show message for top level command让 Thor 显示顶级命令的消息
【发布时间】:2018-11-21 18:54:35
【问题描述】:

有什么方法可以让 Thor 显示顶级命令的一般信息?

$my_command help

I'd like to show a welcome message here.

Commands:
  my_command help [COMMAND]

【问题讨论】:

    标签: ruby thor


    【解决方案1】:

    我能想到的最接近的事情是添加一个默认任务并使用它来调用帮助任务。在不带参数的情况下调用 $my_command 时会收到此消息

    require 'thor'
    class MyCLI < Thor
      desc "hello NAME", "say hello to NAME"
      def hello(name)
        puts "Hello #{name}"
      end
      desc "greeting", "this is just a greeting"
      def greeting
        puts "Welcome to MyCLI"
        invoke :help
      end
      default_task :greeting
    end
    
    MyCLI.start(ARGV)
    
    # $my_command
    # output:
    
    # Welcome to MyCLI
    # Commands:
    #   test.rb greeting        # this is just a greeting
    #   test.rb hello NAME      # say hello to NAME
    #   test.rb help [COMMAND]  # Describe available commands or one spec...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-25
      • 2014-02-05
      • 2015-04-18
      • 2019-03-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      相关资源
      最近更新 更多