ARTS  2.0.49
parameters.cc
Go to the documentation of this file.
1 /* Copyright (C) 2000-2008 Stefan Buehler <sbuehler@ltu.se>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
28 #include <iostream>
29 #include <cstdio>
30 #include <cstdlib>
31 #include "arts.h"
32 #ifdef HAVE_GETOPT_H
33 #include <getopt.h>
34 #else
35 #include <arts_getopt.h>
36 #endif
37 #include "parameters.h"
38 
41 
42 bool get_parameters(int argc, char **argv)
43 {
44  /*
45  The header file getopt.h declares some external variables:
46 
47  extern char *optarg; (argument value for options that take an argument)
48  extern int optind; (index in ARGV of the next element to be scanned.)
49  extern int opterr; (we don´t use this)
50  extern int optopt; (set to an option caracter that was recoginized)
51  */
52 
53  /*
54  From the GNU documentation:
55 
56  Describe the long-named options requested by the application.
57  The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
58  of `struct option' terminated by an element containing a name which is
59  zero.
60 
61  The field `has_arg' is:
62  no_argument (or 0) if the option does not take an argument,
63  required_argument (or 1) if the option requires an argument,
64  optional_argument (or 2) if the option takes an optional argument.
65 
66  If the field `flag' is not NULL, it points to a variable that is set
67  to the value given in the field `val' when the option is found, but
68  left unchanged if the option is not found.
69 
70  To have a long-named option do something other than set an `int' to
71  a compiled-in constant, such as set a value from `optarg', set the
72  option's `flag' field to zero and its `val' field to a nonzero
73  value (the equivalent single-letter option character, if there is
74  one). For long options that have a zero `flag' field, `getopt'
75  returns the contents of the `val' field.
76 
77  struct option
78  {
79  #if defined (__STDC__) && __STDC__
80  const char *name;
81  #else
82  char *name;
83  #endif
84  int has_arg;
85  int *flag;
86  int val;
87  };
88  */
89  struct option longopts[] =
90  {
91  { "basename", required_argument, NULL, 'b' },
92  { "describe", required_argument, NULL, 'd' },
93  { "groups", no_argument, NULL, 'g' },
94 #ifdef ENABLE_GUI
95  { "gui", no_argument, NULL, 'G' },
96 #endif
97  { "help", no_argument, NULL, 'h' },
98  { "includepath", required_argument, NULL, 'I' },
99  { "input", required_argument, NULL, 'i' },
100  { "methods", required_argument, NULL, 'm' },
101  { "numthreads", required_argument, NULL, 'n' },
102  { "plain", no_argument, NULL, 'p' },
103  { "reporting", required_argument, NULL, 'r' },
104 #ifdef ENABLE_DOCSERVER
105  { "docserver", optional_argument, NULL, 's' },
106  { "docdaemon", optional_argument, NULL, 'S' },
107  { "baseurl", required_argument, NULL, 'B' },
108 #endif
109  { "workspacevariables", required_argument, NULL, 'w' },
110  { "version", no_argument, NULL, 'v' },
111  { NULL, no_argument, NULL, 0 }
112  };
113 
114  parameters.usage =
115  "Usage: arts [-bBdghimnrsSvw]\n"
116  " [--basename <name>]\n"
117  " [--describe <method or variable>]\n"
118  " [--groups]\n"
119 #ifdef ENABLE_GUI
120  " [--gui]\n"
121 #endif
122  " [--help]\n"
123  " [--includepath <path>]\n"
124  " [--input <variable>]\n"
125  " [--methods all|<variable>]\n"
126  " [--numthreads <#>\n"
127  " [--plain]\n"
128  " [--reporting <xyz>]\n"
129 #ifdef ENABLE_DOCSERVER
130  " [--docserver[=<port>] --baseurl=BASEURL]\n"
131  " [--docdaemon[=<port>] --baseurl=BASEURL]\n"
132 #endif
133  " [--workspacevariables all|<method>]\n"
134  " file1.arts file2.arts ...";
135 
137  "The Atmospheric Radiative Transfer Simulator.\n\n"
138  "-b, --basename Set the basename for the report\n"
139  " file and for other output files.\n"
140  "-d, --describe Print the description String of the given\n"
141  " workspace variable or method.\n"
142  "-g --groups List all workspace variable groups.\n"
143 #ifdef ENABLE_GUI
144  "-G --gui Start with graphical user interface.\n"
145 #endif
146  "-h, --help Print this message.\n"
147  "-i, --input This is complementary to the --methods switch.\n"
148  " It must be given the name of a variable (or group).\n"
149  " Then it lists all methods that take this variable\n"
150  " (or group) as input.\n"
151  "-I --includepath Search path for include files. Can be given more\n"
152  " than once to add several paths.\n"
153  " Include paths can also be added by setting the\n"
154  " environment variable ARTS_INCLUDE_PATH. Multiple\n"
155  " paths have to be separated by colons.\n"
156  " Paths specified on the commandline have precedence\n"
157  " over the environment variable and will be searched\n"
158  " first.\n"
159  "-m, --methods If this is given the argument `all',\n"
160  " it simply prints a list of all methods.\n"
161  " If it is given the name of a variable\n"
162  " (or variable group), it prints all\n"
163  " methods that produce this\n"
164  " variable (or group) as output.\n"
165  "-n, --numthreads If arts was compiled with OpenMP support this option\n"
166  " can be used to set the maximum number of threads.\n"
167  " By default OpenMP uses all processors/cores.\n"
168  "-p --plain Generate plain help output suitable for\n"
169  " script processing.\n"
170  "-r, --reporting Three digit integer. Sets the reporting\n"
171  " level for agenda calls (first digit),\n"
172  " screen (second digit) and file (third \n"
173  " digit). All reporting levels can reach from 0\n"
174  " (only error messages) to 3 (everything).\n"
175  " The agenda setting applies in addition to both\n"
176  " screen and file output.\n"
177  " Default is 010.\n"
178 #ifdef ENABLE_DOCSERVER
179  "-s, --docserver Start documentation server. Optionally, specify\n"
180  " the port number the server should listen on.\n"
181  " Default is 9000.\n"
182  "-S, --docdaemon Start documentation server in the background.\n"
183 #endif
184  "-v, --version Show version information.\n"
185  "-w, --workspacevariables If this is given the argument `all',\n"
186  " it simply prints a list of all variables.\n"
187  " If it is given the name of a method, it\n"
188  " prints all variables needed by this method.";
189 
190  // Set the short options automatically from the last columns of
191  // longopts.
192  //
193  // Watch out! We also have to put in colons after options that take
194  // an argument, and a double colon for options with optional
195  // arguments. (Watch out, optional arguments only work with GNU
196  // getopt. But since the GNU getopt source code is included with
197  // ARTS, why not use this feature?)
198  //
199  String shortopts;
200  {
201  int i=0;
202  while (NULL != longopts[i].name )
203  {
204  char c = (char)longopts[i].val;
205  shortopts += c;
206  // cout << "name = " << longopts[i].name << "\n";
207  // cout << "val = " << longopts[i].val << "\n";
208 
209  // Check if we need to insert a colon
210  if ( required_argument == longopts[i].has_arg )
211  {
212  shortopts += ":";
213  }
214 
215  // Or a double colon maybe?
216  if ( optional_argument == longopts[i].has_arg )
217  {
218  shortopts += "::";
219  }
220 
221  ++i;
222  }
223  shortopts += '\0';
224  }
225  // cout << "shortopts: " << shortopts << '\n';
226 
227  int optc;
228 
229  while ( EOF != (optc = getopt_long (argc, argv, shortopts.c_str(),
230  longopts, (int *) 0) ) )
231  {
232  // cout << "optc = " << optc << '\n';
233  switch (optc)
234  {
235  case 'h':
236  parameters.help = true;
237  break;
238  case 'b':
240  break;
241  case 'B':
243  break;
244  case 'd':
246  break;
247  case 'g':
248  parameters.groups = true;
249  break;
250  case 'G':
251  parameters.gui = true;
252  break;
253  case 'i':
255  break;
256  case 'I':
257  parameters.includepath.push_back (optarg);
258  break;
259  case 'm':
261  break;
262  case 'n':
263  {
264  istringstream iss(optarg);
265  iss >> std::dec >> parameters.numthreads;
266  if ( iss.bad() || !iss.eof() )
267  {
268  cerr << "Argument to --numthreads (-n) must be an integer!\n";
269  arts_exit ();
270  }
271  break;
272  }
273  case 'p':
274  parameters.plain = true;
275  break;
276  case 'r':
277  {
278  // cout << "optarg = " << optarg << endl;
279  istringstream iss(optarg);
280  iss >> parameters.reporting;
281  ws(iss);
282  // This if statement should cover all cases: If there is
283  // no integer at all, is becomes bad (first condition). If
284  // there is something else behind the integer, ws does not
285  // reach the end of is (second condition).
286  if ( iss.bad() || !iss.eof() )
287  {
288  cerr << "Argument to --reporting (-r) must be an integer!\n";
289  arts_exit ();
290  }
291  break;
292  }
293  case 's':
294  {
295  if (optarg)
296  {
297  istringstream iss(optarg);
298  iss >> std::dec >> parameters.docserver;
299  if ( iss.bad() || !iss.eof() )
300  {
301  cerr << "Argument to --docserver (-s) must be an integer!\n";
302  arts_exit ();
303  }
304  }
305  else
306  parameters.docserver = -1;
307  break;
308  }
309  case 'S':
310  {
311  if (optarg)
312  {
313  istringstream iss(optarg);
314  iss >> std::dec >> parameters.docserver;
315  if ( iss.bad() || !iss.eof() )
316  {
317  cerr << "Argument to --docdaemon (-S) must be an integer!\n";
318  arts_exit ();
319  }
320  }
321  else
322  parameters.docserver = -1;
323 
324  parameters.daemon = true;
325  break;
326  }
327  case 'v':
328  parameters.version = true;
329  break;
330  case 'w':
332  break;
333  default:
334  // There were strange options.
335  return(1);
336  break;
337  }
338  }
339 
340  // Remaining things on the command line must be control file names.
341  // Get them one by one.
342  while ( optind < argc )
343  {
344  String dummy=argv[optind];
345  parameters.controlfiles.push_back(dummy);
346  optind++;
347  }
348 
349  // cout << "alle:\n" << parameters.controlfiles << '\n';
350 
351  // Look for include paths in the ARTS_PATH environment variable and
352  // append them to parameters.includepath
353 
354  char *artspathenv = getenv ("ARTS_INCLUDE_PATH");
355 
356  if (artspathenv)
357  {
358  String artspath (artspathenv);
359 
360  // Skip delimiters at beginning.
361  String::size_type lastPos = artspath.find_first_not_of(":", 0);
362  // Find first "non-delimiter".
363  String::size_type pos = artspath.find_first_of(":", lastPos);
364 
365  while (String::npos != pos || String::npos != lastPos)
366  {
367  parameters.includepath.push_back (artspath.substr (lastPos,
368  pos - lastPos));
369  lastPos = artspath.find_first_not_of(":", pos);
370  pos = artspath.find_first_of(":", lastPos);
371  }
372  }
373 
374 #ifdef ARTS_DEFAULT_INCLUDE_DIR
375  String arts_default_include_path (ARTS_DEFAULT_INCLUDE_DIR);
376  if (arts_default_include_path != "" && !parameters.includepath.nelem())
377  {
378  parameters.includepath.push_back (arts_default_include_path);
379  }
380 #endif
381 
382  return false;
383 }
Parameters::reporting
Index reporting
This should be a two digit integer.
Definition: parameters.h:89
Parameters::help
bool help
Only display the help text.
Definition: parameters.h:72
optional_argument
#define optional_argument
Definition: arts_getopt.h:99
Parameters::docserver
Index docserver
Port to use for the docserver.
Definition: parameters.h:114
getopt_long
int getopt_long()
Parameters
Structure to hold all command line Parameters.
Definition: parameters.h:42
Parameters::usage
String usage
Short message how to call the program.
Definition: parameters.h:68
Parameters::version
bool version
Display version information.
Definition: parameters.h:74
required_argument
#define required_argument
Definition: arts_getopt.h:98
Parameters::gui
bool gui
Flag to run with graphical user interface.
Definition: parameters.h:120
Parameters::workspacevariables
String workspacevariables
If this is given the argument ‘all’, it simply prints a list of all workspace variables.
Definition: parameters.h:105
option::has_arg
int has_arg
Definition: arts_getopt.h:90
my_basic_string< char >::size_type
Index size_type
Definition: mystring.h:98
Parameters::plain
bool plain
Generate plain help out suitable for script processing.
Definition: parameters.h:112
no_argument
#define no_argument
Definition: arts_getopt.h:97
Parameters::describe
String describe
Print the description String of the given workspace variable or method.
Definition: parameters.h:108
Parameters::helptext
String helptext
Longer message explaining the options.
Definition: parameters.h:70
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:62
Parameters::basename
String basename
If this is specified (with the -b –basename option), it is used as the base name for the report file ...
Definition: parameters.h:78
ARTS_DEFAULT_INCLUDE_DIR
#define ARTS_DEFAULT_INCLUDE_DIR
Definition: config.h:12
get_parameters
bool get_parameters(int argc, char **argv)
Get the command line parameters.
Definition: parameters.cc:42
Parameters::controlfiles
ArrayOfString controlfiles
The filenames of the controlfiles.
Definition: parameters.h:81
optarg
char * optarg
parameters.h
This file contains header information for the dealing with command line parameters.
Parameters::groups
bool groups
Print a list of all workspace variable groups.
Definition: parameters.h:110
Parameters::daemon
bool daemon
Flag to run the docserver in the background.
Definition: parameters.h:118
Parameters::methods
String methods
If this is given the argument ‘all’, it simply prints a list of all methods.
Definition: parameters.h:93
option
Definition: arts_getopt.h:82
optind
int optind
arts_getopt.h
Parameters::baseurl
String baseurl
Baseurl for the docserver.
Definition: parameters.h:116
parameters
Parameters parameters
Holds the command line parameters.
Definition: parameters.cc:40
option::name
char * name
Definition: arts_getopt.h:86
option::val
int val
Definition: arts_getopt.h:92
Parameters::input
String input
This is complementary to the methods switch.
Definition: parameters.h:101
Parameters::numthreads
Index numthreads
The maximum number of threads to use.
Definition: parameters.h:95
arts_exit
void arts_exit(int status)
This is the exit function of ARTS.
Definition: arts.cc:42
my_basic_string::npos
static const Index npos
Define npos:
Definition: mystring.h:96
Parameters::includepath
ArrayOfString includepath
List of paths to search for include files.
Definition: parameters.h:97
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:172
arts.h
The global header file for ARTS.