Search code examples
cgetoptunistd.h

Argument Line Option Parsing for C


I've looked at many of the questions asked here before, and I've decided to go with unistd.h for getopt.

However I have a need that a certain option depends on a previous option, as in

exe -a a_opt -b b_bopt

where the -b will do b_opt/a_opt

So it follows that -a must be parsed before -b.

Is there a way to implement this?

Thanks

EDIT: The simple way is to create a flag and check it at the end, however i'm trying to see if there is a more elegant solution.


Solution

  • No there's really no such way to enforce argument order using only getopt.

    What you can do is to set a flag when getopt returns the -a option to you, and check that flag when you get the -b option.