| William Payne 2004-09-02, 6:51 pm |
| Hello, consider this code snippet written in C++:
const int id_input_file_option = 1;
const int id_output_file_option = 2;
const option long_options[] =
{
{"input-file", 1, NULL, id_input_file_option},
{"output-file", 1, NULL, id_output_file_option},
{NULL, 0, NULL, 0}
};
const char* const short_options = "";
int c = 0;
bool input_file_option_found = false;
bool output_file_option_found = false;
while((c = getopt_long(argc,
argv,
short_options,
long_options,
NULL)) != -1)
{
if(c == id_input_file_option)
{
input_file_option_found = true;
input_file = optarg;
}
else if(c == id_output_file_option)
{
output_file_option_found = true;
output_file = optarg;
}
else if(c == '?')
{
throw runtime_error("Unknown option encountered.");
}
else /* Not reached? */
{
throw runtime_error("Unknown error in getopt_long().");
}
}
As you can see in my options-array, has_arg equals 1 for the "output file"
option, meaning that that option requires a value. I thought that meant that
I had to do
--output-file=something or I would get a runtime error, but it turns out
that getopt_long accepts --output-file=. So I have to do a strlen on optarg
to make sure a value really was specified after the = ?
/ WP
|