Skip to main content
added 32 characters in body
Source Link
  1. shebang should be replaced with #!/bin/bash

    shebang should be replaced with #!/bin/bash
  2. "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
  1. shebang should be replaced with #!/bin/bash

  2. "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

  1. shebang should be replaced with #!/bin/bash
  2. "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
Source Link

This is a script that can accept -ab or -ba as well.

It's written for Zsh. For Bash,

  1. shebang should be replaced with #!/bin/bash

  2. "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

The result:

% ./function_param_test.sh
TEST 1
flag_a: true
flag_b: false
args[1]: arg1

TEST 2
flag_a: true
flag_b: true
args[1]: arg1

TEST 3
flag_a: true
flag_b: true
args[1]: arg1

TEST 4
flag_a: false
flag_b: false
args[1]: arg1

TEST 5
Invalid option: -x

TEST 6
Usage: fct_test <command> -[a|b] or fct_test -[a|b] <command>