pktools  2.6.7
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");//bar will only be visible in long help (hide=1)
28  Optionpk<bool> easterEgg_opt("egg","egg","this help information is useless");//this option will not be shown in help (hide=2)
29 
30  bar_opt.setHide(1);//option only visible with long help (--help)
31  easterEgg_opt.setHide(2);//hidden option
32 
33  bool doProcess;//stop process when program was invoked with help option (-h --help)
34  try{
35  doProcess=foo_opt.retrieveOption(argc,argv);
36  bar_opt.retrieveOption(argc,argv);
37  easterEgg_opt.retrieveOption(argc,argv);
38  }
39  catch(std::string predefinedString){//command line option contained license or version
40  std::cout << predefinedString << std::endl;//report the predefined string to stdout
41  exit(0);//stop processing
42  }
43  if(!doProcess){//command line option contained help option
44  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
45  exit(0);//stop processing
46  }
47  std::cout << "foo: ";
48  for(int ifoo=0;ifoo<foo_opt.size();++ifoo){
49  std::cout << foo_opt[ifoo] << " ";
50  }
51  std::cout << std::endl;
52  std::cout << foo_opt << std::endl;//short cut for code above
53 
54  if(bar_opt[0]>0)
55  std::cout << "long option for bar was used with a positive value" << std::endl;
56 
57  if(easterEgg_opt[0])
58  std::cout << "How did you find this option -egg or --egg? Not through the help info!" << std::endl;
59 }