I wasn't trying to establish a format for various syntactic bits so much as trying to standardize how I handle common parts that I found myself rewriting for various new scripts.
For instance, the process of parsing arguments from the command line. I shamelessly borrowed a piece of code that handled arguments in a way that I liked, then enhanced it to make it simple to get a help screen with a list of available arguments.
Code:
#####
#
# ARGUMENTS
#
# process command line arguments - will override defaults -and- config file.
# Note: Variable names except CONF have trailing underscores so they will
# not be overridden by settings in the config file (if any).
# After config file is loaded, these will be used to override any
# conflicting parameters from the config file.
# Credit: this method of processing command line arguments was lifted
# directly from tinycore linux version 15.0 /etc/init.d/tcconfig
# changing only variable name "i" to "ARG" and tabs to two spaces.
# tnx to roberts and curaga
#
#<options>
ARGDATA=""
for ARG in ${*} ; do
case $ARG in
*=*)
case $ARG in
--conf=*) CONF=${ARG#*=} ;;
--debuglvl=*) DEBUGLVL_=${ARG#*=} ;;
-h=*) HELP_=${ARG#*=} ;;
--help=*) HELP_=${ARG#*=} ;;
esac
;;
*)
case $ARG in
-h) HELP_=1 ;;
--help) HELP_=1 ;;
*) ARGDATA="${ARGDATA} ${ARG}"
esac
;;
esac
done
#</options>
#
#####
This is a fairly straight-forward way to process arguments. You just add arguments to be looked for with "name=value" pairs in the first section and stand-alone "flag" arguments in the second. Having the layout of it already written is handy - but what makes it super useful are those two comment lines that look like HTML tags:
#<options> and
#</options>
because there's a help function in the template that parses the code between those two tags. The script parses itself to produce a help screen listing valid arguments.
Code:
available_args(){
# scan this script, ignoring all but the part between OPTSTART and OPTFINAL
# and parse the enclosed case statement for arguments accepted
OPTSTART="#<options>"
OPTFINAL="#</options>"
echo "The following lists valid command line arguments ( -h=/ ):"
INOPTS=0
CLVL=0
cat "${SCRIPTPATH}" |while read REC ; do
TERM1="`echo "${REC}" |awk '{print $1}'`"
[ "${TERM1}" = "${OPTSTART}" ] && INOPTS=1
[ "${TERM1}" = "${OPTFINAL}" ] && INOPTS=0
[ "${INOPTS}" = 0 ] && continue
[ "${TERM1}" = "case" ] && CLVL="`expr "${CLVL}" + 1`" && continue
[ "${TERM1}" = "esac" ] && CLVL="`expr "${CLVL}" - 1`" && continue
echo "${TERM1}" |grep -q "=" && TAG="name=value " || TAG="option flag"
[ "${CLVL}" = "2" ] && [ "${TERM1}" != '*)' ] && echo -n " ${TAG} : " && echo "${TERM1}" |cut -d')' -f1
done
}
(I have to confess - not the most "readable" code I've ever written.)
In a working script that can take a bunch of different arguments, the arguments help system works like this:
Code:
tc@dolly:~$ tce-lode -h=/
#####
#
# /boo/bin/tce-lode
#
tce-lode version 1.3 - Tiny Core extension downloader
by lem, 2016 - 2026
HELP=/
The following lists valid command line arguments ( -h=/ ):
name=value : --conf=*
name=value : --debuglvl=*
name=value : -h=*
name=value : --help=*
name=value : --krndir=*
name=value : --isodir=*
name=value : --tczdir=*
name=value : --dldir=*
name=value : --prot=*
name=value : --mirror=*
name=value : --corver=*
name=value : --relstat=*
name=value : --kernel=*
name=value : --arch=*
name=value : --filxs=*
name=value : --get-list=*
option flag : -h
option flag : --help
option flag : --get-core
option flag : --get-isos
#
#####
tc@dolly:~$
The template itself is about 300 lines, although about half of that is comments. The heavy comment count is because there's also a built in help function that produces help screens from the comments in the script code itself. There can be many help sections, depending upon the complexity of the script and how verbose I want to be. From the same script, the output of help index looks like this:
Code:
tc@dolly:~$ tce-lode -h=?
#####
#
# /boo/bin/tce-lode
#
tce-lode version 1.3 - Tiny Core extension downloader
by lem, 2016 - 2026
HELP=?
Available help sections ( -h=? ):
-h=1 : primary help section ( -h=1 or just -h )
-h=2 : (Template) secondary help section ( -h=2 )
-h=3 : (Template) help with help section ( -h=3 )
-h=4 : tce-lode - common arguments ( -h=4 )
-h=Y : Programming with script_template ( -h=Y )
-h=Z : Revision History help section ( -h=Z )
-h=? : help index (this list)
-h=/ : list valid option arguments
-h=+ : display all help sections
#
#####
tc@dolly:~$