Pergunta

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?

Foi útil?

Solução 2

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.

Outras dicas

getopt(3) uses a global variable optind (option index) to track its progress through argv and initializes it to 1. Try setting optind = 0 before reading the options.

Though the hack @Tom Tanner suggested worked for certain systems, it didn't compile for the target which I'm supposed to make it work. Another work around I found is to replace the first argument in my argv[] array with a dummy string and use getopt() with it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top