Created
September 16, 2022 15:55
-
-
Save User4574/b1bfcb57f17602610678089017c69fb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
#!DESCRIBE: Describe the purpose of an executable | |
require 'ptools' | |
def fatal(filename, msg) | |
$stderr.puts("[ERR] #{filename}: #{msg}") | |
exit 1 | |
end | |
filename = ARGV.shift | |
fatal("???", "File name not given.") if filename.nil? | |
unless File.exist?(filename) | |
ENV["PATH"].split(?:).each do |path| | |
test = File.join(path, filename) | |
if File.exist?(test) | |
filename = test | |
break | |
end | |
end | |
end | |
basename = File.basename(filename) | |
fatal(basename, "File does not exist.") unless File.exist?(filename) | |
fatal(basename, "File is not executable.") unless File.executable?(filename) | |
fatal(basename, "File is binary.") if File.binary?(filename) | |
file = File.readlines(filename) | |
describelines = file.select { |line| | |
line =~ /\A[#\/]*!DESCRIBE:/ | |
}.map { |line| | |
line.split("!DESCRIBE:")[1].strip | |
} | |
fatal(basename, "File is indescribable!") if describelines.empty? | |
puts "#{basename}: #{describelines[0]}" | |
describelines[1..-1].each { |line| | |
puts "#{" " * (basename.length + 2)}#{line}" | |
} | |
#!DESCRIBE: Usage: describe FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment