Skip to content

Commit

Permalink
Load OptionParser defaults from XDG and Haiku standards
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed May 6, 2019
1 parent 970a25b commit 7e72ce0
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1806,13 +1806,26 @@ def candidate(word)
# is not present. Returns whether successfully loaded.
#
# +filename+ defaults to basename of the program without suffix in a
# directory ~/.options.
# directory ~/.options, then the basename with '.options' suffix
# under XDG and Haiku standard places.
#
def load(filename = nil)
begin
filename ||= File.expand_path(File.basename($0, '.*'), '~/.options')
rescue
return false
unless filename
basename = File.basename($0, '.*')
return true if load(File.expand_path(basename, '~/.options')) rescue nil
basename << ".options"
return [
# XDG
ENV['XDG_CONFIG_HOME'],
'~/.config',
*ENV['XDG_CONFIG_DIRS']&.split(File::PATH_SEPARATOR),

# Haiku
'~/config/settings',
].any? {|dir|
next if !dir or dir.empty?
load(File.expand_path(basename, dir)) rescue nil
}
end
begin
parse(*IO.readlines(filename).each {|s| s.chomp!})
Expand Down

0 comments on commit 7e72ce0

Please sign in to comment.