Sample Header Ad - 728x90

Multichar options in Unix sysytems

0 votes
1 answer
244 views
I have a usecase wherein I need to support multiple character option. Currently I am using single character option for getopts. Multi-character option is desirable.What's the best way of doing this? I came across articles implementing a manual parser for this usecase,but is that optimal with respect to performance. I want something like -ab to be treated as -ab only and not -a -b . Also is it a good coding practice? This is just done to make the options more meaningful as opposed to single char options which do not provide complete information about that option. Important: I also want optargs with these multi-character options .Example -ab "sdfd" . Here's the code
while getopts ":s:p:q:j:o" opt; do
  case ${opt} in
    s)
      only_save=TRUE
      new_tok=$OPTARG
      ;;
    p)
      only_upload_enum_json=TRUE
      enum="job_status"
      new_tok=$OPTARG
      ;;
    q)
      only_upload_enum_json=TRUE
      enum="lifecycle_state"
      new_tok=$OPTARG
      ;;
    j)
      only_download_enum_json=TRUE
      enum=$OPTARG
      ;;
    o)
      only_download=TRUE
      ;;
    \?)
      echo "   -s "
      echo "   -p "
      echo "   -q "
      echo "   -j "
      echo "   -o "
      exit;
      ;;
  esac
done
Here for job_status its desirable to use -js instead of -p to make it more meaningful.and similarly for lifecycle_state
Asked by Vatsal A Mehta (101 rep)
Jun 30, 2023, 12:13 PM
Last activity: Jun 30, 2023, 02:10 PM