-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwget.rb
More file actions
executable file
·78 lines (70 loc) · 2.11 KB
/
Copy pathwget.rb
File metadata and controls
executable file
·78 lines (70 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby
########################################################################
# wget.rb: Simple Ruby-based File Downloader
#
# Description:
# This Ruby script downloads a file from a given URL and saves it
# locally. It is a basic implementation similar to the wget command.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# wget.rb <URL>
# Example: ruby wget.rb http://example.com/file.txt
#
# Requirements:
# - Ruby Version: 2.4 or later
#
# Version History:
# v1.3 2026-07-11
# Use $0 instead of $PROGRAM_NAME for the main-script check, matching
# the convention used by the other Ruby scripts in this repository.
# Also specify UTF-8 encoding when usage() reads the script's own
# source, to avoid an Encoding::CompatibilityError under a non-UTF-8
# locale.
# v1.2 2025-06-23
# Unified usage output to display full script header and support common help/version options.
# v1.1 2023-12-06
# Refactored for improved readability and added detailed comments.
# v1.0 2012-02-29
# Initial release.
#
########################################################################
require 'open-uri'
def usage
script = File.expand_path(__FILE__)
in_header = false
File.foreach(script, encoding: 'UTF-8') do |line|
if line.strip.start_with?('#' * 10)
in_header = !in_header
next
end
puts line.sub(/^# ?/, '') if in_header && line.strip.start_with?('#')
end
exit 0
end
# Compatibility wrapper for URI.open (Ruby 2.5+) vs Kernel.open (older versions)
def open_uri_compat(url, &block)
if URI.respond_to?(:open)
URI.open(url, &block)
else
Kernel.open(url, &block)
end
end
def main
if ARGV.empty? || ['-h', '--help', '-v', '--version'].include?(ARGV[0])
usage
end
url = ARGV.shift
filename = url.split(/\//).last
open_uri_compat(url) do |source|
File.open(filename, "w+b") do |o|
o.print source.read
end
end
return 0
end
exit(main) if __FILE__ == $0