From ff910ae9491d81085e25d289f61cec72e96e57ef Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Mon, 9 Mar 2015 14:41:06 +0000 Subject: Moved usage-information to foot of script. This removes the nasty self-parsing of the script to generate the help/manual output. I've also removed global-variables and made the code almost-100% warning free. --- bin/multi-ping | 127 +++++++++++++++++++++++---------------------------------- 1 file changed, 51 insertions(+), 76 deletions(-) (limited to 'bin/multi-ping') diff --git a/bin/multi-ping b/bin/multi-ping index 32fb4ce..7957565 100755 --- a/bin/multi-ping +++ b/bin/multi-ping @@ -1,129 +1,104 @@ -#!/usr/bin/ruby -Ilib/ -I../lib/ +#!/usr/bin/ruby -Ilib/ # -# NAME -# multi-ping - IPv4 and IPv6 ping tool -# -# -# SYNOPSIS -# multi-ping [ -h | --help ] [-m | --manual] hostname -# -# -# OPTIONS -# -# -h, --help Show a help message, and exit. -# -# -m, --manual Show this manual, and exit. -# -# -# ABOUT -# -# The multi-ping tool is designed to be IPv4/IPv6-agnostic ping tool, -# which removes the need to know if you're pinging an IPv4 host or an -# IPv6 host. -# -# The tool works by resolving the hostname specified upon the command line, -# and invoking "ping" or "ping6" upon the result - using the correct one for -# the address family which has been returned. -# -# -# AUTHOR +# Usage information at the end of the script. # -# Steve Kemp -# - require 'getoptlong' require 'custodian/util/ping' - - # -# Options set by the command-line. These are all global. +# Options set by the command-line. # -$help = false -$manual = false +help = false +manual = false opts = GetoptLong.new( - ['--help', '-h', GetoptLong::NO_ARGUMENT], - ['--manual', '-m', GetoptLong::NO_ARGUMENT]) + ['--help', '-h', GetoptLong::NO_ARGUMENT], + ['--manual', '-m', GetoptLong::NO_ARGUMENT]) begin - opts.each do |opt, arg| + opts.each do |opt, _arg| case opt when '--help' then - $help = true + help = true when '--manual' then - $manual = true + manual = true end end rescue => err # any errors, show the help warn err.to_s - $help = true + help = true end - # -# CAUTION! Here be quality kode. +# Show the help information. # -if $manual or $help - - # Open the file, stripping the shebang line - lines = File.open(__FILE__){|fh| fh.readlines}[1..-1] - - found_synopsis = false - - lines.each do |line| - - line.chomp! - break if line.empty? - - if $help and !found_synopsis - found_synopsis = (line =~ /^#\s+SYNOPSIS\s*$/) - next - end - - puts line[2..-1].to_s - - break if $help and found_synopsis and line =~ /^#\s*$/ - +if manual || help + DATA.read.split("\n").each do |line| + puts Regexp.last_match(1).dup if line =~ /^# ?(.*)/ end - exit 0 end - - - - # # Get the address to ping. # hostname = ARGV.shift - # # Abort if we don't have a hostname # -if hostname.nil? +if hostname.nil? puts "Usage: #{$PROGRAM_NAME} hostname" exit 1 end - - # # Create the object # helper = Custodian::Util::Ping.new(hostname) - # # Ping the host, via the helper # -if helper.run_ping +if helper.run_ping puts "#{hostname} - #{helper.address} - is alive." exit 0 else exit 1 end + + +__END__ +# +# NAME +# multi-ping - IPv4 and IPv6 ping tool +# +# +# SYNOPSIS +# multi-ping [ -h | --help ] [-m | --manual] hostname +# +# +# OPTIONS +# +# -h, --help Show a help message, and exit. +# +# -m, --manual Show this manual, and exit. +# +# +# ABOUT +# +# The multi-ping tool is designed to be IPv4/IPv6-agnostic ping tool, +# which removes the need to know if you're pinging an IPv4 host or an +# IPv6 host. +# +# The tool works by resolving the hostname specified upon the command line, +# and invoking "ping" or "ping6" upon the result - using the correct one for +# the address family which has been returned. +# +# +# AUTHOR +# +# Steve Kemp +# -- cgit v1.2.1