help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Handling getopt for option without optional argument value


From: Lawrence Velázquez
Subject: Re: Handling getopt for option without optional argument value
Date: Fri, 23 Jul 2021 16:30:51 -0400
User-agent: Cyrus-JMAP/3.5.0-alpha0-540-g21c5be8f1e-fm-20210722.001-g21c5be8f

On Fri, Jul 23, 2021, at 4:08 PM, lisa-asket@perso.be wrote:
> I have the idea of adding 
> 
> 
> 
> local warn="1"
> 
> 
> 
> just before 
> 
> 
> 
> case "$2" in
> 
> 
> 
> The change will result in
> 
> 
> 
>  ("-w"|"--warning")
> 
>     local warn="1"
>     case "$2" in
>        (+([[:digit:]])) 
>           local warn="$2"
>           shift 2
>           ;;
>        (*) 
>           local warn="1"
>           shift 2
>            ;;
>        esac

This wouldn't fix anything.  The `warn` variable would still only
get set when '-w' or '--warning' is passed to your function.

I don't understand what you want at all.  Isn't the current behavior
fine?  Why would you want `warn` to be set by default?  What is the
behavior you are seeing, and what are you expecting instead?

By the way.

   if [[ -v $f ]] && (( f != 0 )); then

     # print normal multi-line text
     [[ ! -v $warn ]] && printf '%s\n' "$@"

     # print multi-line warnings

     rge='^[0-9]+$'
     if [[ -v $warn && "$warn" == "1" ]]; then
       printf '%s\n' ${red}"$1"${rgl}  # first line red
       printf '%s\n' "${@:2}"          # remaining, uncoloured
     elif [[ -v $warn && "$warn" =~ $rge ]]; then
       printf '%s\n' ${red}"$@"${rgl}  # all lines red
     fi

[[ -v $f ]] and [[ -v $warn ]] are both wrong.  They should be
[[ -v f ]] and [[ -v warn ]].

> Do you think it is better to use `declare` to set numeric values for `warn`?

Not really.

> declare -i local warn=1

Within a function, 'declare' and 'local' are basically equivalent.
So this would declare two local variables -- one named "warn" and
another literally named "local".  This has no advantage over what
you are already doing.


-- 
vq



reply via email to

[Prev in Thread] Current Thread [Next in Thread]