@@ -3,7 +3,7 @@ set -o nounset # Treat unset variables as an error
33# set -o xtrace # Print command traces before executing command.
44
55UNAME_OS=" $( uname -s) "
6- GNU_GETOPT=
6+ GNU_GETOPT=" n " # "y" if GNU getopt is available
77STM32CP_CLI=
88INTERFACE=
99PORT=
@@ -73,6 +73,7 @@ aborting() {
7373case " ${UNAME_OS} " in
7474 Linux* )
7575 STM32CP_CLI=STM32_Programmer.sh
76+ GNU_GETOPT=" y"
7677 if ! command -v $STM32CP_CLI > /dev/null 2>&1 ; then
7778 export PATH=" $HOME /STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin" :" $PATH "
7879 fi
@@ -91,19 +92,50 @@ case "${UNAME_OS}" in
9192 if ! command -v $STM32CP_CLI > /dev/null 2>&1 ; then
9293 aborting
9394 fi
94- if ! command -v /usr/local/opt/gnu-getopt/bin/getopt > /dev/null 2>&1 ; then
95- if ! command -v /opt/homebrew/opt/gnu-getopt/bin/getopt > /dev/null 2>&1 ; then
96- echo " Warning: long options not supported due to getopt from FreeBSD usage."
97- GNU_GETOPT=n
98- else
99- export PATH=" /opt/homebrew/opt/gnu-getopt/bin" :" $PATH "
95+ # Manage getopt
96+ # Check if getopt is in the path and if it is gnu or BSD version in the path
97+ if command -v getopt > /dev/null 2>&1 ; then
98+ getopt --test > /dev/null 2>&1
99+ if [ $? -eq 4 ]; then
100+ GNU_GETOPT=" y"
101+ fi
102+ fi
103+ # If getopt is not gnu version, check if it is installed via brew
104+ if [ " ${GNU_GETOPT} " != " y" ]; then
105+ if command -v brew > /dev/null 2>&1 ; then
106+ BREW_PREFIX=$( brew --prefix)
107+ if command -v " ${BREW_PREFIX} /bin/getopt" > /dev/null 2>&1 ; then
108+ export PATH=" ${BREW_PREFIX} /bin" :" $PATH "
109+ GNU_GETOPT=" y"
110+ elif command -v " ${BREW_PREFIX} /opt/gnu-getopt/bin/getopt" > /dev/null 2>&1 ; then
111+ export PATH=" ${BREW_PREFIX} /opt/gnu-getopt/bin" :" $PATH "
112+ GNU_GETOPT=" y"
113+ fi
114+ fi
115+ # Check for MacPorts getopt
116+ if [ " ${GNU_GETOPT} " != " y" ]; then
117+ if command -v /opt/local/bin/getopt > /dev/null 2>&1 ; then
118+ export PATH=" /opt/local/bin:$PATH "
119+ GNU_GETOPT=" y"
120+ fi
121+ fi
122+ # Fallback to previous check
123+ if [ " ${GNU_GETOPT} " != " y" ]; then
124+ if ! command -v /usr/local/opt/gnu-getopt/bin/getopt > /dev/null 2>&1 ; then
125+ if command -v /opt/homebrew/opt/gnu-getopt/bin/getopt > /dev/null 2>&1 ; then
126+ export PATH=" /opt/homebrew/opt/gnu-getopt/bin" :" $PATH "
127+ GNU_GETOPT=" y"
128+ fi
129+ else
130+ export PATH=" /usr/local/opt/gnu-getopt/bin" :" $PATH "
131+ GNU_GETOPT=" y"
132+ fi
100133 fi
101- else
102- export PATH=" /usr/local/opt/gnu-getopt/bin" :" $PATH "
103134 fi
104135 ;;
105136 Windows* )
106137 STM32CP_CLI=STM32_Programmer_CLI.exe
138+ GNU_GETOPT=" y"
107139 if ! command -v $STM32CP_CLI > /dev/null 2>&1 ; then
108140 if [ -n " ${PROGRAMFILES+x} " ]; then
109141 STM32CP86=${PROGRAMFILES} /STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
@@ -126,13 +158,15 @@ esac
126158
127159# parse command line arguments
128160# options may be followed by one colon to indicate they have a required arg
129- if [ -n " ${GNU_GETOPT} " ]; then
130- if ! options=$( getopt hi:b:a:es:o:f:m:n:c:d:p:r:v: " $@ " ) ; then
161+ if [ " ${GNU_GETOPT} " = " y " ]; then
162+ if ! options=$( getopt -a -o hi:b:a:es:o:f:m:n:c:d:p:r:v: --long help,interface:,bin:,address:,erase,start:,offset:,freq:,mode:,snum:,com:,dtr:,parity:,rts:,pid:,vid: -- " $@ " ) ; then
131163 echo " Terminating..." >&2
132164 exit 1
133165 fi
134166else
135- if ! options=$( getopt -a -o hi:b:a:es:o:f:m:n:c:d:p:r:v: --long help,interface:,bin:,address:,erase,start:,offset:,freq:,mode:,snum:,com:,dtr:,parity:,rts:,pid:,vid: -- " $@ " ) ; then
167+ echo " Warning: long options not supported due to getopt from FreeBSD usage."
168+ echo " Space in arguments is not supported."
169+ if ! options=$( getopt hi:b:a:es:o:f:m:n:c:d:p:r:v: " $@ " ) ; then
136170 echo " Terminating..." >&2
137171 exit 1
138172 fi
0 commit comments