Skip to content

Instantly share code, notes, and snippets.

@andkirby
Last active October 11, 2017 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andkirby/5546ab4cd52d0b68f7654354895e4d69 to your computer and use it in GitHub Desktop.
Save andkirby/5546ab4cd52d0b68f7654354895e4d69 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Script for testing options
set -o pipefail
set -o errexit
set -o nounset
#set -o xtrace
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
readonly __dir __file
ERR_FATAL=1
ERR_LOGIC=2
ERR_PARAMS=3
ERR_FILE_SYSTEM=4
VERBOSE_OFF=0
VERBOSE=1
VERBOSE_VERY=2
VERBOSE_DEBUG=3
APP_VERSION=0.1.0
t_red='\e[0;31m'
t_green='\e[0;32m'
t_yellow='\e[0;33m'
t_reset='\e[0m'
check_error () {
if [ $1 != 0 ]; then
echo -e "${t_red}error:${t_reset} "$2 > /dev/stderr
exit $1
fi
}
show_help () {
cat << EOF
Options test v${APP_VERSION}
Base format:
$ bash options-test.sh ARG1 [ARG2] [OPT1..OPTn]
ARGUMENTS
Pass argumenst:
ARG1 First argument.
ARG2 Second argument.
Default: "0"
OPTIONS
-p|--port PORT
Define custom connection port.
Default: 2222
-g|--use-vcs
Include VCS file into transition archives.
It excludes .idea files and VCS. This option will disable
excluding VCS files.
-s|--silent Disable output.
-v|--verbose Set "very" verbose mode.
-D|--debug Set "debug" verbose mode.
-H|--host HOST
Connection host. It can be passed with a username.
root@example.com
-e|--ignore-excluding
Ignore all default excluding.
-c|--create-destination
Create destination path automatically.
-x|--tar-exclude
Add exclude path on pack files.
-u|--unpack Unpack downloaded archive automatically in case when archive was created before transferring.
-V|--version
Show version number
-h|--help Show this help
EOF
}
process_params() {
# Process options and arguments
arg_first="${1}"
shift
arg_second=0
if [ "${1::1}" != '-' ]; then # check if it's not an option
arg_second="${1}"
shift
fi
local options
declare -A options
options=(
[w]=without-value
[a:]=with-value:
[h]=help
[q]=quiet
)
OPTS=`getopt -o $(echo ${!options[*]} | tr -d ' ') -l $(echo ${options[*]} | tr ' ' ',') -- "$@"`
eval set -- "${OPTS}"
without_value=0
while true; do
case "${1}" in
-w|--without-value)
without_value=1
shift
;;
-a|--with-value)
with_value="${2}"
shift 2
;;
-c|--create-destination)
create_target_path=1
shift
;;
-s|--silent)
sscp_verbose=${VERBOSE_OFF}
shift
;;
-v|--verbose)
verbose=${VERBOSE}
shift
;;
-vv)
verbose=${VERBOSE_VERY}
shift
;;
-vvv)
verbose=${VERBOSE_DEBUG}
shift
;;
-q|--quiet)
verbose=${VERBOSE_OFF}
-h|--help)
show_help
exit 0
;;
-\?)
show_help
exit 1
;;
--)
shift
break
;;
*)
check_error ${ERR_PARAMS} "${0}: unparsable option ${1}."
;;
esac
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment