getopts - macOS


Overview

getopts is a command used in shell scripts to parse positional parameters and options. It is commonly employed in scripts that need to handle options and arguments passed to them, allowing the script to accept different kinds of inputs flexibly. It is typically used in scenarios where user-defined options alter the behavior of the script.

Syntax

The syntax for getopts in a shell script is:

getopts option_string variable_name
  • option_string: A string containing the option characters to be recognized; if a character is followed by a colon (:), it indicates that the option expects an argument.
  • variable_name: The name of the shell variable that is to be assigned the option character found by getopts.

Options/Flags

getopts does not have traditional options or flags like most commands. Instead, the behavior of getopts is controlled through the option_string parameter:

  • Option characters: Each character in option_string represents an option the script should recognize.
  • Colon (:): When an option character is followed by a colon, it indicates that the option requires an argument.

When getopts encounters an option that requires an argument, it places that argument into the variable OPTARG. The index of the next argument to be processed is stored in the variable OPTIND.

Examples

Basic Example

while getopts ":a:b:" opt; do
  case ${opt} in
    a )
      echo "Option -a with value ${OPTARG}"
      ;;
    b )
      echo "Option -b with value ${OPTARG}"
      ;;
    \? )
      echo "Invalid option: $OPTARG"
      ;;
  esac
done

This script processes options -a and -b, both expecting arguments. When an unknown option is passed, it prints an error message.

Handling Optional Arguments

while getopts ":a::b:" opt; do
  case ${opt} in
    a )
      if [[ -n $OPTARG ]]; then
        echo "Option -a with value ${OPTARG}"
      else
        echo "Option -a with no value"
      fi
      ;;
    b )
      echo "Option -b with value ${OPTARG}"
      ;;
  esac
done

This modification allows -a to have an optional argument.

Common Issues

  • Unrecognized Options: Users frequently face issues when they accidentally pass options that are not defined in the option_string.
  • Option Requires an Argument: If a script expects an option to have an argument (indicated by : in option_string), but it is omitted, getopts will place a ? into the opt variable.

Integration

getopts is often used in conjunction with other commands and scripts to create robust and flexible user interfaces in scripts. For instance, it can be integrated with case statements to handle different flags and arguments effectively.

Example of integrating with a logging function:

while getopts ":v" opt; do
  case $opt in
    v )
      verbose_mode=true
      ;;
  esac
done

if [[ $verbose_mode == true ]]; then
  echo "Verbose mode is on."
fi
  • getopt: A more flexible external command that can handle long options (--option). Not built-in in some environments.
  • bash and sh: Shell environments where getopts is often used.

For further reading, consult the official GNU Bash manual, which provides a detailed guide on using getopts in scripting contexts.