运行不带参数或任务名称的 CLI Thor 应用程序

发布于 2024-12-02 09:01:03 字数 298 浏览 2 评论 0原文

我正在寻找一种方法来创建一个命令行雷神应用程序,该应用程序将运行不带任何参数的默认方法。我摆弄了 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏爱自由 2024-12-09 09:01:03

似乎执行此操作的正确雷神方法是使用 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

如果出于某种原因这不是您所需要的,您应该能够执行类似的操作

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

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
撩心不撩汉 2024-12-09 09:01:03

有点黑客,但无论如何只有一个定义的操作,我只是将操作名称添加到传入的 ARGV 数组中:

class GitTranslate < Thor
  desc "translate <repo-name>", "Obtain a full url given only a repo name"
  option :bitbucket, type: :boolean, aliases: 'b' 
  def translate(repo)
    if options[:bitbucket]
      str = "freedomben/#{repo}.git"
      puts "SSH:   [email protected]:#{str}"
      puts "HTTPS: https://[email protected]/#{str}"
    else
      str = "FreedomBen/#{repo}.git"
      puts "SSH:   [email protected]:#{str}"
      puts "HTTPS: https://github.com/#{str}"
    end 
  end 
end

然后我通过传入 ARGV 来启动该类:

GitTranslate.start(ARGV.dup.unshift("translate"))

Kind of hackish, but where there's only one defined action anyway, I just prepended the action name to the ARGV array that gets passed in:

class GitTranslate < Thor
  desc "translate <repo-name>", "Obtain a full url given only a repo name"
  option :bitbucket, type: :boolean, aliases: 'b' 
  def translate(repo)
    if options[:bitbucket]
      str = "freedomben/#{repo}.git"
      puts "SSH:   [email protected]:#{str}"
      puts "HTTPS: https://[email protected]/#{str}"
    else
      str = "FreedomBen/#{repo}.git"
      puts "SSH:   [email protected]:#{str}"
      puts "HTTPS: https://github.com/#{str}"
    end 
  end 
end

Then where I start the class by passing in ARGV:

GitTranslate.start(ARGV.dup.unshift("translate"))
心是晴朗的。 2024-12-09 09:01:03

虽然它有点hackish,但我通过捕获选项作为参数本身解决了类似的问题:

argument :name

def init
 if name === '--init'
   file_name = ".blam"
   template('templates/blam.tt', file_name) unless File.exists?(file_name)
   exit(0)
 end
end

当在 Thor::Group 中运行时,此方法在其他方法之前执行,让我欺骗程序做出响应到像论证这样的选项。

此代码来自 https://github.com/neverstopbuilding/blam

While it is a little hackish, I solved a similar problem by catching the option as the argument itself:

argument :name

def init
 if name === '--init'
   file_name = ".blam"
   template('templates/blam.tt', file_name) unless File.exists?(file_name)
   exit(0)
 end
end

When running in a Thor::Group this method is executed before others and lets me trick the program into responding to an option like argument.

This code is from https://github.com/neverstopbuilding/blam.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文