getopts doesn't execute some flags

64 views Asked by At

I am writing a zsh script to make easier the use of the git clone command. The -h and -s flags works fine, but the -d and -q flags don't. I tried to see if they worked by adding echo $dir and echo $quiet, but I've got nothing in result (they were empty).

I wanted to use the flags and the out-of-the-box method. This is the code I wrote:

#!/usr/bin/zsh

# Git clone
function clone() {
    usage=("Usage: $0 [-h http] [-s ssh] [-d dir] -q

Options:
    -h  Use the http protocol to clone the git repo (https://github.com/<your repository>)
    -s  Use ssh to clone the git repo ([email protected]:<your repository>)
    -d  Destination Directory
    -q  Quiet clone")
    githttp=(git clone https://github.com/)
    gitssh=(git clone [email protected]:)
    while getopts ":h:s:d:q:" opt; do
        case "$opt" in
            h) http=$OPTARG
               $quiet $githttp$http $dir
               return $?
               ;;
            s) ssh=$OPTARG
               $quiet $gitssh$ssh $dir
               return $?
               ;;
            d) dir=$OPTARG 
               ;;
            q) quiet=(gum spin)
               ;;
            *) echo "Invalid option"
               echo $usage
               return 1
               ;;
        esac
    done

    method=$(gum choose "http" "ssh")
    case "$method" in
        http) http=$(gum input --prompt=" " --placeholder="Type url here")
              dir=$(gum input --prompt=" " --placeholder="Type here the directory. Leave blank for current")
              if [[ -z $http || -z $dir ]]; then
                echo "Exiting..."
                return 1
              else
                $githttp$http $dir
              fi
              ;;
        ssh)  ssh=$(gum input --prompt=" " --placeholder="")
              dir=$(gum input --prompt=" " --placeholder="Type here the directory. Leave blank for current")
              if [[ -z $ssh || -z $dir ]]; then
                echo "Exiting..."
                return 1
              else
                $gitssh$ssh $dir
              fi
              ;;
        *)    echo "Exiting..."
              return 1
              ;;
    esac
}

How can I fix this problem? Thanks.

0

There are 0 answers