I want to make my shebang enabled POSIX awk programs have more of a standard interface to them -- not using the -v var=val interface but using one that looks like other programs available from the Linux/UNIX command line. The issue that I have encountered is that awk scripts relay flags to awk -- and then awk itself has first crack at interpreting those flags. Also, different implementations of awk have different flag options. The end result is that it is nigh impossible to build an awk program with an interface that can just work parsing ARGC, ARGV[] for flags.
So, I end up encapsulating my awk programs in shell -- which adds to my support and testing burden -- and that shell code looks like the following:
arg_core=""
arg_directory=""
arg_module=""
arg_output=""
arg_regmap=""
arg_regpage=""
arg_help=0
arg_version=0
arg_verbose=0
while getopts c:d:m:o:p:r:hvV o
do
case "$o" in
c) arg_core="$OPTARG";;
d) arg_directory="$OPTARG";;
m) arg_module="$OPTARG";;
o) arg_output="$OPTARG";;
p) arg_regpage="$OPTARG";;
r) arg_regmap="$OPTARG";;
h) arg_help=1;;
v) arg_version=1;;
V) arg_verbose=1;;
--) break;;
?) help >&2
exit 1;;
esac
done
shift `expr $OPTIND - 1`
# Handling help and version (verbose option also displays revision
# history and notes) is more easily done outside the getopts loop.
if [ $arg_version -gt 0 ]
then
version
[ $arg_verbose -gt 0 ] && rev_history
fi
if [ $arg_help -gt 0 ]
then
[ $arg_version -gt 0 ] && echo
help
fi
[ $arg_help -gt 0 -o $arg_version -gt 0 ] && exit 0
awk -v arg_core="$arg_core" -v arg_directory="$arg_directory" -v arg_module="$arg_module" -v arg_output="$arg_output" -v arg_regmap="$arg_regmap" -v arg_regpage="$arg_regpage" -f rffe2tpf.awk -- "$@"
My question is... I want to eliminate the shell script encapsulation and want to do my argument parsing in awk -- and I want to do it portably. (Note... I am not asking, "How do I do getopts in awk?" I am instead asking, "How do I from the shebang in an awk script portably stop awk from parsing flags?") Is there a way to trick awk or the shebang into accomplishing this goal?
getop
function. Is that what you need? – user2350426