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