which C++ library is used to get the options user pass for program
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 717
Rep:
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
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++.
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.
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
#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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.