From b37a9516d063ed985898b620c5f38f09f3183434 Mon Sep 17 00:00:00 2001 From: hongxuww Date: Thu, 16 Oct 2025 10:02:53 +0000 Subject: [PATCH] fix: correct argument parsing in create-new-feature.sh script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix two critical bugs in the argument parsing logic that caused incorrect behavior when --short-name parameter was used: 1. **Index offset bug**: The loop started at i=0 and used i < $#, which incorrectly processed $0 (script name) as the first argument and skipped the last actual parameter. Changed to i=1 and i <= $# to properly iterate through actual command-line arguments ($1 to $#). 2. **Boundary condition bug**: The condition `[ $((i + 1)) -ge $# ]` incorrectly flagged valid arguments as missing. When --short-name was at position $#-1, the next position $# was valid but treated as out-of-bounds. Changed to `[ $((i + 1)) -gt $# ]` for correct validation. 3. **Enhanced validation**: Added check to ensure --short-name value is not another option (doesn't start with --). **Before**: - `script --json "desc" --short-name "test"` → Error: requires a value - `script --json "desc1" "desc2" --short-name` → Generated wrong branch name **After**: - `script --json "desc" --short-name "test"` → Works correctly - `script --json "desc1" "desc2" --short-name` → Proper error message This ensures the script correctly supports both parameter orders: - `[--json] [--short-name ] ` - `[--json] [--short-name ]` --- scripts/bash/create-new-feature.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/scripts/bash/create-new-feature.sh b/scripts/bash/create-new-feature.sh index 510f6204..01bdf915 100644 --- a/scripts/bash/create-new-feature.sh +++ b/scripts/bash/create-new-feature.sh @@ -5,20 +5,26 @@ set -e JSON_MODE=false SHORT_NAME="" ARGS=() -i=0 -while [ $i -lt $# ]; do +i=1 +while [ $i -le $# ]; do arg="${!i}" case "$arg" in --json) JSON_MODE=true ;; --short-name) - if [ $((i + 1)) -ge $# ]; then + if [ $((i + 1)) -gt $# ]; then echo 'Error: --short-name requires a value' >&2 exit 1 fi i=$((i + 1)) - SHORT_NAME="${!i}" + next_arg="${!i}" + # Check if the next argument is another option (starts with --) + if [[ "$next_arg" == --* ]]; then + echo 'Error: --short-name requires a value' >&2 + exit 1 + fi + SHORT_NAME="$next_arg" ;; --help|-h) echo "Usage: $0 [--json] [--short-name ] "