ARTS  1.0.222
parameters.cc
Go to the documentation of this file.
1 /* Copyright (C) 2000, 2001 Stefan Buehler <sbuehler@uni-bremen.de>
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 "arts.h"
29 #include <getopt.h>
30 #include <cstdio>
31 #include "parameters.h"
32 
35 
36 bool get_parameters(int argc, char **argv)
37 {
38  /*
39  The header file getopt.h declares some external variables:
40 
41  extern char *optarg; (argument value for options that take an argument)
42  extern int optind; (index in ARGV of the next element to be scanned.)
43  extern int opterr; (we don´t use this)
44  extern int optopt; (set to an option caracter that was recoginized)
45  */
46 
47  /*
48  From the GNU documentation:
49 
50  Describe the long-named options requested by the application.
51  The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
52  of `struct option' terminated by an element containing a name which is
53  zero.
54 
55  The field `has_arg' is:
56  no_argument (or 0) if the option does not take an argument,
57  required_argument (or 1) if the option requires an argument,
58  optional_argument (or 2) if the option takes an optional argument.
59 
60  If the field `flag' is not NULL, it points to a variable that is set
61  to the value given in the field `val' when the option is found, but
62  left unchanged if the option is not found.
63 
64  To have a long-named option do something other than set an `int' to
65  a compiled-in constant, such as set a value from `optarg', set the
66  option's `flag' field to zero and its `val' field to a nonzero
67  value (the equivalent single-letter option character, if there is
68  one). For long options that have a zero `flag' field, `getopt'
69  returns the contents of the `val' field.
70 
71  struct option
72  {
73  #if defined (__STDC__) && __STDC__
74  const char *name;
75  #else
76  char *name;
77  #endif
78  int has_arg;
79  int *flag;
80  int val;
81  };
82  */
83  struct option longopts[] =
84  {
85  { "help", no_argument, NULL, 'h' },
86  { "version", no_argument, NULL, 'v' },
87  { "basename", required_argument, NULL, 'b' },
88  { "reporting", required_argument, NULL, 'r' },
89  { "methods", required_argument, NULL, 'm' },
90  { "input", required_argument, NULL, 'i' },
91  { "workspacevariables", required_argument, NULL, 'w' },
92  { "describe", required_argument, NULL, 'd' },
93  { "groups", no_argument, NULL, 'g' },
94  { NULL, no_argument, NULL, 0 }
95  };
96 
98  "Usage: arts [-hvbrmiwdg] [--help] [--version] [--basename <name>]\n"
99  " [--reporting xy]\n"
100  " [--methods all|<variable>]\n"
101  " [--input <variable>]\n"
102  " [--workspacevariables all|<method>]\n"
103  " [--describe <method or variable>]\n"
104  " [--groups]\n"
105  " file1.arts file2.arts ...";
106 
108  "The Atmospheric Radiative Transfer System.\n\n"
109  "-h, --help Print this message.\n"
110  "-v, --version Show version information.\n"
111  "-b, --basename Set the basename for the report\n"
112  " file and for other output files.\n"
113  "-r, --reporting Two digit integer. Sets the reporting\n"
114  " level for screen (first digit) anf file\n"
115  " (second digit). Levels can reach from 0\n"
116  " (only error messages) to 3 (everything).\n"
117  "-m, --methods If this is given the argument `all',\n"
118  " it simply prints a list of all methods.\n"
119  " If it is given the name of a variable\n"
120  " (or variable group), it prints all\n"
121  " methods that produce this\n"
122  " variable (or group) as output.\n"
123  "-i, --input This is complementary to the --methods switch.\n"
124  " It must be given the name of a variable (or group).\n"
125  " Then it lists all methods that take this variable\n"
126  " (or group) as input.\n"
127  "-w, --workspacevariables If this is given the argument `all',\n"
128  " it simply prints a list of all variables.\n"
129  " If it is given the name of a method, it\n"
130  " prints all variables needed by this method.\n"
131  "-d, --describe Print the description String of the given\n"
132  " workspace variable or method.\n"
133  "-g --groups List all workspace variable groups.";
134 
135  // Set the short options automatically from the last columns of
136  // longopts.
137  //
138  // Watch out! We also have to put in colons after options that take
139  // an argument, and a double colon for options with optional
140  // arguments. (Watch out, optional arguments only work with GNU
141  // getopt. But since the GNU getopt source code is included with
142  // ARTS, why not use this feature?)
143  //
144  String shortopts;
145  {
146  int i=0;
147  while (NULL != longopts[i].name )
148  {
149  char c = (char)longopts[i].val;
150  shortopts += c;
151  // cout << "name = " << longopts[i].name << "\n";
152  // cout << "val = " << longopts[i].val << "\n";
153 
154  // Check if we need to insert a colon
155  if ( required_argument == longopts[i].has_arg )
156  {
157  shortopts += ":";
158  }
159 
160  // Or a double colon maybe?
161  if ( optional_argument == longopts[i].has_arg )
162  {
163  shortopts += "::";
164  }
165 
166  ++i;
167  }
168  shortopts += '\0';
169  }
170  // cout << "shortopts: " << shortopts << '\n';
171 
172  int optc;
173 
174  while ( EOF != (optc = getopt_long (argc, argv, shortopts.c_str(),
175  longopts, (int *) 0) ) )
176  {
177  // cout << "optc = " << optc << '\n';
178  switch (optc)
179  {
180  case 'h':
181  parameters.help = true;
182  break;
183  case 'v':
184  parameters.version = true;
185  break;
186  case 'b':
188  break;
189  case 'r':
190  {
191  // cout << "optarg = " << optarg << endl;
192  istringstream is(optarg);
193  is >> parameters.reporting;
194  ws(is);
195  // This if statement should cover all cases: If there is
196  // no integer at all, is becomes bad (first condition). If
197  // there is something else behind the integer, ws does not
198  // reach the end of is (second condition).
199  if ( !is || !is.eof() )
200  {
201  cerr << "Argument to --reporting (-r) must be an integer!\n";
202  exit(1);
203  }
204  break;
205  }
206  case 'm':
208  break;
209  case 'i':
211  break;
212  case 'w':
214  break;
215  case 'd':
217  break;
218  case 'g':
219  parameters.groups = true;
220  break;
221  default:
222  // There were strange options.
223  return(1);
224  break;
225  }
226  }
227 
228  // Remaining things on the command line must be control file names.
229  // Get them one by one.
230  while ( optind < argc )
231  {
232  String dummy=argv[optind];
233  parameters.controlfiles.push_back(dummy);
234  optind++;
235  }
236 
237  // cout << "alle:\n" << parameters.controlfiles << '\n';
238 
239 
240 
241  return false;
242 }
Parameters::input
String input
This is complementary to the methods switch.
Definition: parameters.h:87
Parameters::reporting
Index reporting
This should be a two digit integer.
Definition: parameters.h:79
getopt.h
Parameters::describe
String describe
Print the description String of the given workspace variable or method.
Definition: parameters.h:94
Parameters::groups
bool groups
Print a list of all workspace variable groups.
Definition: parameters.h:96
Parameters::methods
String methods
If this is given the argument ‘all’, it simply prints a list of all methods.
Definition: parameters.h:83
Parameters::helptext
String helptext
Longer message explaining the options.
Definition: parameters.h:60
optind
int optind
Parameters::controlfiles
ArrayOfString controlfiles
The filenames of the controlfiles.
Definition: parameters.h:71
option::has_arg
int has_arg
Definition: getopt.h:90
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:68
my_basic_string< char >
get_parameters
bool get_parameters(int argc, char **argv)
Get the command line parameters.
Definition: parameters.cc:36
optional_argument
#define optional_argument
Definition: getopt.h:99
optarg
char * optarg
parameters.h
This file contains header information for the dealing with command line parameters.
option
Definition: getopt.h:82
Parameters
Structure to hold all command line Parameters.
Definition: parameters.h:42
Parameters::version
bool version
Display version information.
Definition: parameters.h:64
parameters
Parameters parameters
Holds the command line parameters.
Definition: parameters.cc:34
option::name
char * name
Definition: getopt.h:86
Parameters::usage
String usage
Short message how to call the program.
Definition: parameters.h:58
option::val
int val
Definition: getopt.h:92
no_argument
#define no_argument
Definition: getopt.h:97
Parameters::workspacevariables
String workspacevariables
If this is given the argument ‘all’, it simply prints a list of all workspace variables.
Definition: parameters.h:91
required_argument
#define required_argument
Definition: getopt.h:98
getopt_long
int getopt_long()
Parameters::help
bool help
Only display the help text.
Definition: parameters.h:62
arts.h
The global header file for ARTS.