#!/bin/sh

init_variables() {
  command=${0##*/}
  arg=
  operand=
}

parse_command_line() {
  # reset in case getopts has been used previously in the shell.
  OPTIND=1
  while getopts 'o:' opt; do
    # shellcheck disable=SC2249
    case "${opt}" in
      o) arg="${OPTARG}" ;;
      ?) exit_error ;;
    esac
  done
  shift "$((OPTIND - 1))"

  operand="$1"
}

validate_command_line() {
  if [ -z "${operand}" ]; then
    exit_error 'missing operand'
  fi
}

exit_error() {
  [ -z "$1" ] || echo "${command}: $1"
  exit 1
} >&2

print_items() {
  printf 'arg: %s\n' "${arg}"
  printf 'operand: %s\n' "${operand}"
}

init_variables
parse_command_line "$@"
validate_command_line

print_items
