Last active
January 19, 2023 01:12
Revisions
-
rpearce revised this gist
Jan 19, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal 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="${0##*/}" VERSION_SHORT="$SCRIPT_NAME version $VERSION" VERSION_LONG="\ $VERSION_SHORT -
rpearce renamed this gist
Jan 5, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
rpearce revised this gist
Jan 5, 2023 . 1 changed file with 9 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal 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" 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 # local all_args=("$@") while :; do @@ -85,7 +85,7 @@ function parse_args { shift # past arg shift # past val else 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=) 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 print_error "warn: unknown flag (ignored): $flag" shift # past arg ;; # Default case, no more options -
rpearce revised this gist
Jan 5, 2023 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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 -
rpearce revised this gist
Jan 5, 2023 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -66,17 +66,17 @@ function parse_args { 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 @@ -87,29 +87,29 @@ function parse_args { 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 -
rpearce created this gist
Jan 5, 2023 .There are no files selected for viewing
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 charactersOriginal 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 "$@"