On Wed, Apr 28, 2021 at 10:38:37PM +0200, Christian Weisgerber wrote:
> Switch the argument parsing from the deprecated getopt to getopts.
>
> ok?
Not before I see actual documentation pointing to getopts
Specifically: I wasn't even aware getopts existed.
It is a built-in in sh (ksh ?)
getopt(1) doesn't mention it as an alternative.
Oh, and sh(1) doesn't contain any usable example.
I'm not advocating against getopts proper.
I would just like it to be properly documented so that this code
doesn't appear out-of-the-blue first.
> Index: fetch_cmd.template
> ===================================================================
> RCS file: /cvs/ports/infrastructure/templates/fetch_cmd.template,v
> retrieving revision 1.1
> diff -u -p -r1.1 fetch_cmd.template
> --- fetch_cmd.template 28 Apr 2021 16:42:30 -0000 1.1
> +++ fetch_cmd.template 28 Apr 2021 20:34:41 -0000
> @@ -32,59 +32,35 @@ sessionfile=
> outputfile=
> pipeout=false
>
> -args=`getopt CmvVo:S: $*`
> -if [ $? -ne 0 ]
> -then
> - echo "Usage: $0 [ftp options] [files ...]"
> - exit 2
> -fi
> set -e
> -set -- $args
> -while [ $# -ne 0 ]
> +while getopts CmVvo:S: name
> do
> - case "$1" in
> - -C)
> - continued=true
> - shift
> - ;;
> - -v)
> - verbose=true
> - shift
> - ;;
> - -V)
> - verbose=false
> - shift
> - ;;
> - -m)
> - progressmeter=true
> - shift
> - ;;
> - -o)
> - outputfile=$2
> - case $outputfile in
> - -)
> - pipeout=true
> - ;;
> + case $name in
> + C) continued=true
> + ;;
> + m) progressmeter=true
> + ;;
> + V) verbose=false
> + ;;
> + v) verbose=true
> + ;;
> + o) outputfile=$OPTARG
> + case $outputfile in
> + -) pipeout=true ;;
> + esac
> + ;;
> + S) ssl=$OPTARG
> + case $ssl in
> + session=*)
> + sessionfile=${sessionfile#session=} ;;
> + esac
> + ;;
> + ?) echo "Usage: $0 [ftp options] [files ...]" >&2
> + exit 2
> + ;;
> esac
> - shift
> - shift
> - ;;
> - -S)
> - ssl=$2
> - shift
> - shift
> - case $ssl in
> - session=*)
> - sessionfile=`echo $ssl|sed -e 's/session=//'`
> - ;;
> - esac
> - ;;
> - --)
> - shift
> - break
> - ;;
> - esac
> done
> +shift $((OPTIND - 1))
>
> # for instance, a skeleton using curl
> args="-L -s"
> --
> Christian "naddy" Weisgerber naddy@mips.inka.de
>
>
No comments:
Post a Comment