I am working on windows, so I had to compile getopt and getopt_long from this excellent source
I modified getopt_long.c (below) to accommodate two input arguments. I was not worried about a more general case with several arguments, as this would require more (and cleaner) refinement than I had the time / need. The second argument is placed in another global "optarg2".
If you don't need to compile getopt from the source code, Frank's answer above looks more elegant.
extern char * optarg2 . . . int getopt_long(nargc, nargv, options, long_options, index) { . . . if (long_options[match].has_arg == required_argument || long_options[match].has_arg == optional_argument || long_options[match].has_arg == two_req_arguments) { if (has_equal) optarg = has_equal; else optarg = nargv[optind++]; if (long_options[match].has_arg == two_req_arguments) { optarg2 = nargv[optind++]; } } if ((long_options[match].has_arg == required_argument || long_options[match].has_arg == two_req_arguments) && (optarg == NULL)) { if ((opterr) && (*options != ':')) (void)fprintf(stderr, "%s: option requires an argument -- %s\n", __progname(nargv[0]), current_argv); return (BADARG); } if ((long_options[match].has_arg == two_req_arguments) && (optarg2 == NULL)) { if ((opterr) && (*options != ':')) (void)fprintf(stderr, "%s: option requires 2nd argument -- %s\n", __progname(nargv[0]), current_argv); return (BADARG); }
You also need to add a definition to getopt.h for "two_required_args" or "multiple_args" as you see fit.
edit: I'm bad at markdown
source share