-2

I want the ability to specify whether to use Normal Error Reporting or Silent Error Reporting provided with getopts.

Then I would be able to call my bash function like this

mari -s -n VAL -z VAL

The -s would set Silent Error Reporting (i.e. set opstring=":n:z:"), otherwise Normal Error Reporting (opstring="n:z:") will be used.

Currently I am hardwiring opstring inside the function.

mari() 
{

 impl=""
 if [[ "$impl"  == "SILENT" ]]; then
   opstring=":n:z:"  # Short options string
 else
   opstring="n:z:"
 fi

 while getopts "$opstring" opname; do
  case ${opname} in
    ("n")
      if [ -n "$OPTARG" ]; then
        echo "The string is not empty"
      else
        echo "The string is empty"
      fi
      ;;
    ("z")
      if [ -z "$OPTARG" ]; then
        echo "The string is empty"
      else
        echo "The string is not empty"
      fi
      ;;
    (?)
      ## Invalid Option Found, OPNAME set to '?'
      echo "Invalid option: -$OPTARG" 1>&2
      exit 1
      ;;
    (:)
      ## Required option argument not found, OPNAME set to ':'
      echo "Option -$OPTARG requires an argument" 1>&2
      exit 1
      ;;
  esac
 done
}
Dilna
  • 405
  • 1
  • 6
  • *"would like the users to specify ...*" Sorry, very unclear. Please edit your question to show examples of your problem usage. What I think you're saying is you'd like to have either ``opstring="n:z:"` OR `opstring=":n:z:"`. Yes? I think you would need to prompt the user "Silent Errors (Y/N)", have an `If/then/else` block that picks which of the 2 `optstring=` to use. If that is your need, based on code you have already posted, you can figure that out. .... If that is NOT what you need, you have to edit your question to show a sample use case of interactions with your user. Good luck. – shellter May 06 '23 at 16:18
  • Yes, without having to hardwire the `opstring` used by `getopts`. Can one skip the first option and have `getopts` process the rest? – Dilna May 06 '23 at 17:42
  • 1
    I"m still confused about what you're trying to achieve. Don't try to describe it verbally, **show us**. Create a "use-case" example to demonstrate what is needed. Show the user launching your program and then any other actions they must take to have it run as you need. **Edit your question above, don't reply in comments**. .... OR I really don't understand. Maybe others readers here will understand. Good luck. – shellter May 06 '23 at 23:40
  • 1
    Are you asking on multiple sites https://unix.stackexchange.com/questions/745061/introducing-option-to-select-whether-getopts-uses-silent-reporting-or-not ? – KamilCuk May 07 '23 at 08:35
  • 2
    I’m voting to close this question because the OP is multi-posting on multiple sites from multiple logins as usual. – Ed Morton May 07 '23 at 14:46
  • @EdMorton, I think that's cause for a moderator flag -- a mod can review the logs with IP addresses &c, and issue bans as appropriate. – Charles Duffy May 07 '23 at 16:09
  • @CharlesDuffy I've flagged this persons posts for those reasons before and I'm not aware of anything happening to those posts, nor has there being a change in the posters behavior. I did see a mod post a suggestion for how to merge accounts on one of their questions but that seemed to fall on deaf ears. – Ed Morton May 07 '23 at 17:02

1 Answers1

1

Modify the opstring once s is encountered.

? is a glob, it matches anything. You have to quote it.

mari() {
    local opstring
    opstring="sn:z:"
    while getopts "$opstring" opname; do
        case ${opname} in
        "s")
            opstring=":${opstring##:}"
            ;;
        "n")
            if [ -n "$OPTARG" ]; then
                echo "The string is not empty"
            else
                echo "The string is empty"
            fi
            ;;
        "z")
            if [ -z "$OPTARG" ]; then
                echo "The string is empty"
            else
                echo "The string is not empty"
            fi
            ;;
        '?')
            ## Invalid Option Found, OPNAME set to '?'
            echo "Invalid option: -$OPTARG" 1>&2
            exit 1
            ;;
        ':')
            ## Required option argument not found, OPNAME set to ':'
            echo "Option -$OPTARG requires an argument" 1>&2
            exit 1
            ;;
        esac
    done
}

echo "no invalid option:"
mari -s -bla
echo "invalid option:"
mari -bla
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • I did not know that one can modify `opstring` at runtime. – Dilna May 07 '23 at 08:54
  • `while getopts "$opstring"` is executed on every loop... – KamilCuk May 07 '23 at 08:56
  • Why do you remove trailing colon ? – Dilna May 07 '23 at 09:32
  • I always mix %% and ##. I even asked https://stackoverflow.com/questions/66421468/how-to-remember-which-expansion-var-var-work-from-which-end and I am still mixing it. – KamilCuk May 07 '23 at 09:37
  • What do you want to remove from `opstring` then ? And why is it necessary? – Dilna May 07 '23 at 09:41
  • If not removing, in case you do `mari -s -s` where will be two `::` in front of opstring. No idea, maybe that's not a problem. – KamilCuk May 07 '23 at 09:41
  • 2
    On my keyboard setup the `#` is also the numeric `3 key`, whilst `%` is also the numeric `5 key`. As `3` occurs first, I interpret it as deletion of `leading` pattern, whereas `%` as deletion of `trailing` pattern. Would this help remembering ? – Dilna May 07 '23 at 09:45
  • We then have to ensure that there is only one colon (`:`) at the beginning of `opstring` when the `s` option is called. – Dilna May 07 '23 at 11:11