strusBase  0.17
programOptions.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 #ifndef _STRUS_BASE_PROGRAM_OPTIONS_HPP_INCLUDED
9 #define _STRUS_BASE_PROGRAM_OPTIONS_HPP_INCLUDED
11 #include <map>
12 #include <vector>
13 #include <string>
14 #include <iostream>
15 
16 namespace strus {
17 
20 {
21 public:
24  :m_errorhnd(errorhnd_),m_argc(0),m_argv(0){}
25 
28  :m_errorhnd(o.m_errorhnd),m_argc(o.m_argc),m_argv(o.m_argv),m_opt(o.m_opt){}
29 
31  ProgramOptions( ErrorBufferInterface* errorhnd_, int argc_, const char** argv_, int nofopt, ...);
32 
34  bool operator()( const std::string& optname) const;
35 
40  const char* operator[]( std::size_t idx) const;
41 
45  const char* operator[]( const std::string& optname) const;
46 
50  int asInt( const std::string& optname) const;
51 
55  unsigned int asUint( const std::string& optname) const;
56 
60  double asDouble( const std::string& optname) const;
61 
65  std::vector<std::string> list( const std::string& optname) const;
66 
69  int nofargs() const;
70 
73  const char** argv() const;
74 
77  void print( std::ostream& out);
78 
79 private:
81  class OptMapDef
82  {
83  public:
84  explicit OptMapDef( ErrorBufferInterface* errorhnd_) :m_errorhnd(errorhnd_){}
85 
86  void add( const char* arg);
87 
88  bool getOpt( const char* argv, std::vector<std::string>& optlist, std::string& optarg);
89 
90  ErrorBufferInterface* m_errorhnd;
91  std::map<std::string,bool> m_longnamemap;
92  std::map<char,std::string> m_aliasmap;
93  };
94 
95 private:
96  ErrorBufferInterface* m_errorhnd;
97  std::size_t m_argc;
98  char const** m_argv;
99  typedef std::multimap<std::string,std::string> OptMap;
100  typedef std::pair<std::string,std::string> OptMapElem;
101  OptMap m_opt;
102 };
103 
104 }//namespace
105 #endif
106 
ProgramOptions(const ProgramOptions &o)
Copy constructor.
Definition: programOptions.hpp:27
const char * operator[](std::size_t idx) const
Get the value of an argument specified by its index.
int nofargs() const
Get the number of program arguments.
unsigned int asUint(const std::string &optname) const
Get the value of an option specified by its long name as unsigned integer.
int asInt(const std::string &optname) const
Get the value of an option specified by its long name as signed integer.
std::vector< std::string > list(const std::string &optname) const
Get the list of values of an option specified by its long name.
ProgramOptions(ErrorBufferInterface *errorhnd_)
Default constructor.
Definition: programOptions.hpp:23
Program options parser.
Definition: programOptions.hpp:19
void print(std::ostream &out)
Print all proram options defined.
Interface for reporting and catching errors in modules.
Definition: errorBufferInterface.hpp:24
Interface for reporting and catching errors.
double asDouble(const std::string &optname) const
Get the value of an option specified by its long name as unsigned integer.
bool operator()(const std::string &optname) const
Test if an option specified by its long name is defined.
const char ** argv() const
Get the list of program arguments.