Search code examples
cargumentsgetoptargp

C - how to allow unrecognized options in Argp or Getopt?


I'm writing FUSE filesystem which uses some arguments. I'd like to pass all unrecognized options to FUSE (because it has its own options). Can I do that using argp or getopt? For now both give me "Unknown option" error.


Solution

  • Argp

    From the "Argp Flags" section of the documentation:

    ARGP_NO_ERRS

    Don't print error messages for unknown options to stderr; unless this flag is set, ARGP_PARSE_ARGV0 is ignored, as argv[0] is used as the program name in the error messages. This flag implies ARGP_NO_EXIT. This is based on the assumption that silent exiting upon errors is bad behavior.

    Getopt

    For getopt (as well as getopt_long and getopt_long_only), you simply set the global variable opterr to 0 before calling the function. Alternatively, you can use a short option string with a leading : character as in ":o:v" to handle -o output-file and -v (: will be returned if -o is missing its argument and ? if any option that does not exist in your option string is found).