Skip to content

Commit

Permalink
Alias getopt command
Browse files Browse the repository at this point in the history
This is to allow replacing the `getopt` command with a full path alias, e.g. for homebrew

Signed-off-by: Cristian Le <[email protected]>
  • Loading branch information
LecrisUT committed Mar 22, 2024
1 parent 35fc954 commit a4825d3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
16 changes: 10 additions & 6 deletions src/infrastructure.sh
Expand Up @@ -28,6 +28,10 @@
# Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Command aliases for compatibilities
declare GETOPT_CMD="getopt"

echo "${__INTERNAL_SOURCED}" | grep -qF -- " ${BASH_SOURCE} " && return || __INTERNAL_SOURCED+=" ${BASH_SOURCE} "

: <<'=cut'
Expand Down Expand Up @@ -219,7 +223,7 @@ Returns 0 if mounting the share was successful.

rlMount() {
local OPTIONS=''
local GETOPT=$(getopt -o o: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)); eval set -- "$GETOPT"
local GETOPT=$($GETOPT_CMD -o o: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)); eval set -- "$GETOPT"
while true; do
case $1 in
--) shift; break; ;;
Expand Down Expand Up @@ -288,7 +292,7 @@ options, 2 otherwise.

rlCheckMount() {
local MNTOPTS=''
local GETOPT=$(getopt -o o: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)); eval set -- "$GETOPT"
local GETOPT=$($GETOPT_CMD -o o: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)); eval set -- "$GETOPT"
while true; do
case $1 in
--) shift; break; ;;
Expand Down Expand Up @@ -383,7 +387,7 @@ the mountpoint uses all the given options.

rlAssertMount() {
local MNTOPTS=''
local GETOPT=$(getopt -o o: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)); eval set -- "$GETOPT"
local GETOPT=$($GETOPT_CMD -o o: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)); eval set -- "$GETOPT"
while true; do
case $1 in
--) shift; break; ;;
Expand Down Expand Up @@ -461,7 +465,7 @@ Returns 0 if success.
=cut

rlHash() {
local GETOPT=$(getopt -o a: -l decode,algorithm:,stdin -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)); eval set -- "$GETOPT"
local GETOPT=$($GETOPT_CMD -o a: -l decode,algorithm:,stdin -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)); eval set -- "$GETOPT"
local decode=0 alg="$rlHashAlgorithm" stdin=0
while true; do
case $1 in
Expand Down Expand Up @@ -637,7 +641,7 @@ rlFileBackup() {
local IFS

# getopt will cut off first long opt when no short are defined
OPTS=$(getopt -o "." -l "clean,namespace:,no-missing-ok,missing-ok" -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
OPTS=$($GETOPT_CMD -o "." -l "clean,namespace:,no-missing-ok,missing-ok" -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
[ $? -ne 0 ] && return 1

eval set -- "$OPTS"
Expand Down Expand Up @@ -815,7 +819,7 @@ rlFileRestore() {
local IFS

# getopt will cut off first long opt when no short are defined
OPTS=$(getopt -o "n:" -l "namespace:" -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
OPTS=$($GETOPT_CMD -o "n:" -l "namespace:" -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
[ $? -ne 0 ] && return 1

eval set -- "$OPTS"
Expand Down
6 changes: 5 additions & 1 deletion src/logging.sh
Expand Up @@ -30,6 +30,10 @@
# Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Command aliases for compatibilities
declare GETOPT_CMD="getopt"

echo "${__INTERNAL_SOURCED}" | grep -qF -- " ${BASH_SOURCE} " && return || __INTERNAL_SOURCED+=" ${BASH_SOURCE} "

export __INTERNAL_DEFAULT_SUBMIT_LOG=__INTERNAL_FileSubmit
Expand Down Expand Up @@ -517,7 +521,7 @@ rlFileSubmit -s '_' /etc/passwd -> etc_passwd
=cut

rlFileSubmit() {
GETOPT=$(getopt -o s: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
GETOPT=$(GETOPT_CMD -o s: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
eval set -- "$GETOPT"

SEPARATOR='-'
Expand Down
9 changes: 6 additions & 3 deletions src/lsb_release
Expand Up @@ -62,6 +62,9 @@
# DECLARATIONS
###############################################################################

# Command aliases for compatibilities
GETOPT_CMD="getopt"

# This script version
SCRIPTVERSION="2.0"

Expand Down Expand Up @@ -147,17 +150,17 @@ Usage() {

# Handles the enhanced args (i.e. --something)
EnhancedGetopt() {
getopt -T >/dev/null 2>&1 # is getopt the enhanced one ?
$GETOPT_CMD -T >/dev/null 2>&1 # is getopt the enhanced one ?
if [ $? = 4 ]
then # Yes, advanced args ALLOWED
OPT=$(getopt -o acdhirsvp \
OPT=$($GETOPT_CMD -o acdhirsvp \
--long all,codename,description,help,id,release,short,version,program_version \
-n 'lsb_release' \
-- "$@")
else # No, advanced args NOT allowed
# convert (if needed) the enhanced options into basic ones
MYARGS=$(echo "$@" | sed -e "/--/s/-\(-[[:alnum:]]\)[[:alnum:]]*/\1/g")
OPT=$(getopt -o acdhirsvp \
OPT=$($GETOPT_CMD -o acdhirsvp \
-n 'lsb_release' \
-- "$MYARGS")
fi
Expand Down
3 changes: 2 additions & 1 deletion src/storage.sh
Expand Up @@ -44,9 +44,10 @@ __INTERNAL_STORAGE_DEFAULT_SECTION="GENERIC"
__INTERNAL_STORAGE_DEFAULT_NAMESPACE="GENERIC"

__INTERNAL_ST_OPTION_PARSER='
declare GETOPT_CMD="getopt"
local namespace="$__INTERNAL_STORAGE_DEFAULT_NAMESPACE"
local section="$__INTERNAL_STORAGE_DEFAULT_SECTION"
local GETOPT=$(getopt -o : -l namespace:,section: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)) || return 126
local GETOPT=$($GETOPT_CMD -o : -l namespace:,section: -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done)) || return 126
eval set -- "$GETOPT"
while true; do
case $1 in
Expand Down
15 changes: 10 additions & 5 deletions src/synchronisation.sh
Expand Up @@ -24,11 +24,16 @@
# Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Command aliases for compatibilities
declare GETOPT_CMD="getopt"

echo "${__INTERNAL_SOURCED}" | grep -qF -- " ${BASH_SOURCE} " && return || __INTERNAL_SOURCED+=" ${BASH_SOURCE} "

getopt -T || ret=$?
$GETOPT_CMD -T || ret=$?
if [ ${ret:-0} -ne 4 ]; then
echo "ERROR: Non enhanced getopt version detected" 1>&2
echo "getopt command used: $GETOPT_CMD" 1>&2
exit 1
fi

Expand Down Expand Up @@ -118,7 +123,7 @@ __INTERNAL_wait_for_cmd() {
shift 1

# that is the GNU extended getopt syntax!
local TEMP=$(getopt -o t:p:m:d:r: -n '$routine_name' -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
local TEMP=$($GETOPT_CMD -o t:p:m:d:r: -n '$routine_name' -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
if [[ $? != 0 ]] ; then
rlLogError "$routine_name: Can't parse command options, terminating..."
return 127
Expand Down Expand Up @@ -348,7 +353,7 @@ rlWaitForFile() {
local file=""

# that is the GNU extended getopt syntax!
local TEMP=$(getopt -o t:p:d: -n 'rlWaitForFile' -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
local TEMP=$($GETOPT_CMD -o t:p:d: -n 'rlWaitForFile' -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
if [[ $? != 0 ]] ; then
rlLogError "rlWaitForSocket: Can't parse command options, terminating..."
return 127
Expand Down Expand Up @@ -440,7 +445,7 @@ rlWaitForSocket(){
local remote=false

# that is the GNU extended getopt syntax!
local TEMP=$(getopt -o t:p:d: --longoptions close,remote -n 'rlWaitForSocket' -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
local TEMP=$($GETOPT_CMD -o t:p:d: --longoptions close,remote -n 'rlWaitForSocket' -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
if [[ $? != 0 ]] ; then
rlLogError "rlWaitForSocket: Can't parse command options, terminating..."
return 127
Expand Down Expand Up @@ -531,7 +536,7 @@ Signal used to kill the process, optional SIGTERM by default.
#'
rlWait() {
# that is the GNU extended getopt syntax!
local TEMP=$(getopt -o t:s: -n 'rlWait' -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
local TEMP=$($GETOPT_CMD -o t:s: -n 'rlWait' -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
if [[ $? != 0 ]]; then
rlLogError "rlWait: Can't parse command options, terminating..."
return 128
Expand Down
5 changes: 4 additions & 1 deletion src/testing.sh
@@ -1,3 +1,6 @@
# Command aliases for compatibilities
declare GETOPT_CMD="getopt"

echo "${__INTERNAL_SOURCED}" | grep -qF -- " ${BASH_SOURCE} " && return || __INTERNAL_SOURCED+=" ${BASH_SOURCE} "
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
Expand Down Expand Up @@ -759,7 +762,7 @@ B<Warning:> using C<unbuffer> tool is now disabled because of bug 547686.
#'

rlRun() {
local __INTERNAL_rlRun_GETOPT=$(getopt -o lcts -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
local __INTERNAL_rlRun_GETOPT=$($GETOPT_CMD -o lcts -- "$@" 2> >(while read -r line; do rlLogError "$FUNCNAME: $line"; done))
eval set -- "$__INTERNAL_rlRun_GETOPT"

local __INTERNAL_rlRun_DO_LOG=false
Expand Down

0 comments on commit a4825d3

Please sign in to comment.