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

Downloader: Define per-class command line options

Move `Downloader::Unicode` specific options, and parse options after
the downloader specificier.
This commit is contained in:
Nobuyoshi Nakada 2022-09-17 22:48:40 +09:00
parent e2e1058e66
commit a0b0991eed
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6

View file

@ -36,6 +36,12 @@ else
end end
class Downloader class Downloader
def self.find(dlname)
constants.find do |name|
return const_get(name) if dlname.casecmp(name.to_s) == 0
end
end
def self.https=(https) def self.https=(https)
@@https = https @@https = https
end end
@ -48,6 +54,10 @@ class Downloader
@@https @@https
end end
def self.get_option(argv, options)
false
end
class GNU < self class GNU < self
def self.download(name, *rest) def self.download(name, *rest)
if https? if https?
@ -78,6 +88,21 @@ class Downloader
INDEX = {} # cache index file information across files in the same directory INDEX = {} # cache index file information across files in the same directory
UNICODE_PUBLIC = "https://www.unicode.org/Public/" UNICODE_PUBLIC = "https://www.unicode.org/Public/"
def self.get_option(argv, options)
case argv[0]
when '--unicode-beta'
options[:unicode_beta] = argv[1]
argv.shift(2)
true
when /\A--unicode-beta=(.*)/m
options[:unicode_beta] = $1
argv.shift
true
else
super
end
end
def self.download(name, dir = nil, since = true, options = {}) def self.download(name, dir = nil, since = true, options = {})
options = options.dup options = options.dup
unicode_beta = options.delete(:unicode_beta) unicode_beta = options.delete(:unicode_beta)
@ -173,7 +198,6 @@ class Downloader
options = options.dup options = options.dup
url = URI(url) url = URI(url)
dryrun = options.delete(:dryrun) dryrun = options.delete(:dryrun)
options.delete(:unicode_beta) # just to be on the safe side for gems and gcc
if name if name
file = Pathname.new(under(dir, name)) file = Pathname.new(under(dir, name))
@ -351,7 +375,25 @@ Downloader.https = https.freeze
if $0 == __FILE__ if $0 == __FILE__
since = true since = true
options = {} options = {}
dl = nil
(args = []).singleton_class.__send__(:define_method, :downloader?) do |arg|
!dl and args.empty? and (dl = Downloader.find(arg))
end
until ARGV.empty? until ARGV.empty?
if ARGV[0] == '--'
ARGV.shift
break if ARGV.empty?
ARGV.shift if args.downloader? ARGV[0]
args.concat(ARGV)
break
end
if dl and dl.get_option(ARGV, options)
# the downloader dealt with the arguments, and should be removed
# from ARGV.
next
end
case ARGV[0] case ARGV[0]
when '-d' when '-d'
destdir = ARGV[1] destdir = ARGV[1]
@ -370,26 +412,18 @@ if $0 == __FILE__
when '--cache-dir' when '--cache-dir'
options[:cache_dir] = ARGV[1] options[:cache_dir] = ARGV[1]
ARGV.shift ARGV.shift
when '--unicode-beta'
options[:unicode_beta] = ARGV[1]
ARGV.shift
when /\A--cache-dir=(.*)/m when /\A--cache-dir=(.*)/m
options[:cache_dir] = $1 options[:cache_dir] = $1
when /\A-/ when /\A-/
abort "#{$0}: unknown option #{ARGV[0]}" abort "#{$0}: unknown option #{ARGV[0]}"
else else
break args << ARGV[0] unless args.downloader? ARGV[0]
end end
ARGV.shift ARGV.shift
end end
dl = Downloader.constants.find do |name|
ARGV[0].casecmp(name.to_s) == 0
end unless ARGV.empty?
$VERBOSE = true $VERBOSE = true
if dl if dl
dl = Downloader.const_get(dl) args.each do |name|
ARGV.shift
ARGV.each do |name|
dir = destdir dir = destdir
if prefix if prefix
name = name.sub(/\A\.\//, '') name = name.sub(/\A\.\//, '')
@ -409,7 +443,7 @@ if $0 == __FILE__
dl.download(name, dir, since, options) dl.download(name, dir, since, options)
end end
else else
abort "usage: #{$0} url name" unless ARGV.size == 2 abort "usage: #{$0} url name" unless args.size == 2
Downloader.download(ARGV[0], ARGV[1], destdir, since, options) Downloader.download(args[0], args[1], destdir, since, options)
end end
end end