Search code examples
c++clinuxgetopt

How to ignore invalid options in getopt_long


I am using function getopt_long to get command line options. I want to ignore error when a invalid option is given. Currently it printing error to stderr like:

 invalid option -- 's'

Solution

  • There's variable opterr in unistd.h which will avoid printing the the error to stderr if you set it to 0.

    Just set it to 0:

    #include <unistd.h>
    
    opterr = 0;