- Option parsing system is now complete, shame on me, Glib::Option* was so easy to use...

- This is not so important, but the app now segfaults because of this "improvement", don`t say it`s my fault, from what I can understand, glib crashes at some time in between the call to parse_options() is terminated and the call to start_gui() is performed

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@735 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
elvez 2006-07-06 00:05:50 +00:00
parent b6da06e014
commit 3125f3d3cf
1 changed files with 156 additions and 107 deletions

View File

@ -24,142 +24,191 @@
#include "backend/global_preferences.hh" #include "backend/global_preferences.hh"
#include "parse_opts.hh" #include "parse_opts.hh"
#ifdef _GNU_SOURCE //#ifdef _GNU_SOURCE
#include <getopt.h> //#include <getopt.h>
#endif //#endif
#include <cstdio> #include <glibmm/optioncontext.h>
//#include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <unistd.h> //#include <unistd.h>
#include <iostream> #include <iostream>
using namespace sgpem;
// Static declarations: // Static declarations:
static void display_help(); static void display_help();
// Glib-version
//void
//parse_options(int& argc, char**& argv)
//{
// print_license();
//
// // Prepare the option entries
//
// OptionEntry no_gui;
// OptionEntry policies_dir;
// OptionEntry modules_dir;
//
// no_gui.set_short_name("N");
// policies_dir.set_short_name("P");
// modules_dir.set_short_name("M");
//
//#ifdef _GNU_SOURCE
// no_gui.set_long_name("no-gui");
// policies_dir.set_long_name("policies-dir");
// modules_dir.set_long_name("modules_dir");
//#endif
//
// no_gui.set_flags(OptionEntry::FLAG_NO_ARG);
//
// // Places where arguments will be saved
// std::string policies_dir_val;
// std::string modules_dir_val;
//
// // Create the only group
//
// OptionGroup group("options", "options");
//
// group.add_entry(no_gui);
// group.add_entry(policies_dir, policies_dir_val);
// group.add_entry(modules_dir, modules_dir_val);
//
// // Create context
//
// OptionContext context("SGPEMv2, a graphical simulator for process scheduling in a multitasking computer");
//
// context.set_main_group(group);
// context.set_help_enabled(true);
//
// // Parse options
//
// std::auto_ptr<Glib::Error> error(NULL);
// if(!context.parse_opts(argc, argv, error))
// {
// std::cout << "Bad invocation: " << error.what() << std::endl;
//
// const char *force_help[] = { "sgpem", "--help" };
// int count = 2;
//
// assert(context.parse_opts(force_help, count, error));
// }
//
// // FIXME and now ??? I need MORE documentation for this stuff!!!
// }
void void
parse_options(int& argc, char**& argv) parse_options(int& argc, char**& argv)
{ {
using sgpem::GlobalPreferences; // FIXME this code causes a segfault some time after the method is returned (!!!),
// ddd says the problem originates from libglib. Any clue?
using Glib::OptionEntry;
using Glib::OptionContext;
using Glib::OptionGroup;
print_license(); print_license();
static const char* short_options = "NhP:M:"; // Here we try to sort out the order of creation of the Option* objects, but it's useless...
OptionContext context("SGPEMv2, a graphical simulator for process "
"scheduling in a multitasking computer");
OptionGroup group("options", "options");
// Prepare the option entries
OptionEntry no_gui;
OptionEntry policies_dir;
OptionEntry modules_dir;
no_gui.set_short_name('N');
policies_dir.set_short_name('P');
modules_dir.set_short_name('M');
#ifdef _GNU_SOURCE #ifdef _GNU_SOURCE
// Initialize the array for GNU long options no_gui.set_long_name("no-gui");
static struct option long_options[] = policies_dir.set_long_name("policies-dir");
{ modules_dir.set_long_name("modules_dir");
{"no-gui", no_argument, NULL, 'N' },
{"help", no_argument, NULL, 'h' },
{"policies-dir", required_argument, NULL, 'P'},
{"modules-dir", required_argument, NULL, 'M'}
};
int option_index = 0;
#endif #endif
int opt; no_gui.set_description("starts the program in command line mode");
do policies_dir.set_description("adds this directory to the default modules search path");
modules_dir.set_description("adds this directory to default plugin search path");
// Places where araguments will be saved
bool no_gui_enabled = false;
OptionGroup::vecustrings policies_dir_val;
OptionGroup::vecustrings modules_dir_val;
// Create the only group
//OptionGroup group("options", "options");
group.add_entry(no_gui, no_gui_enabled);
group.add_entry(policies_dir, policies_dir_val);
group.add_entry(modules_dir, modules_dir_val);
// Create context
//OptionContext context("SGPEMv2, a graphical simulator for process "
// "scheduling in a multitasking computer");
context.set_main_group(group);
context.set_help_enabled(true);
context.set_ignore_unknown_options(true);
// Parse options
try
{ {
#ifdef _GNU_SOURCE context.parse(argc, argv);
opt = getopt_long(argc, argv, short_options,
long_options, &option_index);
#else
opt = getopt(argc, argv, short_options);
#endif
switch(opt) GlobalPreferences& prefs = GlobalPreferences::get_instance();
for(Glib::OptionGroup::vecustrings::const_iterator it = policies_dir_val.begin();
it != policies_dir_val.end(); ++it)
prefs.add_policies_dir(*it);
for(Glib::OptionGroup::vecustrings::const_iterator it = modules_dir_val.begin();
it != modules_dir_val.end(); ++it)
prefs.add_modules_dir(*it);
if(no_gui_enabled)
{ {
case - 1:
// We have finished normally
break;
case 'N' :
// We don't return to main, instead we // We don't return to main, instead we
// initialize the command line version // initialize the command line version
// of sgpemv2 (?) // of sgpemv2 (?)
// FIXME : to be written! // FIXME : to be written!
break;
case 'P':
GlobalPreferences::get_instance().add_policies_dir(optarg);
break;
case 'M':
GlobalPreferences::get_instance().add_modules_dir(optarg);
break;
case ':':
printf(_("[EE] Wrong number of parameters. Please see \n"
"%s --help\n"), argv[0]);
exit(-1);
case 'h' :
default :
display_help();
} }
} }
while( opt != -1 ); catch(Glib::OptionError e)
{
std::cout << "Bad invocation: " << e.what() << std::endl;
// Set these two to start from additional filenames on the cmdline: //char *force_help_vec[] = { "sgpem", "--help" };
argc -= optind; //char **force_help = &force_help_vec[0];
argv += optind; //int count = 2;
//assert(context.parse(count, force_help));
}
// and now ??? I need MORE documentation for this stuff!!!
// shame on me! it was so easy...
} }
//void
//parse_options(int& argc, char**& argv)
//{
// using sgpem::GlobalPreferences;
//
// print_license();
//
// static const char* short_options = "NhP:M:";
//
//#ifdef _GNU_SOURCE
// // Initialize the array for GNU long options
// static struct option long_options[] =
// {
// {"no-gui", no_argument, NULL, 'N' },
// {"help", no_argument, NULL, 'h' },
// {"policies-dir", required_argument, NULL, 'P'},
// {"modules-dir", required_argument, NULL, 'M'}
// };
// int option_index = 0;
//#endif
//
// int opt;
// do
// {
//#ifdef _GNU_SOURCE
// opt = getopt_long(argc, argv, short_options,
// long_options, &option_index);
//#else
// opt = getopt(argc, argv, short_options);
//#endif
//
// switch(opt)
// {
// case - 1:
// // We have finished normally
// break;
// case 'N' :
// // We don't return to main, instead we
// // initialize the command line version
// // of sgpemv2 (?)
//
// // FIXME : to be written!
// break;
// case 'P':
// GlobalPreferences::get_instance().add_policies_dir(optarg);
// break;
// case 'M':
// GlobalPreferences::get_instance().add_modules_dir(optarg);
// break;
// case ':':
// printf(_("[EE] Wrong number of parameters. Please see \n"
// "%s --help\n"), argv[0]);
// exit(-1);
// case 'h' :
// default :
// display_help();
// }
// }
// while( opt != -1 );
//
// // Set these two to start from additional filenames on the cmdline:
// argc -= optind;
// argv += optind;
//}
void void
display_help() display_help()