Hex  1.0
Hydrogen-electron collision solver
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmdline.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2  * *
3  * / / / / __ \ \ / / *
4  * / /__ / / / _ \ \ \/ / *
5  * / ___ / | |/_/ / /\ \ *
6  * / / / / \_\ / / \ \ *
7  * *
8  * Jakub Benda (c) 2014 *
9  * Charles University in Prague *
10  * *
11 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12 
13 #ifndef HEX_CMDLINE
14 #define HEX_CMDLINE
15 
16 #include <algorithm>
17 #include <string>
18 
19 #include "misc.h"
20 
35 template <class DefaultCallback> bool HandleSwitch
36 (
37  int &i, int argc, char* argv[],
38  DefaultCallback callback
39 )
40 {
41  // check bounds
42  if (i >= argc)
43  return false;
44 
45  // option name
46  std::string optname = argv[i++];
47 
48  // remove leading dashes from the optname
49  while (optname[0] == '-')
50  optname.erase(optname.begin());
51 
52  // option argument
53  std::string optarg = "";
54 
55  // split option name at equation sign (if present)
56  auto iter = std::find (optname.begin(), optname.end(), '=');
57  if (iter != optname.end())
58  {
59  optarg = optname.substr(iter-optname.begin()+1);
60  optname = optname.substr(0, iter-optname.begin());
61  }
62  else
63  {
64  // look for optarg in the next argv[]
65  if (i < argc and argv[i][0] != '-')
66  optarg = argv[i++];
67  }
68 
69  return callback (optname, optarg);
70 }
71 
92 template <class Callback, class ...Params> bool HandleSwitch
93 (
94  int &i, int argc, char* argv[],
95  std::string longoptname, std::string shortoptname, unsigned noptarg, Callback callback,
96  Params ...params
97 )
98 {
99  // check bounds
100  if (i >= argc)
101  return false;
102 
103  // option name
104  std::string optname = argv[i];
105 
106  // remove leading dashes from the optname
107  while (optname[0] == '-')
108  optname.erase(optname.begin());
109 
110  // option argument
111  std::string optarg = "";
112 
113  // split option name at equation sign (if present)
114  auto iter = std::find (optname.begin(), optname.end(), '=');
115  if (iter != optname.end())
116  {
117  optarg = optname.substr(iter-optname.begin()+1);
118  optname = optname.substr(0, iter-optname.begin());
119  }
120 
121  // check that argv[i] is equal to the current optname
122  if (optname == longoptname or optname == shortoptname)
123  {
124  // move to next argv[]
125  i++;
126 
127  // get option argument (if required)
128  if (noptarg == 0)
129  {
130  return callback ("");
131  }
132  else if (noptarg == 1)
133  {
134  // use existing optarg (if any)
135  if (optarg.size() > 0)
136  return callback (optarg);
137 
138  // are there any more words in input?
139  if (i == argc)
140  throw exception ("Missing an argument for the option \"%s\".", optname.c_str());
141 
142  // use the next word as the option
143  return callback (argv[i++]);
144  }
145  else
146  {
147  throw exception ("An option cannot have more than one arguments, but \"%s\" has %d.", optname.c_str(), noptarg);
148  }
149  }
150 
151  // try to match the switch in the next pass
152  return HandleSwitch (i, argc, argv, params...);
153 }
154 
193 template <class ...Params> void ParseCommandLine
194 (
195  int argc, char* argv[], Params ...params
196 )
197 {
198  int i = 1; // start by handling the first program argument
199 
200  while
201  (
202  HandleSwitch (i, argc, argv, params...)
203  );
204 }
205 
206 #endif
void ParseCommandLine(int argc, char *argv[], Params...params)
Parse command line.
Definition: cmdline.h:194
CLArrayView exception
bool HandleSwitch(int &i, int argc, char *argv[], DefaultCallback callback)
Command line option default hanndler.
Definition: cmdline.h:36