[pacman-dev] [PATCH] Fix command line argument parsing in makepkg

Xavier shiningxc at gmail.com
Wed May 21 19:13:52 EDT 2008

Sebastian Nowicki wrote:
> On 22/05/2008, at 1:13 AM, Xavier wrote:
>> It is not eval, it is set. As far as I can tell, it is perfectly
>> normal,
>> that call seems to be made exactly for that purpose: changing the args
>> ($@, $1, etc). And I get the same behavior here anyway.
> I see. After some playing around it seems that this version of getopt
> simply doesn't support any parameters. Using "getopt abc $@" works
> fine. When running the test script with "-a -b -c", $@ becomes "-a -b -
> c --". If "getopt -o abs $@" is used, $@ becomes "-- abc -a -b -c", in
> which case makepkg would immediately break out of the loop.
>> $ cat test.sh
>> #!/bin/bash
>> echo "\$@=$@"
>> args=$(getopt abc $@)
>> if [ $? != 0 ]; then
>>     echo "Invalid arguments"
>>     exit 1
>> fi
>> eval set -- $args
>> echo "\$@=$@"
>> while true; do
>>     case "$1" in
>>         -a|-b|-c)
>>             echo "Got $1"; shift;;
>>         *)
>>             echo "Invalid: $1"; break;;
>>     esac
>> done
>> $ ./test.sh -a -b -c
>> $@=-a -b -c
>> $@=-a -b -c --
>> Got -a
>> Got -b
>> Got -c
>> Invalid: --
> After changing "getopt abc $@" to "getopt -o abc $@":
>> $ ./test.sh -a -b -c
>> $@=-a -b -c
>> $@=-- abc -a -b -c
> Invalid: --

Because you need to use -- to separate the getopt options from the 
argument, this is all in the manpage actually, I just figured that :)
getopt -o abc -- $@"
That is what makepkg does.

man getopt:

        The  parameters  getopt  is called with can be
        divided into two parts: options  which  modify
        the   way   getopt  will  parse  (options  and
        -o|--options optstring in the  SYNOPSIS),  and
        the parameters which are to be parsed (parame‐
        ters in the SYNOPSIS).  The second  part  will
        start  at  the first non-option parameter that
        is not an option argument, or after the  first
        occurrence of ‘--'.  If no ‘-o' or ‘--options'
        option is found in the first part,  the  first
        parameter  of  the  second part is used as the
        short options string.

        If the environment variable  GETOPT_COMPATIBLE
        is  set,  or  if its first parameter is not an
        option (does not start with a ‘-', this is the
        first  format  in  the  SYNOPSIS), getopt will
        generate output that is compatible  with  that
        of other versions of getopt(1).  It will still
        do parameter shuffling and recognize  optional
        arguments  (see section COMPATIBILITY for more

Actually I am confused. If we want to use that compatible format, getopt 
abc $@, then we can't use long options anymore?

More information about the pacman-dev mailing list