Search code examples
cgetopt

making getopt() process argv[0]


I trying to emulate main() function like behavior for normal functions using string tokenizing and storing tokens in a NULL terminated char* array.

Every thing is fine except getopt(). It won't rearrange argv[0] coz it expects the first argument to be program name. But for my function the argv[0] isn't the program name. I want to make getopt() to also rearrange argv[0](non-option). How do I do that?


Solution

  • Try using

    c = getopt(argc + 1, argv - 1, "xyz")
    

    Edit: Which, as pointed out below, is a hack But I'd be interested to see a machine on which it didn't work.