1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/optparse] #load() into hash

(https://github.com/ruby/optparse/pull/42)

OptionParser#load learns .load(into: Hash)

https://github.com/ruby/optparse/commit/2ea626fcff

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
This commit is contained in:
Whyme Lyu 2022-10-29 21:46:23 +08:00 committed by git
parent 4dac53f0a4
commit 37291df91d

View file

@ -1903,10 +1903,13 @@ XXX
# directory ~/.options, then the basename with '.options' suffix # directory ~/.options, then the basename with '.options' suffix
# under XDG and Haiku standard places. # under XDG and Haiku standard places.
# #
def load(filename = nil) # The optional +into+ keyword argument works exactly like that accepted in
# method #parse.
#
def load(filename = nil, into: nil)
unless filename unless filename
basename = File.basename($0, '.*') basename = File.basename($0, '.*')
return true if load(File.expand_path(basename, '~/.options')) rescue nil return true if load(File.expand_path(basename, '~/.options'), into: into) rescue nil
basename << ".options" basename << ".options"
return [ return [
# XDG # XDG
@ -1918,11 +1921,11 @@ XXX
'~/config/settings', '~/config/settings',
].any? {|dir| ].any? {|dir|
next if !dir or dir.empty? next if !dir or dir.empty?
load(File.expand_path(basename, dir)) rescue nil load(File.expand_path(basename, dir), into: into) rescue nil
} }
end end
begin begin
parse(*IO.readlines(filename).each {|s| s.chomp!}) parse(*IO.readlines(filename).each {|s| s.chomp!}, into: into)
true true
rescue Errno::ENOENT, Errno::ENOTDIR rescue Errno::ENOENT, Errno::ENOTDIR
false false