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