Skip to content

Instantly share code, notes, and snippets.

@rpearce
Last active January 19, 2023 01:12

Revisions

  1. rpearce revised this gist Jan 19, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bash_args_parsing_template
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ set -eou pipefail
    VERSION="0.1.0"
    AUTHOR="Robert W. Pearce"

    SCRIPT_NAME="$(basename "$0")"
    SCRIPT_NAME="${0##*/}"
    VERSION_SHORT="$SCRIPT_NAME version $VERSION"
    VERSION_LONG="\
    $VERSION_SHORT
  2. rpearce renamed this gist Jan 5, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. rpearce revised this gist Jan 5, 2023. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions bash_template
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,7 @@ SCRIPT_NAME="$(basename "$0")"
    VERSION_SHORT="$SCRIPT_NAME version $VERSION"
    VERSION_LONG="\
    $VERSION_SHORT
    Author: $AUTHOR
    "
    Author: $AUTHOR"

    bold=$(tput bold)
    normal=$(tput sgr0)
    @@ -50,6 +49,10 @@ function print_version {
    echo "$VERSION_LONG"
    }

    function print_error {
    echo "$1" >&2
    }

    # ==============================================================================

    function parse_args {
    @@ -58,9 +61,6 @@ function parse_args {
    exit 1
    fi

    # Store the arguments for later if you want,
    # after they've been shifted out.
    #
    # local all_args=("$@")

    while :; do
    @@ -85,7 +85,7 @@ function parse_args {
    shift # past arg
    shift # past val
    else
    echo "error: --foo requires a non-empty argument"
    print_error "error: --foo requires a non-empty argument"
    exit 1
    fi
    ;;
    @@ -97,7 +97,7 @@ function parse_args {
    ;;
    # Foo: handle equals syntax wih no value
    --foo=)
    echo "error: --foo requires a non-empty argument"
    print_error "error: --foo requires a non-empty argument"
    exit 1
    ;;
    # End of all flags
    @@ -108,7 +108,8 @@ function parse_args {
    ;;
    # Unknown flag
    -?*)
    printf "warn: unknown flag (ignored): %s\n" "$flag" >&2
    # printf "warn: unknown flag (ignored): %s\n" "$flag" >&2
    print_error "warn: unknown flag (ignored): $flag"
    shift # past arg
    ;;
    # Default case, no more options
  4. rpearce revised this gist Jan 5, 2023. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions bash_template
    Original file line number Diff line number Diff line change
    @@ -58,6 +58,9 @@ function parse_args {
    exit 1
    fi

    # Store the arguments for later if you want,
    # after they've been shifted out.
    #
    # local all_args=("$@")

    while :; do
  5. rpearce revised this gist Jan 5, 2023. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions bash_template
    Original file line number Diff line number Diff line change
    @@ -66,17 +66,17 @@ function parse_args {

    case "$flag" in
    # Help menu
    "-h"|"-\?"|"--help")
    -h|-\?|--help)
    print_help
    exit 0
    ;;
    # Version
    "-v"|"--version")
    -v|--version)
    print_version
    exit 0
    ;;
    # Foo flag
    "-f"|"--foo")
    -f|--foo)
    if [ "$flag_val" ]; then
    echo "--foo value: $flag_val"
    shift # past arg
    @@ -87,29 +87,29 @@ function parse_args {
    fi
    ;;
    # Foo: handle equals syntax
    "--foo=?*")
    --foo=?*)
    local foo_val=${1#*=} # Delete everything up to "=" and assign the remainder
    echo "--foo=?* value is $foo_val"
    shift # past arg
    ;;
    # Foo: handle equals syntax wih no value
    "--foo=")
    --foo=)
    echo "error: --foo requires a non-empty argument"
    exit 1
    ;;
    # End of all flags
    "--")
    --)
    shift # past arg
    shift # past val
    break
    ;;
    # Unknown flag
    "-?*")
    -?*)
    printf "warn: unknown flag (ignored): %s\n" "$flag" >&2
    shift # past arg
    ;;
    # Default case, no more options
    "*")
    *)
    break
    ;;
    esac
  6. rpearce created this gist Jan 5, 2023.
    125 changes: 125 additions & 0 deletions bash_template
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,125 @@
    #!/bin/bash

    set -o errexit
    set -o errtrace
    set -o nounset
    set -eou pipefail

    # ==============================================================================

    VERSION="0.1.0"
    AUTHOR="Robert W. Pearce"

    SCRIPT_NAME="$(basename "$0")"
    VERSION_SHORT="$SCRIPT_NAME version $VERSION"
    VERSION_LONG="\
    $VERSION_SHORT
    Author: $AUTHOR
    "

    bold=$(tput bold)
    normal=$(tput sgr0)

    HELP="\
    This is a helpful description.
    ${bold}USAGE${normal}
    $SCRIPT_NAME <command> [flags]
    ${bold}FLAGS${normal}
    -h, -?, --help
    Print the help menu
    -v, --version
    Print version information
    -f <arg>, --foo <arg>
    Pass an argument for foo
    ${bold}EXAMPLES${normal}
    $ $SCRIPT_NAME -h
    $ $SCRIPT_NAME --version
    $ $SCRIPT_NAME --foo bar
    "

    # ==============================================================================

    function print_help {
    echo "$HELP" # | less -FRX
    }

    function print_version {
    echo "$VERSION_LONG"
    }

    # ==============================================================================

    function parse_args {
    if [[ -z "$*" ]]; then
    echo "No arguments provided"
    exit 1
    fi

    # local all_args=("$@")

    while :; do
    local flag="${1:-}"
    local flag_val="${2:-}"

    case "$flag" in
    # Help menu
    "-h"|"-\?"|"--help")
    print_help
    exit 0
    ;;
    # Version
    "-v"|"--version")
    print_version
    exit 0
    ;;
    # Foo flag
    "-f"|"--foo")
    if [ "$flag_val" ]; then
    echo "--foo value: $flag_val"
    shift # past arg
    shift # past val
    else
    echo "error: --foo requires a non-empty argument"
    exit 1
    fi
    ;;
    # Foo: handle equals syntax
    "--foo=?*")
    local foo_val=${1#*=} # Delete everything up to "=" and assign the remainder
    echo "--foo=?* value is $foo_val"
    shift # past arg
    ;;
    # Foo: handle equals syntax wih no value
    "--foo=")
    echo "error: --foo requires a non-empty argument"
    exit 1
    ;;
    # End of all flags
    "--")
    shift # past arg
    shift # past val
    break
    ;;
    # Unknown flag
    "-?*")
    printf "warn: unknown flag (ignored): %s\n" "$flag" >&2
    shift # past arg
    ;;
    # Default case, no more options
    "*")
    break
    ;;
    esac
    done
    }

    # ==============================================================================

    function main {
    parse_args "$@"
    }

    main "$@"