File: test30.cpp

package info (click to toggle)
tclap 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, experimental, forky, sid, trixie
  • size: 10,584 kB
  • sloc: cpp: 3,724; xml: 1,028; sh: 855; makefile: 308; javascript: 214; ansic: 43
file content (33 lines) | stat: -rw-r--r-- 1,184 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Example based on question in https://sourceforge.net/p/tclap/support-requests/2/
//
// Shows how to use a pair as a "custom type". Note that the argument
// flag must be specified, like -p "1 2.3" (with quotes).

#include <iostream>
#include <utility>

// We need to tell TCLAP how to parse our pair, we assume it will be
// given as two arguments separated by whitespace.
std::istream &operator>>(std::istream &is, std::pair<int, double> &p) {
  return is >> p.first >> p.second;
}

// Make it easy to print values of our type.
std::ostream &operator<<(std::ostream &os, const std::pair<int, double> &p) {
  return os << p.first << ' ' << p.second;
}

#include "tclap/CmdLine.h"
using namespace TCLAP;

// Our pair can now be used as any other type.
int main(int argc, char **argv) {
  CmdLine cmd("test pair argument");
  ValueArg<std::pair<int, double> > parg("p", "pair", "int,double pair",
                                         true,
                                         std::make_pair(0, 0.0),
                                         "int,double",
                                         cmd);
  cmd.parse(argc, argv);
  std::cout << parg.getValue() << std::endl;
}