简体   繁体   中英

How can I use a flag as a command with Thor

Given a Ruby program using Thor, how can I implement a method that gets called when an argument that looks like a flag is called.

For example, if I run this on the command line:

mycmd --version

I would like to execute the code:

desc 'version', 'Print version number'
def version
  puts "mycmd version #{Mycmd::VERSION}"
end

You can make a "top level" default task, which examines its arguments and outputs the correct thing:

class MyThing < Thor
  desc "meta", "Information about the task itself"
  argument :name
  def meta
    if name == "--version"
      puts "v 1.1.1"
    elsif name == "--author"
      puts "meagar"
    end
  end
  default_task :meta
end

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