getopt_long() problem with missing value for option
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > getopt_long() problem with missing value for option




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    getopt_long() problem with missing value for option  
William Payne


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-02-04 11: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







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:00 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register