MJN All Blog Cheatsheets Elasticsearch GCP JS LinuxBash Misc Notes Other ShortcutKeys / - Search

Home / LinuxBash / Script bash-gen-func - Example Bash Script


#!/bin/bash
help_text="
NAME
  bash-gen-func - Generates code and header of a bash function

USAGE
  bash-gen-func [options] <function_name> <parameters>

OPTIONS

  -f|--flag <param_name> <variable_name>
    A flag parameter.  If passed then a corresponding variable named <variable_name>_yn is 
    set to 'y'.

  -fv|--flagvalue <param_name> <variable_name>
    A flag plus value parameter.  If passed then a corresponding variable named
    <variable_name> is set to the value <value> passed.

  -h|--help
    Show help text.

  -o|--optional
    All flag / flag value parameter declared after this are marked as optional.

DESCRIPTION
  The script generates a function header and function code shell for a specified function
  name and a list of parameters.

AUTHOR
  mjnurse.dev - 2023
"
help_line="tbc"
web_desc_line="Example Bash Script"

try="Try ${0##*/} -h for more information"
tmp="${help_text##*USAGE}"
usage=$(echo "Usage: ${tmp%%OPTIONS*}" | tr -d "\n" | sed "s/  */ /g")

if [[ "$1" == "" ]]; then
  echo "${usage}"
  echo "${try}"
  exit 1
fi

while [[ "$1" != "" ]]; do
  case $1 in
    -h|--help)
      echo "$help_text"
      exit
      ;;
    ?*)
      break
      ;;
  esac
  shift
done

#*
#* This function runs through a set of parameters and generates a function description header.
#* In other words generate this header.
#*
#* {flag-value} -o optional_yn - signifies that any further parameters are optional.
#* {flag-value} -f <flag_value> - describes a flag_value parameter.
#* {opt-param} <parameter_1> - describes parameter in position 1.
#* {opt-param} <parameter_2> - describes parameter in position 2.
#* etc..
#*
function desc() {
  optional_yn=n
  echo "#*"
  echo "#* DESC"
  echo "#*"
  shift
  while [[ "$1" ]]; do
    case $1 in
      -o|--optional)
        optional_yn=y
        ;;
      -f|--flag)
        shift
        echo "#* {flag} -${1} ${2}_yn - DESC"
        shift
        ;;
      -fv|--flagvalue)
        shift
        echo "#* {flag-value} -${1} <${2}> - DESC"
        shift
        ;;
      ?*)
        if [[ ${optional_yn} == n ]]; then
          echo "#* {param} <${1}> - DESC"
        else
          echo "#* {opt-param} <${1}> - DESC"
        fi
        ;;
    esac
    shift
  done
  echo "#*"
}

#*
#* Function to print an flag error message.
#*
function flag_error() {
  echo
  echo "########################################################"
  echo "ERROR: Flag or flag with value listed after a parameter."
  echo "       Flags cannot follow a parameter."
  echo "########################################################"
  exit 1
}

function func() {
  optional_yn=n
  param_pos=1
  flags_yn=n
  function_name="$1"
  body=""
  paramlist=""
  function b() {
    body="$body$1\n"
  }
  function p() {
    paramlist="$paramlist\n$1"
  }
  shift
  if [[ $1 =~ -.* ]]; then
    b "  while [[ \"\$1 ]]; do"
    b "    case \$1 in"
    flags_yn=y
  fi
  while [[ "$1" ]]; do
    case $1 in
      -o|--optional)
        optional_yn=y
        ;;
      -f|--flag)
        if [[ ${flags_yn} == n ]]; then
          flag_error
        fi
        shift
        b "      -${1})"
        b "        ${1}_yn=y"
        b "        ;;"
        p "  ${1}_yn=n"
        shift
        ;;
      -fv|--flag-value)
        if [[ ${flags_yn} == n ]]; then
          flag_error
        fi
        shift
        b "      -${1})"
        b "        shift"
        b "        ${2}=\"\${1}\""
        b "        ;;"
        shift
        ;;
      ?*)
        if [[ ${flags_yn} == y ]]; then
          flags_yn=n;
          b "    esac"
          b "    shift"
          b "  done"
        fi
        if [[ ${optional_yn} == n ]]; then
          b "  ${1}=\"\${${param_pos}}\""
          b "  if [[ \"\$$1\" == \"\" ]]; then"
          b "    echo \"Error: Function: $function_name.  Mandatory parameter <$1> not set\""
          b "    exit 1"
          b "  fi"
        else
          b "  ${1}=\"\${${param_pos}}\" # Optional parameter"
        fi
        let param_pos=param_pos+1
        ;;
    esac
    shift
  done
  b "    esac"
  b "  done"

  echo "function ${function_name} () {"
  echo -e "$paramlist"
  echo -e "$body"
  echo "}"
}

desc $*
func $*


This page was generated by GitHub Pages. Page last modified: 23/08/17 08:54