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
精彩评论