Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
OptionParser#load
(?::String filename) -> bool
load(filename = nil, **keywords)
lib/optparse.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/lib/optparse.rb, line 2042
def load(filename = nil, **keywords)
unless filename
basename = File.basename($0, '.*')
return true if load(File.expand_path("~/.options/#{basename}"), **keywords) rescue nil
basename << ".options"
if !(xdg = ENV['XDG_CONFIG_HOME']) or xdg.empty?
# https://specifications.freedesktop.org/basedir-spec/latest/#variables
#
# If $XDG_CONFIG_HOME is either not set or empty, a default
# equal to $HOME/.config should be used.
xdg = ['~/.config', true]
end
return [
xdg,
*ENV['XDG_CONFIG_DIRS']&.split(File::PATH_SEPARATOR),
# Haiku
['~/config/settings', true],
].any? {|dir, expand|
next if !dir or dir.empty?
filename = File.join(dir, basename)
filename = File.expand_path(filename) if expand
load(filename, **keywords) rescue nil
}
end
begin
parse(*File.readlines(filename, chomp: true), **keywords)
true
rescue Errno::ENOENT, Errno::ENOTDIR
false
end
end
Loads options from file names as filename. Does nothing when the file is not present. Returns whether successfully loaded.
filename defaults to basename of the program without suffix in a directory ~/.options, then the basename with ‘.options’ suffix under XDG and Haiku standard places.
The optional into keyword argument works exactly like that accepted in method parse.