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