On Fri, Apr 30, 2021 at 11:54:16AM +0200, Marc Espie wrote:
> Until a patch from naddy, I wasn't even aware of getopts in sh(1)
> 
> Unless I made some mistakes, this translates the example in getopt(1)
> manpage.
> 
> It's likely some stronger wording might be adequate, I suspect some
> of the BUGS section in getopt(1) does not apply to the sh(1) built-in.
> 

hi marc.

is there a reason why you wanted the example in sh(1) and not ksh(1)?

i think the overall structure is that ksh(1) is the full monty, whereas
sh(1) is really a simplified reference to which bits you can use if you
want to keep things portable. to that end there aren;t really any
examples in sh(1). i felt that having a short page was more beneficial
in the long run (especially since we already have a full on ksh(1)
page).

having said that, getopts is one of those things that you really won;t
get from reading descriptive text. an example is definitely helpful. so
i'd not be dead against it in sh(1) if there's a good reason.

there's also the possibility that it may be more helpful to show it in
getopts(1) itself? i guess there are pros and cons there too.

jmc

> Index: bin/ksh/sh.1
> ===================================================================
> RCS file: /cvs/src/bin/ksh/sh.1,v
> retrieving revision 1.152
> diff -u -p -r1.152 sh.1
> --- bin/ksh/sh.1      22 May 2019 15:23:23 -0000      1.152
> +++ bin/ksh/sh.1      30 Apr 2021 09:51:42 -0000
> @@ -508,6 +508,26 @@ is a colon,
>  .Ev OPTARG
>  is set to the unsupported option,
>  otherwise an error message is displayed.
> +.Pp
> +The following example has identical functionality to the
> +example in
> +.Xr getopt 1 .
> +.Bd -literal -offset indent
> +while getopts abo: name
> +do
> +     case "$name"
> +     in
> +             a|b)
> +                     flag="-$name";;
> +             o)
> +                     oarg="$OPTARG";;
> +             ?)
> +                     echo "Usage: ..."
> +                     exit 2
> +             ;;
> +     esac
> +done
> +.Ed
>  .It Ic hash Op Fl r | Ar utility
>  Add
>  .Ar utility
> Index: usr.bin/getopt/getopt.1
> ===================================================================
> RCS file: /cvs/src/usr.bin/getopt/getopt.1,v
> retrieving revision 1.19
> diff -u -p -r1.19 getopt.1
> --- usr.bin/getopt/getopt.1   16 Mar 2018 16:58:26 -0000      1.19
> +++ usr.bin/getopt/getopt.1   30 Apr 2021 09:51:42 -0000
> @@ -54,7 +54,7 @@ which requires an argument.
>  args=`getopt abo: $*`
>  if [ $? -ne 0 ]
>  then
> -     echo 'Usage: ...'
> +     echo "Usage: ..."
>       exit 2
>  fi
>  set -- $args
> @@ -79,6 +79,11 @@ cmd -a -o arg file file
>  cmd -oarg -a file file
>  cmd -a -oarg -- file file
>  .Ed
> +Note that
> +.Xr sh 1
> +offers the
> +.Ic getopts
> +built-in with a simpler usage.
>  .Sh DIAGNOSTICS
>  .Nm
>  prints an error message on the standard error output when it
> 

Reply via email to