简体   繁体   中英

Make Thor show message for top level command

Is there any way to make Thor show a general message for the top level command?

$my_command help

I'd like to show a welcome message here.

Commands:
  my_command help [COMMAND]

The closest thing I can think of is adding a default task and using it to invoke the help task. You'd get this message when calling $my_command with no arguments

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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