LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 06-29-2005, 01:02 PM   #1
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 717

Rep: Reputation: 33
which C++ library is used to get the options user pass for program


Hi everyone.
I am learning C++ programing.
I go to Advance linux programing website
and download its document.
It tells me that, good gnu linux software
must have the ability to process the options
pass to it in both short form and long form.
It also shows me the way to use GNU C getopt
library to solve this problem.
Nevertheless, I am using C++ and this library
itself recommend it to be use for C only. I 've found
that this getopt_long function did not work too.
It always says that no options passed although I 've
tried many way to pass options.

So Is there any C++ library can solve this problem
 
Old 06-29-2005, 02:19 PM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Erm, int main( int argc, char* argv[] ) ??
 
Old 06-29-2005, 02:55 PM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
i found one named 'anyoption'. i just googled it. it is a set of classes and functions that help do exactly what your talking about. i'm not sure if it was covered by the GPL, so you better make sure.

it's to bad that your asking for a command line option handler in c++. one of the thing i do to help in learning a new language is write a command line option handler. i've written option handlers that can handle long options in C, Perl, Python, Java and a short option handler in FORTRAN.

But i found anyoption and never wrote one for C++.
 
Old 06-29-2005, 02:59 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,408

Rep: Reputation: 108Reputation: 108
getopt_long works for me now and worked many times in the past. The code is usually short. Maybe copy it here so we can see where's problem in it? It's usually not hard at all to fix.
 
Old 06-29-2005, 03:11 PM   #5
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
I dont see why you couldn't use getopts and getopts_long in C++...
 
Old 06-29-2005, 03:48 PM   #6
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
i don't either, i didn't even know about it.
 
Old 07-04-2005, 10:41 AM   #7
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 717

Original Poster
Rep: Reputation: 33
You require the code, here is it

Quote:
Originally posted by Mara
gThe code is usually short. Maybe copy it here so we can see where's problem in it? It's usually not hard at all to fix.
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <getopt.h>
#include <iostream>
#include <solve.h>

using namespace std;

int main(int argc, char *argv[])
{
const char* const short_options = "ht:";
const struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "type", 1, NULL, 't' },
{ NULL, 0, NULL, 0 }
};
int next_options;
short choosen ;

do {

if (getopt_long(argc,argv,short_options,long_options,NULL) == '?') {
print_usage(2,argv[0],false);
cout << endl;
cout << "Wrong arguments";
break;
};
cout << endl;
next_options=getopt_long(argc,argv,short_options,long_options,NULL);
switch (next_options){
case 'h': //Print help message. The print_usage() is declare
//in another file, it print help message the exit with the signal
// is the arguments passed to it

print_usage(EXIT_SUCCESS); break;
case 't': choosen=atoi(optarg); break;
}
} while (next_options != -1);

//My main excute code goes here
};

Getopt_long always return -1 deespite what arguments I passed to my program
 
Old 07-05-2005, 04:56 PM   #8
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,408

Rep: Reputation: 108Reputation: 108
Re: You require the code, here is it

Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <getopt.h>
#include <iostream>
#include <solve.h>

using namespace std;

int main(int argc, char *argv[])
{
	const char* const short_options = "ht:";
	const struct option long_options[] = {
		{ "help", 0, NULL, 'h' },
		{ "type", 1, NULL, 't' },
		{ NULL, 0, NULL, 0 }
	};
	int next_options;
	short choosen ;

	do {

		/* commented out, but in fact you can check (manually) argv[1] here
                if (getopt_long(argc,argv,short_options,long_options,NULL) == '?') { 
			print_usage(2,argv[0],false);
			cout << endl;
			cout << "Wrong arguments";
			break;
		};
		cout << endl;*/
		next_options=getopt_long(argc,argv,short_options,long_options,NULL);
		switch (next_options){
			case 'h': //Print help message. The print_usage() is declare
                                       //in another file, it print help message the exit with the signal
                                      // is the arguments passed to it

				print_usage(EXIT_SUCCESS); break;
			case 't': choosen=atoi(optarg); break;
		}
	} while (next_options != -1);
        /* this is added */
        if(optind < argc){
                 cout<<argv[optind]<<endl;
        }
	
//My main excute code goes here
	};
Now explanation. getopt_long doesn't return a non-option. You don't have ? in short_ptions and long_options, so the condition will never be true. Instead, you can write it as in my version above or before the do loop check if argv[1] is ? and print usage etc.
 
  


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
rpmbuild --with : mysql - unknown option. How to pass extra options to build? fireman949 Red Hat 0 07-06-2005 06:53 PM
Pass SCP User passward in shell program michaelyu33 Programming 3 03-18-2005 12:15 PM
how to pass kernel module options (cx8800, ATI Tv Wonder pro) johnford Linux - Hardware 3 11-16-2004 08:23 PM
pass a parameter to a running program nimra Linux - Software 1 06-29-2004 01:38 AM
Pass Options at boot time garr0323 Linux - Software 1 11-20-2003 02:13 PM


All times are GMT -5. The time now is 06:10 PM.

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