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

I am using Thor to create the CLI for the Ruby gem that I am making. Ideally, the executable should accept a command like myapp path/to/file in the sense that I would prefer that the user does not have to define the task, but only an argument.

I looked at the API, but default_task only works if there are no jobs / arguments.

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

+4
source share
1 answer

myapp path / to / file

The answer is in two parts:

1) myapp ... in order to use an executable file other than thor, you will need to use the thor / runner library.

2) path/to/file can be executed in the initialization method, for example:

 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
source

Source: https://habr.com/ru/post/1334767/


All Articles