#!/bin/bash # This script processes short and long command # line arguments. This is similar in function # to getopt and getopts but I am trying to make it # more flexible and usable since neither one # is a great solution for my purposes atleast. ### PROBLEMS ### ## - Too much repetition is required. The programming is forced to ## define the corresponding short and long arguments in two places. ## This is a waste of time and increases the chances of bugs. ## This *MUST* be fixed. ## Tuesday December 12th 7:57 PM ## --fatsheep until [ "$1" = "" ] ; do if [ "${1:0:1}" = "-" ] ; then if [ "${1:1:1}" = "-" ] ; then ################################## ### Processing Long Arguments #### ################################## case "${1:2}" in help ) helpflag="1";; verbose ) verbose="1";; info ) infoflag="1";; test ) testflag="1";; f ) filename="1" shift file="$1";; * ) # This case applies to all undefined long arguments. echo "Error: undefined argument: ${1}" errorflag="1";; esac else ################################## ### Processing Short Arguments ### ################################## nu="1" arglength="${#1}" until [ "$nu" -eq "$arglength" ] ; do case "${1:${nu}:1}" in h ) helpflag="1";; v ) verbose="1";; i ) infoflag="1";; t ) testflag="1";; f ) filename="1" shift file="$1";; * ) # This case applies to all undefined short arguments. echo "Error: undefined argument: -${1:${nu}:1}" errorflag="1";; esac nu="$((nu + 1))" done fi else echo "Error: undefined argument: ${1}" # catches all arguments not preceeded with '-' errorflag="1" fi shift done if [ "$errorflag" = "1" ] ; then echo "Argument Syntax Error: Exiting..." exit 1 fi if [ "$filename" = "1" ] && [ "$file" = "" ] ; then echo "Error: No file specified!" exit 1 fi if [ "$helpflag" = "1" ] ; then if [ "$verbose" = "1" ] ; then echo 'Display Help Verbosely!' else echo 'Display Help!' fi fi if [ "$infoflag" = "1" ] ; then if [ "$verbose" = "1" ] ; then echo 'Display Info Verbosely!' else echo 'Display Info!' fi fi if [ "$testflag" = "1" ] ; then if [ "$verbose" = "1" ] ; then echo 'Test Verbosely!' else echo 'Test!' fi fi if [ "$filename" = "1" ] ; then echo "Filename: $file" fi