-1

I wrote a bash script to help me with the build process of couple web apps. There are different build modes so i tried implementing this using flags (getopts)


    dev=false;
    clean_dest=false;
    all=false;
    
    while getopts c:clean-dest:a:all:d:dev:t:target: flags
    do
        case "${flags}" in
            d) dev=true;;
            dev) dev=true;;
            c) clean_dest=true;;
            clean-dest) clean_dest=true;;
            a) all=true;;
            all) all=true;;
            t) target=${OPTARG};;
            target) target=${OPTARG};;
        esac
    done

If I call it with -d -t "someTarget" it works but when I call it with -c -a -d -t "someTarget" it doesn't take into account '-c' and '-a'.

If I swap around the order of arguments in the while another flag combinations breaks.

What am I doing wrong?

I am thankful in advance.

fmi21
  • 485
  • 3
  • 15
  • 3
    The colon in `c:` means that `-c` takes an argument. So `-c -a` means that the flag is `-c` and its argument is `-a`. – Barmar Oct 15 '21 at 17:43
  • 5
    BTW, `getopts` doesn't support multi-character options like `-target`. – Barmar Oct 15 '21 at 17:43

1 Answers1

0

Solved by adhering to getopts's syntax; I had to change the while line to while getopts cadt: flags.

fmi21
  • 485
  • 3
  • 15