shebang should be replaced with
shebang should be replaced with#!/bin/bash#!/bin/bash"args[1]: ${args[1]}"should be replaced with"args[0]: ${args[0]}"#!/bin/zsh
fct_test() { local flag_a=false local flag_b=false
local args=() while (( $# )); do case $1 in -a) flag_a=true ;; -b) flag_b=true ;; -ab|-ba) flag_a=true flag_b=true ;; -*) echo >&2 "Invalid option: $1" return 1 ;; *) args+=("$1") ;; esac shift done # Check if exactly one command is provided if [ ${#args[@]} -ne 1 ]; then echo >&2 "Usage: fct_test <command> -[a|b] or fct_test -[a|b] <command>" return 1 # Return 1 to indicate an error fi echo "flag_a: $flag_a" echo "flag_b: $flag_b" echo "args[1]: ${args[1]}"} echo "TEST 1" fct_test -a "arg1" echo
echo "TEST 2" fct_test "arg1" -ba echo
echo "TEST 3" fct_test -a "arg1" -b echo
echo "TEST 4" fct_test "arg1" echo
echo "TEST 5" fct_test -x "arg1" echo
echo "TEST 6" fct_test "arg1" "arg2" echo
"args[1]: ${args[1]}"should be replaced with"args[0]: ${args[0]}"
The code starts from here:
#!/bin/zsh
fct_test() {
local flag_a=false
local flag_b=false
local args=()
while (( $# )); do
case $1 in
-a) flag_a=true ;;
-b) flag_b=true ;;
-ab|-ba)
flag_a=true
flag_b=true ;;
-*)
echo >&2 "Invalid option: $1"
return 1
;;
*) args+=("$1") ;;
esac
shift
done
# Check if exactly one command is provided
if [ ${#args[@]} -ne 1 ]; then
echo >&2 "Usage: fct_test <command> -[a|b] or fct_test -[a|b] <command>"
return 1 # Return 1 to indicate an error
fi
echo "flag_a: $flag_a"
echo "flag_b: $flag_b"
echo "args[1]: ${args[1]}"
}
echo "TEST 1"
fct_test -a "arg1"
echo
echo "TEST 2"
fct_test "arg1" -ba
echo
echo "TEST 3"
fct_test -a "arg1" -b
echo
echo "TEST 4"
fct_test "arg1"
echo
echo "TEST 5"
fct_test -x "arg1"
echo
echo "TEST 6"
fct_test "arg1" "arg2"
echo