本文介绍了运行不带参数或任务名称的CLI Thor应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种创建命令行应用程序的方法,该应用程序将运行不带任何参数的默认方法.我摆弄了Thor的default_method选项,但仍然要求我传入一个参数.我发现了一个类似的情况运行带有参数但没有任务名称的CLI Thor任务.

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?

推荐答案

看来执行此操作的正确方法是使用default_task:

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

这篇关于运行不带参数或任务名称的CLI Thor应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 18:15