msbGrid  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
commandlinereader.hh
Go to the documentation of this file.
1 /*****************************************************************************
2 * This program is part of the msbGrid software *
3 * *
4 * msbGrid stands for multi-structured block Grid generator *
5 * *
6 * msbGrid is a free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * msbGrid is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * See the file COPYING for full copying permissions. *
17 *****************************************************************************/
23 #include <parametertree.hh>
24 #include <parametertreeparser.hh>
25 
26 #ifndef _COMMAND_LINE_READER_HH_
27 #define _COMMAND_LINE_READER_HH_
28 
29 namespace msbGrid
30 {
31 std :: string readCommandLineOptions(int argc, char **argv, msbGrid :: ParameterTree &paramTree)
32 {
33  // All command line options need to start with '-'
34  for ( int i = 1; i < argc; ++i ) {
35  if ( argv [ i ] [ 0 ] != '-' ) {
36  std :: ostringstream oss;
37  oss << "Command line argument " << i << " (='" << argv [ i ] << "') is invalid.";
38  return oss.str();
39  }
40 
41  std :: string paramName, paramValue;
42 
43  // read a --my-opt=abc option. This gets transformed
44  // into the parameter "MyOpt" with the value being "abc"
45  if ( argv [ i ] [ 1 ] == '-' ) {
46  // There is nothing after the '-'
47  if ( argv [ i ] [ 2 ] == 0 || !std :: isalpha(argv [ i ] [ 2 ]) ) {
48  std :: ostringstream oss;
49  oss << "Parameter name of argument " << i << " ('" << argv [ i ] << "')"
50  << " is invalid because it does not start with a letter.";
51  return oss.str();
52  }
53 
54  // copy everything after the "--" into a separate string
55  std :: string s(argv [ i ] + 2);
56 
57  // parse argument
58  unsigned j = 0;
59  while ( true ) {
60  if ( j >= s.size() ) {
61  // encountered the end of the string, i.e. we
62  // have a parameter where the argument is empty
63  paramName = s;
64  paramValue = "";
65  break;
66  } else if ( s [ j ] == '=' ) {
67  // we encountered a '=' character. everything
68  // before is the name of the parameter,
69  // everything after is the value.
70  paramName = s.substr(0, j);
71  paramValue = s.substr(j + 1);
72  break;
73  } else if ( s [ j ] == '-' ) {
74  // remove all "-" characters and capitalize the character after them
75  s.erase(j, 1);
76  if ( s.size() == j ) {
77  std :: ostringstream oss;
78  oss << "Parameter name of argument " << i << " ('" << argv [ i ] << "')"
79  << " is invalid (ends with a '-' character).";
80  return oss.str();
81  } else if ( s [ j ] == '-' ) {
82  std :: ostringstream oss;
83  oss << "Malformed parameter name in argument " << i << " ('" << argv [ i ] << "'): "
84  << "'--' in parameter name.";
85  return oss.str();
86  }
87 
88  s [ j ] = std :: toupper(s [ j ]);
89  } else if ( !std :: isalnum(s [ j ]) ) {
90  std :: ostringstream oss;
91  oss << "Parameter name of argument " << i << " ('" << argv [ i ] << "')"
92  << " is invalid (character '" << s [ j ] << "' is not a letter or digit).";
93  return oss.str();
94  }
95 
96  ++j;
97  }
98  } else {
99  // read a -myOpt abc option for the parameter "MyOpt" with a value of "abc"
100  paramName = argv [ i ] + 1;
101 
102  if ( argc == i + 1 || argv [ i + 1 ] [ 0 ] == '-' ) {
103  std :: ostringstream oss;
104  oss << "No argument given for parameter '" << argv [ i ] << "'!";
105  return oss.str();
106  }
107 
108  paramValue = argv [ i + 1 ];
109  ++i; // In the case of '-myOpt abc' each pair counts as two arguments
110  }
111 
112  // capitalize first letter of parameter name
113  paramName [ 0 ] = std :: toupper(paramName [ 0 ]);
114 
115  // Put the key=value pair into the parameter tree
116  paramTree [ paramName ] = paramValue;
117  }
118 
119  return "";
120 }
121 } /* namespace msbGrid*/
122 
123 #endif /* #ifndef _COMMAND_LINE_READER_HH_ */