I have the awk script below and things go wrong when I do
awk -v dsrmx=25 -f ./checkSRDry.awk --usage
I basically want to override the usual --usage and --help that awk gives.
How do people usually handle this situation when you also want to supply your own usage and help
concerning the script one has written?
################################################################################
#
# --Module---------------------------------------------------------------------
#
# checkSRDry.awk
#
# Prints warnings when the source-receiver distances exceed dsrmx.
#
# --Usage----------------------------------------------------------------------
#
# awk [optionalArgs] -f ./checkSRDry.awk file > file_error_log
#
# --Optional Arguments---------------------------------------------------------
#
# -v dsrmx=dist
# Source-receiver distance to check.
#
# file
# Input ray path file.
#
# file_error_log
# Output file containing warnings.
#
# -h, --help, -u, --usage, -e, --examples
# Prints a usage message to STDOUT and exits.
#
# --Operations-----------------------------------------------------------------
#
# usage()
# help(Version)
#
# --Examples-------------------------------------------------------------------
#
# awk -v dsrmx=65 -f ./checkSRDry.awk file > file_error_log
# awk -f ./checkSRDry.awk -h
#
# --History--------------------------------------------------------------------
#
# V01 - DEC 2009 - Christopher Dimech
# Initial release.
# V02 - JAN 2010 - Christopher Dimech
# Included command line arguments.
#
################################################################################
# abs: Returns the absolute value of val
function abs(val) {
return val > 0 ? val \
: -val
}
################################################################################
# usage: Description of this awk script
function usage() {
print ""
print "--Usage---------------------------------------------------------------"
print ""
print " awk [optionalArgs ] ./checkSRDry.awk file > file_error_log"
print " awk -f ./checkSRDry.awk -h"
print ""
print "--Optional Arguments--------------------------------------------------"
print ""
print " -v dsrmx=dist Source to receiver distance to check."
print " fin.ry Input ray path file."
print " fout.chk Output file containing warnings."
print " -h, --help, -u, --usage, -e, --examples"
print " Prints a usage message."
print ""
print "----------------------------------------------------------------------"
print ""
}
################################################################################
# examples:
function examples() {
print ""
print "--Examples----------------------------------------------------------"
print ""
print " awk -f ./checkSRDry.awk file > file_error_log"
print " awk -f ./checkSRDry.awk -h"
print ""
print "--------------------------------------------------------------------"
print ""
}
################################################################################
# help: Description of this awk script
function help() {
print ""
print "--Module--------------------------------------------------------------"
print ""
print " checkRays.awk"
print ""
print " Prints warning when source-receiver distances exceed dsrmx"
print ""
print "--Usage---------------------------------------------------------------"
print ""
print " awk [optionalArgs] -f ./checkSRDry.awk file > file_error_log"
print ""
print "--Optional Arguments--------------------------------------------------"
print ""
print " -v dsrmx=value"
print " Source to receiver distance to check"
print ""
print " file"
print " Input ray path file."
print ""
print " file_error_log"
print " Output file containing warnings."
print ""
print " -h, --help, -u, --usage, -e, --examples"
print " Prints a usage message."
print ""
print "--Examples-------------------------------------------------------------"
print ""
print " awk -v dsrmx=65 -f ./checkSRDry.awk file > file_error_log"
print ""
print "----------------------------------------------------------------------"
print ""
}
################################################################################
BEGIN {
c = ARGV[ARGC-1]
print c
if ((c == "-u") || (c == "--usage")) {
usage()
exit 1
}
if ((c == "-e") || (c == "--examples")) {
examples()
exit 1
}
if ((c == "-h") || (c == "--help")) {
help()
exit 1
}
if ( !dsrmx ) {
usage()
print ""
print "--Error-------------------------------------------------------------"
print ""
print " Source to receiver distance not set. Mandatory"
print " Set source to receiver distance using -v dsrmx=dist"
print ""
print "--------------------------------------------------------------------"
print ""
exit 1
}
RS = ">" # Change record separator
ARGV[ARGC++] = ARGV[ARGC-1] # Read file twice
}
# Error header
NR == 1 {
print ""
print "--Warnings------------------------------------------------------------"
print ""
print " Source to receiver distance exceeding",dsrmx,"Mm"
print ""
print "----------------------------------------------------------------------"
print ""
}
# --Read file: First time round-------------------------------------------------
FNR == NR { # Set number of records
++i
next
}
# --Read file: Second time round------------------------------------------------
NF > 2 {
srdist = abs( $1 - $(NF-2) )
if (srdist > dsrmx) { # Print error
print "WARNING: FNR="FNR", dist("$1","$(NF-2)")="srdist
}
}
END {
# if (c !~ /(-u|--usage|-e|--examples|-h|--help)/) {
# print c
# print ""
# print "--End of checkTrvtdat.awk---------------------------------------------"
# print ""
# }
}
################################################################################