Opened 4 years ago

Closed 4 years ago

#5383 closed bug (wontfix)

GetOpt parser too strict

Reported by: mcandre Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 7.0.4
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

For GetOpt, some CLI arguments must be right next to the flag, and some do not.

Relevant scripts: http://www.delicious.com/mcandre/getoptfu

The command specified two bananas, but the 2 isn't parsed correctly.

$ ./groceries.hs -p -j -b 2
FoodCo welcomes you, Mr. Derp.
YOU BOUGHT PEANUT BUTTER!!!
YOU BOUGHT JELLY!!!
YOU BOUGHT 1 BANANAS!!!
Mr. Derp bought 3 items.
Thank you, come again soon.

When 2 is specified right after -b, it is parsed correctly. This is against most CLI formats. Most CLI users expect a space between flags and values.

$ ./groceries.hs -p -j -b2
FoodCo welcomes you, Mr. Derp.
YOU BOUGHT PEANUT BUTTER!!!
YOU BOUGHT JELLY!!!
YOU BOUGHT 2 BANANAS!!!
Mr. Derp bought 4 items.
Thank you, come again soon.

Furthermore, GetOpt is inconsistent: it does not need a space between flags and string values--it parses them either way.

$ ./groceries.hs -s Moops
Moops welcomes you, Mr. Derp.
Mr. Derp bought 0 items.
Thank you, come again soon.
$ ./groceries.hs -sMoops
Moops welcomes you, Mr. Derp.
Mr. Derp bought 0 items.
Thank you, come again soon.

Change History (1)

comment:1 Changed 4 years ago by igloo

  • Component changed from libraries/unix to libraries/base
  • Resolution set to wontfix
  • Status changed from new to closed

This matches the GNU getopt behaviour with optional arguments:

$ cat z.c

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main (int argc, char **argv) {
  int c;

  while ((c = getopt (argc, argv, "ab:c::")) != -1) {
    switch (c) {
        case 'a':
            printf("Got a: %p\n", optarg);
            break;
        case 'b':
            printf("Got b: %p\n", optarg);
            break;
        case 'c':
            printf("Got c: %p\n", optarg);
            break;
        default:
            printf("Got something else\n");
        }
    }

    return 0;
}

$ gcc z.c -o z   
$ ./z -a 2       
Got a: (nil)
$ ./z -b 2
Got b: 0x7fff1fed24a9
$ ./z -c 2
Got c: (nil)
$ ./z -b2 
Got b: 0x7fff2e0a24a9
$ ./z -c2
Got c: 0x7fff4016d4a9

The difference is ReqArg vs OptArg, not String vs Int (both are String as far as GetOpt is concerned).

Note: See TracTickets for help on using tickets.