@@ -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,47 @@ 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+ if ! getopt --test 2> /dev/null; then
99+ GNU_GETOPT=" y"
100+ fi
101+ fi
102+ # If getopt is not gnu version, check if it is installed via brew or macports and add it to the path
103+ if [ " ${GNU_GETOPT} " != " y" ]; then
104+ if command -v brew > /dev/null 2>&1 ; then
105+ BREW_PREFIX=$( brew --prefix)
106+ # Only check in bin as brew symlinks its files into the 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+ fi
111+ fi
112+ # Check if GNU_GETOPT is set then check for MacPorts getopt
113+ if [ " ${GNU_GETOPT} " != " y" ]; then
114+ if command -v /opt/local/bin/getopt > /dev/null 2>&1 ; then
115+ export PATH=" /opt/local/bin:$PATH "
116+ GNU_GETOPT=" y"
117+ fi
118+ fi
119+ # Fallback to previous check
120+ if [ " ${GNU_GETOPT} " != " y" ]; then
121+ if ! command -v /usr/local/opt/gnu-getopt/bin/getopt > /dev/null 2>&1 ; then
122+ if command -v /opt/homebrew/opt/gnu-getopt/bin/getopt > /dev/null 2>&1 ; then
123+ export PATH=" /opt/homebrew/opt/gnu-getopt/bin" :" $PATH "
124+ GNU_GETOPT=" y"
125+ fi
126+ else
127+ export PATH=" /usr/local/opt/gnu-getopt/bin" :" $PATH "
128+ GNU_GETOPT=" y"
129+ fi
100130 fi
101- else
102- export PATH=" /usr/local/opt/gnu-getopt/bin" :" $PATH "
103131 fi
104132 ;;
105133 Windows* )
106134 STM32CP_CLI=STM32_Programmer_CLI.exe
135+ GNU_GETOPT=" y"
107136 if ! command -v $STM32CP_CLI > /dev/null 2>&1 ; then
108137 if [ -n " ${PROGRAMFILES+x} " ]; then
109138 STM32CP86=${PROGRAMFILES} /STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
@@ -126,13 +155,15 @@ esac
126155
127156# parse command line arguments
128157# 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
158+ if [ " ${GNU_GETOPT} " = " y " ]; then
159+ 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
131160 echo " Terminating..." >&2
132161 exit 1
133162 fi
134163else
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
164+ echo " Warning: long options not supported due to getopt from FreeBSD usage."
165+ echo " Space in arguments is not supported."
166+ if ! options=$( getopt hi:b:a:es:o:f:m:n:c:d:p:r:v: " $@ " ) ; then
136167 echo " Terminating..." >&2
137168 exit 1
138169 fi
0 commit comments