开发者

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.

H开发者_开发问答ow 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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜