My problem is: the required file intercepts the option parser.
I'm writing a ruby program in 3 files, one (connect.rb) that handles connection to a certain site, and another 2 (populate.rb and update.rb) that require the first one to collect data from that site.
connect.rb can be ran to configure and test the connection, as such:
$ruby connect.rb --adduser myuser -p 1234 --test
Created user myuser
Connecting as myuser...
Connection test OK
They way I solved something similar to this is to check if the file is being run solo like this:
# connect.rb # wrap logic in a module module Connect def self.run(args: ARGV) args # access to command line options end end # run if solo Connect.run if $0 == __FILE__
In the last conditional
$0 is a Ruby global set to the name of the executed file and
__FILE__ is the name of the current file. If both are the same then you know the file is running solo i.e.
With that setup you can run
connect.rb solo and it'll work fine. Then, when you require it in your other file
populate.rb You need to explicitly run it:
# popoulate.rb require 'connect' Connect.run ARGV.dup # dup the commandline options in case other files mutate them
Hope that helps.