Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Thor, can I pass the CLI an argument only (not a task) and send it to a default method/task?

I am using Thor to create a CLI for a Ruby gem that I am making. The executable would ideally take a command like myapp path/to/file, in the sense that I would rather the user not have to define a task, only an argument.

I've looked over the API but default_task only works when no task/argument is present.

How can I make Thor send this variable file argument to a default method/task, and not interpret it like a task that does not exist?

like image 969
Walking Wiki Avatar asked Nov 14 '22 04:11

Walking Wiki


1 Answers

myapp path/to/file

two part answer:

1) myapp ... to use an executable other than 'thor' you will need to utilize the 'thor/runner' library.

2) path/to/file can be accomplished in the initialize method, like so:

class Something < Thor
  def initialize(*args)
    super
    case @path
      when /something$/; self.class.new([@path],options).do_run
    end
  end

  desc 'do_run', "do something"
  argument :path, :banner=>"path/to/file", :optional=>true
  def do_run
    # something
  end
end
like image 115
jdar Avatar answered Dec 22 '22 09:12

jdar