gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
OptionParser.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef OPTIONPARSER_H_
18 #define OPTIONPARSER_H_
19 
20 #include <sstream>
21 #include <string>
22 using namespace std;
23 #include <string.h>
24 
31 class OptionParser {
32 public:
42  template<typename TYPE>
43  int getArg(string key, int argc, char *argv[], TYPE &ret, int i) {
44  string args;
45  int rv=getArgString(key, argc, argv, args, i);
46  stringstream arg;
47  arg<<args;
48  arg>>ret;
49  return rv;
50  }
51 
57  template<typename TYPE>
58  void convertArg(const char* arg, TYPE &ret) {
59  stringstream argC;
60  argC<<arg;
61  argC>>ret;
62  }
63 
72  int getArgString(string key, int argc, char *argv[], string &ret, int i) {
73  int loc=i;
74  do {
75  // if found, then break, with loc==i.
76  int startIndex=0; // strip any leading '-'
77  while (argv[i][startIndex]=='-') startIndex++;
78  string arg(&argv[i][startIndex]); // find the length of the arg
79  istringstream input(arg);
80  input.ignore(arg.length(),'=');
81  unsigned int length=arg.find('=');
82  if (length==string::npos)
83  length=arg.size();
84  if ((startIndex!=0) && (key.compare(0,key.length(),arg, 0, length)==0) && (arg.compare(0,length, key)==0)) { // we have a match !
85  // find if the key is in the first position
86  loc=i;
87  loc++;
88  if ((key.length()==arg.length()) & (i+1<argc)) { // argument is of the form : -count 10, so get the next arg.
89  if (argv[i+1][0]!='-') // guard against the -h -v case
90  ret=argv[++i];
91  } else // argument is for the form : --count=10, so strip the initial part
92  if (length==key.length()) // ensure that we havn't matched -n for -net
93  input>>ret;
94  break;
95  }
96  } while (++i<argc);
97  return loc;
98  }
99 };
100 
101 #endif // OPTIONPARSER_H_
int getArg(string key, int argc, char *argv[], TYPE &ret, int i)
Definition: OptionParser.H:43
STL namespace.
void convertArg(const char *arg, TYPE &ret)
Definition: OptionParser.H:58
int getArgString(string key, int argc, char *argv[], string &ret, int i)
Definition: OptionParser.H:72
gtkIOStream: /tmp/gtkiostream/include/OptionParser.H Source File
GTK+ IOStream  Beta