Fastlane设置选项自动值

时间:2017-11-17 10:20:05

标签: fastlane

我想通过可选选项提交我的泳道。例如,车道:

lane :mylane do |options|
  mailgun(
      to: "#{options[:mailto]}"
      ....
    )
end

如何为:mailto提供默认值?因此,如果我运行fastlane mylane,它会自动将:mailto设置为mail@example.com。

但如果我要运行fastlane mylane mailto:"secondmail@example.com",它会使用该值

2 个答案:

答案 0 :(得分:3)

正如 Lyndsey Ferguson 在对 this answer 的评论中指出的那样,以下是最简单的:

mail_addr = options.fetch(:mailto, 'mail@example.com')

其中fetch的第一个参数是要获取的选项,第二个是没有传入选项的默认值。

我只想补充一点,这比其他建议要好得多:

options[:mailto] || 'mail@example.com'

处理布尔选项时。

Fastlane(或者可能是 Ruby)将 truefalseyesno 解释为布尔值而不是字符串(也许其他人也是如此,尽管我尝试了 {{ 1}}、NnNO 并且它们被视为字符串),因此如果在您的通道实现中您有:

FALSE

options[:my_option] || true

你会得到意想不到的行为。

如果您根本没有传入 (options[:my_option] || 'true') == 'true' ,则默认为 myOption,如您所料。如果您传入 true,这也将返回 true。但是,如果您传入 true,这将变成 false,您当然不希望这样。

使用 true 可以很好地处理上面提到的布尔标志,因此在一般情况下似乎更适合使用。

这是一个非常详尽的示例,以防您想自己测试:

options.fetch(:myOption, true)

输出:

lane :my_lane do |options| puts("You passed in #{options[:my_option]}") my_option = options[:my_option] || true if my_option puts('Using options[:my_option], the result is true') else puts('Using options[:my_option] the result is false') end my_option_fetched = options.fetch(:my_option, true) if my_option_fetched puts('Using fetched, the result is true') else puts('Using fetched, the result is false') end end

<块引用>

你传入的是真的
使用options[:my_option],结果为true
使用fetched,结果为true

fastlane my_lane my_option:true

<块引用>

你传入了 false
使用options[:my_option],结果为true
使用fetched,结果为false

fastlane my_lane my_option:false

<块引用>

你传入了 false
使用options[:my_option],结果为true
使用fetched,结果为false

注意,例如fastlane my_lane my_option:no 将默认为 FALSE,因为它不会被解释为布尔值,这对我来说似乎是合理的。

(快车道 1.77.0,Ruby 2.7.2)

答案 1 :(得分:1)

我不确定是否有办法让Fastlane通过默认设置。处理非常简单:

https://github.com/fastlane/fastlane/blob/master/fastlane/lib/fastlane/command_line_handler.rb#L10

但您可以在Fastfile中轻松完成此操作:

lane :mylane do |options|
  mail_addr = options[:mailto] || "mail@example.com"
  mailgun(
      to: "#{mail_addr}"
      ....
    )
end