LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-27-2003, 11:04 PM   #1
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 30
Using getopt in cplus


// getopt_long.cpp
// Using getopt_long

#include <iostream>
#include <cstdlib>
#include <getopt.h>
using namespace std;

const char* program_name;

void print_usage(ostream& os, int exit_code) {
os << "Usage : %s options [ inputfile ... ]\n" << program_name << endl;
os << " -h --help Display this usage information." << endl
<< " -o --output filename Write output to file." << endl
<< " -v --verbose Print verbose messages." << endl;
exit (exit_code);
}

int main(int argc, char** argv) {
int next_option;

const char* const short_options = "ho:v";

const struct option long_options[] = {
{ "help", 0, NULL, "h" },
{ "output", 1, NULL, "o" },
{ "verbose", 0, NULL, "v" },
{ NULL, 0, NULL, 0 }
};

const char* output_filename = NULL;

int verbose = 0;

program_name = argv[0];

do {
next_option = getopt_long (argc,argv,short_options,long_options,NULL);

switch (next_option) {
case 'h':
print_usage(cout, 0);

case 'o':
output_filename = optarg;
break;

case 'v':
verbose = 1;
break;

case '?':
print_usage (cerr, 1);

case -1:
break;

default:
abort();
}
} while (next_option != -1);

if (verbose) {
for (int i = optind; i < argc; i++)
cout << "Argument : " << argv[i] << endl;
}

return 0;
}

I got a series of error messages , which I shall post here.
They are...

$ g++ -g getopt_long.cpp -Wall
getopt_long.cpp: In function `int main(int, char**)':
getopt_long.cpp:29: invalid conversion from `const char*' to `int'
getopt_long.cpp:29: invalid conversion from `const char*' to `int'
getopt_long.cpp:29: invalid conversion from `const char*' to `int'

Compilation exited abnormally with code 1 at Thu Aug 28 09:32:27

I don't know what are the messages.
I tried the same code with c, and it compiled with a series of warnings. I got the code form Advanced Linux Programming, and I want to use the getopt in a C++ program of mine.

Thx for any help.
 
Old 08-28-2003, 04:54 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,261

Rep: Reputation: 45
your problem lies here, corrections in red.
Code:
const struct option long_options[] = {
    { "help", 0, NULL, 'h' },
    { "output", 1, NULL, 'o' },
    { "verbose", 0, NULL, 'v' },
    { NULL, 0, NULL, 0 }
};
if you want a character then enclose it in ' not in ", double quotes produce null terminated strings. also please use code tags, makes the code a lot easier to read

Last edited by kev82; 08-28-2003 at 04:57 AM.
 
Old 08-28-2003, 05:29 AM   #3
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Original Poster
Rep: Reputation: 30
Thank you very very much, kev82. I did not notice it and was very frustrated with this error.

Ok, what do you mean by code tags?

I use emacs, and it automatically formats the code, and when I cut and pasted it into konqueror, the format was gone. Is this what you were referring to?
 
Old 08-28-2003, 05:35 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,261

Rep: Reputation: 45
i meant on the forum, you can use different tags to keep the code formatted correctly, instead of losing your formatting, makes it much easier for everyone here to read. http://www.linuxquestions.org/questi...&action=bbcode
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getopt availability Bostonian Programming 2 12-26-2004 10:37 AM
getopt in unix hari_s_82 Linux - Newbie 2 10-01-2004 04:19 AM
Help for perl--- Getopt::Declare arobinson74 Programming 1 08-20-2004 10:31 AM
getopt(3) Berhanie Programming 8 06-15-2004 09:50 PM
Getopt() newbie... AMMullan Programming 6 02-11-2004 01:02 PM


All times are GMT -5. The time now is 05:44 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration