pktools  2.6.6
Processing Kernel for geospatial data
pktestOption.cc
1 /**********************************************************************
2 pktestOption: example program how to use class Optionpk pktestOption.cc
3 Copyright (C) 2008-2013 Pieter Kempeneers
4 
5 This file is part of pktools
6 
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with pktools. If not, see <http://www.gnu.org/licenses/>.
19 ***********************************************************************/
20 #include <iostream>
21 #include <string>
22 #include "base/Optionpk.h"
23 
24 int main(int argc, char *argv[])
25 {
26  Optionpk<std::string> foo_opt("f","foo","command line option **foo** of type string can be invoked with either short (f) or long (foo) option","defaultString");
27  Optionpk<int> bar_opt("\0","bar","command line option **bar** of type int has no short option",false,1);//bar will only be visible in long help (hide=1)
28  Optionpk<bool> easterEgg_opt("egg","egg","this help information is useless",false,2);//this option will not be shown in help (hide=2)
29 
30  bool doProcess;//stop process when program was invoked with help option (-h --help)
31  try{
32  doProcess=foo_opt.retrieveOption(argc,argv);
33  bar_opt.retrieveOption(argc,argv);
34  easterEgg_opt.retrieveOption(argc,argv);
35  }
36  catch(std::string predefinedString){//command line option contained license or version
37  std::cout << predefinedString << std::endl;//report the predefined string to stdout
38  exit(0);//stop processing
39  }
40  if(!doProcess){//command line option contained help option
41  std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;//provide extra details for help to the user
42  exit(0);//stop processing
43  }
44  std::cout << "foo: ";
45  for(int ifoo=0;ifoo<foo_opt.size();++ifoo){
46  std::cout << foo_opt[ifoo] << " ";
47  }
48  std::cout << std::endl;
49  std::cout << foo_opt << std::endl;//short cut for code above
50 
51  if(bar_opt[0]>0)
52  std::cout << "long option for bar was used with a positive value" << std::endl;
53 
54  if(easterEgg_opt[0])
55  std::cout << "How did you find this option -egg or --egg? Not through the help info!" << std::endl;
56 }