Run a CLI Thor app without arguments or task name

dhulihan picture dhulihan · Aug 31, 2011 · Viewed 7.6k times · Source

I'm looking for a way to create a command-line thor app that will run a default method without any arguments. I fiddled with Thor's default_method option, but still requires that I pass in an argument. I found a similar case where someone wanted to run a CLI Thor task with arguments but without a task name.

I'd like to run a task with no task name and no arguments. Is such a thing possible?

Answer

Jakob S picture Jakob S · Aug 31, 2011

It seems the proper Thor-way to do this is using default_task:

class Commands < Thor
  desc "whatever", "The default task to run when no command is given"
  def whatever
    ...
  end
  default_task :whatever
end
Commands.start

If for whatever reason that isn't what you need, you should be able to do something like

class Commands < Thor
  ...
end

if ARGV.empty?
  # Perform the default, it doesn't have to be a Thor task
  Commands.new.whatever
else
  # Start Thor as usual
  Commands.start
end