Computer Assited Medical Intervention Tool Kit  version 3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
programarg.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * $CAMITK_LICENCE_BEGIN$
3  *
4  * CamiTK - Computer Assisted Medical Intervention ToolKit
5  * (c) 2001-2014 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
6  *
7  * Visit http://camitk.imag.fr for more information
8  *
9  * This file is part of CamiTK.
10  *
11  * CamiTK is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * CamiTK is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License version 3 for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
22  *
23  * $CAMITK_LICENCE_END$
24  ****************************************************************************/
25 
26 // -------------------- ProgramArg ------------------------
27 // Code inspired from ProgVals "Thinking in C++, 2nd Edition, Volume 2", chapter 4
28 // by Bruce Eckel & Chuck Allison, (c) 2001 MindView, Inc.
29 // Available at www.BruceEckel.com.
30 // Program values can be changed by command lineclass ProgVals
31 #include <map>
32 #include <iostream>
33 #include <string>
34 
42 class ProgramArg : public std::map<std::string, std::string> {
43 public:
44  ProgramArg(std::string defaults[][2], unsigned int sz) {
45  for(unsigned int i=0;i<sz;i++) insert(pair(defaults[i][0], defaults[i][1]));
46  };
47 
48  void parse(int argc, char* argv[], std::string usage, int offset = 1) {
49  for(int i = offset; i < argc; i++) {
50  string flag(argv[i]);
51  unsigned int equal = flag.find('=');
52  if(equal == string::npos) {
53  cerr << "Command line error: " << argv[i] << endl << usage << endl;
54  continue; // Next argument
55  }
56  string name = flag.substr(0, equal);
57  string value = flag.substr(equal + 1);
58  if(find(name) == end()) {
59  cerr << name << endl << usage << endl;
60  continue; // Next argument
61  }
62  operator[](name) = value;
63  }
64  };
65 
66  void print(std::ostream& out = std::cout) {
67  out << "Argument values:" << endl;
68  for(iterator it = begin(); it != end(); it++)
69  out << (*it).first << " = " << (*it).second << endl;
70  };
71 };
72 
73 string defaultsArg[][2] = {
74  { "-n", "none" },
75  { "-e", "none" },
76  { "-t", "none" },
77  { "-o", "ansys2pml-output.pml" },
78 };
79 
80 const char* usage = "usage:\n"
81 "ansys2pml -n=nodes1.txt,..,nodesN.txt [-e=elem1.txt,..elemN.txt] [-t=ELEM_TYPE] [-o=output]\n"
82 "Transform nodes and elements ANSYS files to a PML\n"
83 "To produce a correct PML, the file are to be ordered the same way they were exported in ANSYS\n"
84 "(Note no space around '=')\n"
85 "Where the flags can be any of: \n"
86 " -n input node files separated by a comma\n"
87 " -e input element files separated by a comma\n"
88 " -t element type (see below)\n"
89 " -o output PML file name\n"
90 "\n"
91 "Element type defines the element geometry:\n"
92 "- TETRAHEDRON elements are tetrahedron, node index are integer #1, #2, #3 and #5 for each element file line\n"
93 "- QUAD elements are 2D quads, node index are integer #1, #2, #3 and #4 (I,J,K,L) for each element file line\n"
94 ;
95 
96 // global ProgramArgument
98 
99 
ProgramArg(std::string defaults[][2], unsigned int sz)
Definition: programarg.h:44
ProgramArg argVal(defaultsArg, sizeof defaultsArg/sizeof *defaultsArg)
void print(std::ostream &out=std::cout)
Definition: programarg.h:66
string defaultsArg[][2]
Definition: programarg.h:73
TODO Comment class here.
Definition: programarg.h:42
void parse(int argc, char *argv[], std::string usage, int offset=1)
Definition: programarg.h:48
const char * usage
Definition: programarg.h:80