@@ -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=
@@ -69,7 +69,71 @@ aborting() {
6969 exit 1
7070}
7171
72- # Check STM32CubeProgrammer cli availability and getopt version
72+ check_getopt () {
73+ case " ${UNAME_OS} " in
74+ Linux* | Windows* )
75+ GNU_GETOPT=" y"
76+ ;;
77+ Darwin* )
78+ # Check if getopt is in the path and if it is gnu or BSD version in the path
79+ if command -v getopt > /dev/null 2>&1 ; then
80+ getopt --test > /dev/null 2>&1
81+ if [ $? -eq 4 ]; then
82+ GNU_GETOPT=" y"
83+ fi
84+ fi
85+ # If getopt is not gnu version, check if it is installed via brew
86+ if [ " ${GNU_GETOPT} " != " y" ]; then
87+ if command -v brew > /dev/null 2>&1 ; then
88+ BREW_PREFIX=$( brew --prefix)
89+ if command -v " ${BREW_PREFIX} /bin/getopt" > /dev/null 2>&1 ; then
90+ " ${BREW_PREFIX} " /bin/getopt --test > /dev/null 2>&1
91+ if [ $? -eq 4 ]; then
92+ export PATH=" ${BREW_PREFIX} /bin:$PATH "
93+ GNU_GETOPT=" y"
94+ fi
95+ fi
96+ if [ " ${GNU_GETOPT} " != " y" ]; then
97+ if command -v " ${BREW_PREFIX} /opt/gnu-getopt/bin/getopt" > /dev/null 2>&1 ; then
98+ export PATH=" ${BREW_PREFIX} /opt/gnu-getopt/bin" :" $PATH "
99+ GNU_GETOPT=" y"
100+ fi
101+ fi
102+ fi
103+ # Check for MacPorts getopt
104+ if [ " ${GNU_GETOPT} " != " y" ]; then
105+ if command -v /opt/local/bin/getopt > /dev/null 2>&1 ; then
106+ /opt/local/bin/getopt --test > /dev/null 2>&1
107+ if [ $? -eq 4 ]; then
108+ export PATH=" /opt/local/bin:$PATH "
109+ GNU_GETOPT=" y"
110+ fi
111+ fi
112+ fi
113+ # Fallback to previous check
114+ if [ " ${GNU_GETOPT} " != " y" ]; then
115+ if ! command -v /usr/local/opt/gnu-getopt/bin/getopt > /dev/null 2>&1 ; then
116+ if command -v /opt/homebrew/opt/gnu-getopt/bin/getopt > /dev/null 2>&1 ; then
117+ export PATH=" /opt/homebrew/opt/gnu-getopt/bin" :" $PATH "
118+ GNU_GETOPT=" y"
119+ fi
120+ else
121+ export PATH=" /usr/local/opt/gnu-getopt/bin" :" $PATH "
122+ GNU_GETOPT=" y"
123+ fi
124+ fi
125+ fi
126+ ;;
127+ * )
128+ echo " Unknown host OS: ${UNAME_OS} ." >&2
129+ exit 1
130+ ;;
131+ esac
132+ }
133+
134+ check_getopt
135+
136+ # Check STM32CubeProgrammer cli availability
73137case " ${UNAME_OS} " in
74138 Linux* )
75139 STM32CP_CLI=STM32_Programmer.sh
@@ -91,16 +155,6 @@ case "${UNAME_OS}" in
91155 if ! command -v $STM32CP_CLI > /dev/null 2>&1 ; then
92156 aborting
93157 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 "
100- fi
101- else
102- export PATH=" /usr/local/opt/gnu-getopt/bin" :" $PATH "
103- fi
104158 ;;
105159 Windows* )
106160 STM32CP_CLI=STM32_Programmer_CLI.exe
@@ -126,13 +180,15 @@ esac
126180
127181# parse command line arguments
128182# 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
183+ if [ " ${GNU_GETOPT} " = " y " ]; then
184+ 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
131185 echo " Terminating..." >&2
132186 exit 1
133187 fi
134188else
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
189+ echo " Warning: long options not supported due to getopt from FreeBSD usage."
190+ echo " Space in arguments is not supported."
191+ if ! options=$( getopt hi:b:a:es:o:f:m:n:c:d:p:r:v: " $@ " ) ; then
136192 echo " Terminating..." >&2
137193 exit 1
138194 fi
0 commit comments