1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 #include "tclap/CmdLine.h"
4 #include <iterator>
5 
6 #include <sstream>
7 
8 using namespace TCLAP;
9 
10 // Define a simple 3D vector type
11 struct Vect3D {
12     double v[3];
13 
14     // operator= will be used to assign to the vector
operator =Vect3D15     Vect3D &operator=(const std::string &str) {
16         std::istringstream iss(str);
17         if (!(iss >> v[0] >> v[1] >> v[2]))
18             throw TCLAP::ArgParseException(str + " is not a 3D vector");
19 
20         return *this;
21     }
22 
printVect3D23     std::ostream &print(std::ostream &os) const {
24         std::copy(v, v + 3, std::ostream_iterator<double>(os, " "));
25         return os;
26     }
27 };
28 
29 // Create an ArgTraits for the 3D vector type that declares it to be
30 // of string like type
31 namespace TCLAP {
32 template <>
33 struct ArgTraits<Vect3D> {
34     typedef StringLike ValueCategory;
35 };
36 }
37 
main(int argc,char * argv[])38 int main(int argc, char *argv[]) {
39     CmdLine cmd("Command description message", ' ', "0.9");
40     ValueArg<Vect3D> vec("v", "vect", "vector", true, Vect3D(), "3D vector",
41                          cmd);
42 
43     try {
44         cmd.parse(argc, argv);
45     } catch (std::exception &e) {
46         std::cout << e.what() << std::endl;
47         return EXIT_FAILURE;
48     }
49 
50     vec.getValue().print(std::cout);
51     std::cout << std::endl;
52 }
53