Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastlane set options auto value

Tags:

fastlane

I would like to submit my lane with optional options. So for example the lane:

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

How do I give :mailto a default value? So if I would run fastlane mylane it would automatically set :mailto to [email protected].

But if I would runfastlane mylane mailto:"[email protected]" it would use that value

like image 418
Roy Berris Avatar asked Nov 17 '17 10:11

Roy Berris


1 Answers

As Lyndsey Ferguson pointed out in a comment on this answer, the following is simplest:

mail_addr = options.fetch(:mailto, '[email protected]')

where the first parameter of fetch is the option to fetch, and the second is the default value if the option was not passed in.

I just want to add that this works a lot better than the other suggestion:

options[:mailto] || '[email protected]'

when dealing with boolean options.

Fastlane (or maybe Ruby) interprets true, false, yes, and no as boolean values instead of strings (maybe others too, though I tried N, n, NO, and FALSE and they were treated as strings), so if in your lane implementation you had:

options[:my_option] || true

or

(options[:my_option] || 'true') == 'true'

you would get unexpected behaviour.

If you didn't pass in myOption at all, this would default to true as you would expect. If you passed in true this would also return true. But if you passed in false this would turn into true, which you of course wouldn't want.

Using options.fetch(:myOption, true) works great with boolean flags like the ones mentioned above and therefore seems better to use in general.

Here's a very thorough example in case you want to test it yourself:

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

Outputs:

fastlane my_lane my_option:true

You passed in true
Using options[:my_option], the result is true
Using fetched, the result is true

fastlane my_lane my_option:false

You passed in false
Using options[:my_option], the result is true
Using fetched, the result is false

fastlane my_lane my_option:no

You passed in false
Using options[:my_option], the result is true
Using fetched, the result is false

Note, e.g. FALSE would default to true as it is not being interpreted as a boolean, which seems reasonable to me.

(Fastlane 1.77.0, Ruby 2.7.2)

EDIT: It's worth noting that if you pass an empty string instead of nothing/null you would not get the default value from the fetch method.

like image 64
shim Avatar answered Oct 10 '22 07:10

shim