title: getopt to push user to input mandatory arguments
body: <p>I wrote a general getopt case switch before modifying the original script which runs based on simple arguments $1 $2 $3 etc, but not sure of the part to have getopt push a user to have mandatory flags/arguments.</p>
  <p>Main portion of the code, to show you:</p>
  <pre><code>usage()
  {
      echo -e &quot;\nMandatory Usage: $0 -d|--domain hello.com -t|--to to@test.com\n&quot;;
      echo -e &quot;If you want to put valid FROM address instead of sending from root@$(hostname), usage is:\n\t $0 -d|--domain hello.com -f|--from from@test.com -t|--to to@test.com\n&quot;;
  }
  
  options=$(getopt -o d:f:t: -l domain:,from:,to: -- &quot;$@&quot;)
  [ $? -eq 0 ] || {
      echo &quot;Incorrect options provided&quot;;
      usage;
      exit 1;
  }
  eval set -- &quot;$options&quot;
  while true; do
      case &quot;$1&quot; in
      -d|--domain)
          DOMAIN=$2;
          shift
          ;;
      -f|--from)
          FROM=$2;
          shift
          ;;
      -t|--to)
          TO=$2;
          shift
          ;;
      --)
          shift
          break
          ;;
      *)
          echo &quot;Invalid options!!&quot;;
          exit 1
          ;;
      esac
      shift
  done
  
  echo &quot;Domain is $DOMAIN&quot;
  echo &quot;From address is $FROM&quot;
  echo &quot;To address is $TO&quot;
  exit 0;
  </code></pre>
  <p>How to make these two flags -d and -t mandatory and -f as optional which takes default value if empty:</p>
  <p>To be precise:</p>
  <p>Mandatory flag usage should be: ((-f should not be mandatory and if not specified, takes a default value for it))</p>
  <pre><code>getopt_check -d hello.com -t to@test.com
  </code></pre>
  <p>Also should throw an error if the user runs the script simply without any arguments, like <code>bash getopt_check</code> without providing any flags.</p>
  <p>Also, it should throw an error when the user runs it like as follows, because of the mandatory flag -t missing:</p>
  <pre><code>getopt_check -d hello.com
  </code></pre>
  <p>Sincere apologies if this question has a triviality in it. I never used to getopt usage before and using it the first time and struggling a bit to get my first script worked successfully. I have been referring to many URLs and online guides but failed to solve my particular requirement with it.</p>