Skip to content

Commit b37a951

Browse files
committed
fix: correct argument parsing in create-new-feature.sh script
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 <name>] <feature_description>` - `[--json] <feature_description> [--short-name <name>]`
1 parent 3b000fc commit b37a951

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

scripts/bash/create-new-feature.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@ set -e
55
JSON_MODE=false
66
SHORT_NAME=""
77
ARGS=()
8-
i=0
9-
while [ $i -lt $# ]; do
8+
i=1
9+
while [ $i -le $# ]; do
1010
arg="${!i}"
1111
case "$arg" in
1212
--json)
1313
JSON_MODE=true
1414
;;
1515
--short-name)
16-
if [ $((i + 1)) -ge $# ]; then
16+
if [ $((i + 1)) -gt $# ]; then
1717
echo 'Error: --short-name requires a value' >&2
1818
exit 1
1919
fi
2020
i=$((i + 1))
21-
SHORT_NAME="${!i}"
21+
next_arg="${!i}"
22+
# Check if the next argument is another option (starts with --)
23+
if [[ "$next_arg" == --* ]]; then
24+
echo 'Error: --short-name requires a value' >&2
25+
exit 1
26+
fi
27+
SHORT_NAME="$next_arg"
2228
;;
2329
--help|-h)
2430
echo "Usage: $0 [--json] [--short-name <name>] <feature_description>"

0 commit comments

Comments
 (0)