Search code examples
c++command-line-interfacegetoptgetopt-long

How do I make getopt in C++ do option checking strictly?


I am using getopt to parse inputs for a CLI written in C++. I have long and short options and my struct long_options[] element looks like this:

{"verbose", no_argument, NULL, "v"}

One observation is - on the command line, even if I pass

# mycommand --verb

it still accepts that and routes to the function that handles the verbose behavior. Is there any way to make getopt do a strict option check? It shouldn't accept --verb as --verbose right?


Solution

  • According to the manual[1] [2] and the source[3] there is no way to turn off matching abbreviated long options.

    Your options are to accept this behavior (which has been around for decades and is unlikely to surprise users) or to look for another option parser library that allow turning off long option abbreviations.