Skip to content

Instantly share code, notes, and snippets.

@User4574
Created September 16, 2022 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save User4574/b1bfcb57f17602610678089017c69fb0 to your computer and use it in GitHub Desktop.
Save User4574/b1bfcb57f17602610678089017c69fb0 to your computer and use it in GitHub Desktop.
#!/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