Created
July 22, 2023 19:12
-
-
Save Stewie410/e1034e331f431ff14a743fbe03568491 to your computer and use it in GitHub Desktop.
Rewrite of 'Generic Bash Script Options' posted to reddit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # Generic Bash Script Args | |
| show_help() { | |
| cat << EOF | |
| Generic Bash Script Args | |
| USAGE: ${0##*/} [OPTIONS] -c PATH -r TIME -n NUM -s NUM | |
| ${0##*/} [OPTIONS] --copy-from PATH --creation TIME --var-num NUM --step NUM | |
| OPTIONS: | |
| -h, --help Show this help message | |
| REQUIRED: | |
| -c, --copy-from PATH Copy from PATH | |
| -r, --creation TIME Creation TIME | |
| -n, --var-num NUM Variable NUM | |
| -s, --step NUM Step NUM | |
| EOF | |
| } | |
| is_shell_compatible() { | |
| # Cannot assume bashisms are available | |
| [ -n "$BASH_VERSION" ] && return 0 | |
| printf '%s\n' "${0##*/} requires the BASH shell" >&2 | |
| return 1 | |
| } | |
| is_bash_compatible() { | |
| (( BASH_VERINFO[0] > 3 )) && return 0 | |
| printf '%s\n' "${0##*/} requires Bash v4.x or newer: '${BASH_VERSION}'" >&2 | |
| return 1 | |
| } | |
| require() { | |
| command -v "${1}" &>/dev/null && return | |
| printf '%s\n' "Missing dependency: '${1}'" >&2 | |
| return 1 | |
| } | |
| has_dependencies() { | |
| local err i | |
| for i in "grep" "git" "awk" "vim" "rm"; do | |
| require "${i}" || err="1" | |
| done | |
| return "${err:-0}" | |
| } | |
| are_required_vars_defined() { | |
| local i err | |
| for i in "${!requires[@]}"; do | |
| [[ -n "${requires[$i]}" ]] && continue | |
| printf '%s\n' "Required option not specified: '${i}'" >&2 | |
| err="1" | |
| done | |
| if [[ -n "${err}" ]]; then | |
| show_help | |
| return 1 | |
| fi | |
| return 0 | |
| } | |
| main() { | |
| local -A required | |
| local opts | |
| opts="$(getopt \ | |
| --options hc:r:n:s: \ | |
| --longoptions help,copy-from:,creation:,var-num:,step: \ | |
| --name "${0##*/}" \ | |
| -- "${@}" \ | |
| )" | |
| required=( | |
| ['copy-from']="" | |
| ['creation']="" | |
| ['var-num']="" | |
| ['step']="" | |
| ) | |
| eval set -- "${opts}" | |
| while true; do | |
| case "${1}" in | |
| -h | --help ) show_help; return 0;; | |
| -c | --copy-from ) required['copy-from']="${2}"; shift;; | |
| -r | --creation ) required['creation']="${2}"; shift;; | |
| -n | --var-num ) required['var-num']="${2}"; shift;; | |
| -s | --step ) required['step']="${2}"; shift;; | |
| -- ) shift; break;; | |
| * ) break;; | |
| esac | |
| shift | |
| done | |
| has_dependencies || return 1 | |
| are_required_vars_defined || return 1 | |
| cat <<- EOF | |
| Copy-From = "${required['copy-from']}" | |
| Creation = "${required['creation']}" | |
| Var-Num = "${required['var-num']}" | |
| Step = "${required['step']}" | |
| EOF | |
| return 0 | |
| } | |
| ( is_shell_compatible && is_bash_compatible ) || exit 2 | |
| main "${@}" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original Post