From 439a07d7a3dcf6742956c94c1bbc8c3cc89361e6 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 1 Jul 2024 15:36:57 +0200 Subject: [PATCH] fix(stm32CubeProg): use BSD getopt On MacOS getopt is the BSD based one while other using the gnu-based getopt. Then "-o" and long options are not supported Fixes #99 Signed-off-by: Frederic Pillon --- stm32CubeProg.sh | 56 +++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/stm32CubeProg.sh b/stm32CubeProg.sh index 403d1a425..88b1eeee4 100644 --- a/stm32CubeProg.sh +++ b/stm32CubeProg.sh @@ -23,23 +23,23 @@ usage() { echo "Usage: $(basename "$0") [OPTIONS]... Mandatory options: - -i, --interface <'swd'/'dfu'/'serial'> interface identifier: 'swd', 'dfu' or 'serial' - -f, --file file path to be downloaded: bin or hex + -i <'swd'/'dfu'/'serial'> interface identifier: 'swd', 'dfu' or 'serial' + -f file path to be downloaded: bin or hex Optional options: - -e, --erase erase all sectors before flashing - -o, --offset offset from flash base ($ADDRESS) where flashing should start + -e + -o offset from flash base ($ADDRESS) where flashing should start Specific options for Serial protocol: Mandatory: - -c, --com serial identifier, ex: COM1 or /dev/ttyS0,... + -c serial identifier, ex: COM1 or /dev/ttyS0,... Optional: - -r, --rts polarity of RTS signal ('low' by default) - -d, --dtr polarity of DTR signal + -r polarity of RTS signal ('low' by default) + -d polarity of DTR signal Specific options for DFU protocol: Mandatory: - -v, --vid vendor id, ex: 0x0483 - -p, --pid product id, ex: 0xdf11 + -v vendor id, ex: 0x0483 + -p product id, ex: 0xdf11 " >&2 exit "$1" @@ -55,7 +55,7 @@ aborting() { # parse command line arguments # options may be followed by one colon to indicate they have a required arg -if ! options=$(getopt -a -o hi:ef:o:c:r:d:v:p: --long help,interface:,erase,file:,offset:,com:,rts:,dtr:,vid:,pid: -- "$@"); then +if ! options=$(getopt hi:ef:o:c:r:d:v:p: "$@"); then echo "Terminating..." >&2 exit 1 fi @@ -64,44 +64,56 @@ eval set -- "$options" while true; do case "$1" in - -h | --help | -\?) + -h | -\?) usage 0 ;; - -i | --interface) + -i) INTERFACE=$(echo "$2" | tr '[:upper:]' '[:lower:]') echo "Selected interface: $INTERFACE" shift 2 ;; - -e | --erase) + -e) ERASE="--erase all" shift 1 ;; - -f | --file) + -f) FILEPATH=$2 shift 2 + # Need to check if next arg start with '-' else + # it is probably a path including one or more space + # Since getopt is used space is not protected + while true; do + case $1 in + -*) + break + ;; + esac + FILEPATH="$FILEPATH $1" + shift 1 + done ;; - -o | --offset) + -o) OFFSET=$2 ADDRESS=$(printf "0x%x" $((ADDRESS + OFFSET))) shift 2 ;; - -c | --com) + -c) PORT=$2 shift 2 ;; - -r | --rts) + -r) RTS=$(echo "rts=$2" | tr '[:upper:]' '[:lower:]') shift 2 ;; - -d | --dtr) + -d) DTR=$(echo "dtr=$2" | tr '[:upper:]' '[:lower:]') shift 2 ;; - -v | --vid) + -v) VID=$2 shift 2 ;; - -p | --pid) + -p) PID=$2 shift 2 ;; @@ -109,6 +121,10 @@ while true; do shift break ;; + *) + echo "Unknown option $1" + usage 1 + ;; esac done # Check STM32CubeProgrammer cli availability, fallback to dfu-util if protocol dfu