On Tue, 15 Feb 2005, Frank K�ster wrote:

> "Francesco P. Lovergine" <[EMAIL PROTECTED]> schrieb:
>
> > On Tue, Feb 15, 2005 at 07:38:08AM -0600, John Hasler wrote:
> >> Francesco P. Lovergine writes:
> >> > It depends on programs, sometimes the same usage function is used for
> >> > either --help or invalid options.
> >>
> >> Sure, but the output should still be directed correctly.
> >
> > Quite difficult if the function is the same. In both cases it uses stderr.
>
> #!/bin/sh
>
> usage(){
>   if [ "$1" = "STDERR" ]; then
>     EXITCODE=1
>     exec >&2;
>   else
>       EXITCODE=0
>   fi
>   echo "bla..."
>   exit $EXITCODE
> }
>
> case "$1" in
>      --help|-h|-help)
>        usage;;
>      --foo)
>       ...
>      *)
>        usage STDERR;;
> esac
>
> Translation to Perl, Python, C, whatever, and to multiple arguments is
> left to the reader as an afternoon exercise.
What's with all this complexity?  Just redirect stdout to stderr when you call
the function.  Geez.

==
usage() {
        if [ $# -gt 0 ]; then
                echo "$@"
        fi
        echo foo
        echo baz
}

case "$1" in
        (-h)    usage; exit 0;;
        (*)     usage "Unknown option($1)" 1>&2; exit 1;;
esac
==

Reply via email to