ruby-changes:73609
From: Nobuyoshi <ko1@a...>
Date: Mon, 19 Sep 2022 12:13:33 +0900 (JST)
Subject: [ruby-changes:73609] a0b0991eed (master): Downloader: Define per-class command line options
https://git.ruby-lang.org/ruby.git/commit/?id=a0b0991eed From a0b0991eed5b202b61323496560d8a803fddab02 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sat, 17 Sep 2022 22:48:40 +0900 Subject: Downloader: Define per-class command line options Move `Downloader::Unicode` specific options, and parse options after the downloader specificier. --- tool/downloader.rb | 60 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/tool/downloader.rb b/tool/downloader.rb index d3a9f75637..f09d44851e 100644 --- a/tool/downloader.rb +++ b/tool/downloader.rb @@ -36,6 +36,12 @@ else https://github.com/ruby/ruby/blob/trunk/tool/downloader.rb#L36 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://github.com/ruby/ruby/blob/trunk/tool/downloader.rb#L54 @@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 https://github.com/ruby/ruby/blob/trunk/tool/downloader.rb#L88 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 https://github.com/ruby/ruby/blob/trunk/tool/downloader.rb#L198 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 https://github.com/ruby/ruby/blob/trunk/tool/downloader.rb#L375 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__ https://github.com/ruby/ruby/blob/trunk/tool/downloader.rb#L412 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__ https://github.com/ruby/ruby/blob/trunk/tool/downloader.rb#L443 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 -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/