TAP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Parser.hpp
Go to the documentation of this file.
1 
25 #pragma once
26 
27 #include <algorithm> // min/max
28 
29 namespace TAP {
30 
47 protected:
49  std::vector<ArgumentSet> m_argSets;
50 
53 
56  std::string m_programName;
57 public:
63  template<typename... Args>
64  ArgumentParser(Args&&... args);
65 
69  virtual ~ArgumentParser() {
70  }
71 
78  ArgumentParser& program_name(const std::string& programName) {
79  m_programName = programName;
80  return *this;
81  }
82 
87  const std::string& program_name() const {
88  return m_programName;
89  }
90 
96  template<typename... Args>
97  ArgumentParser& addAll(Args&&... args);
98 
104  template<typename Arg>
105  ArgumentParser& add(Arg&& arg);
106 
112  ArgumentParser& add(ArgumentSet argSet);
113 
122  template<typename Arg>
123  ArgumentParser& addConstraint(Arg&& constr);
124 
131  const Argument& operator[](char flag) const {
132  const Argument* arg = findArg(flag);
133  if (arg == nullptr) {
134  throw std::out_of_range("Argument not found");
135  }
136  return *arg;
137  }
138 
145  const Argument& operator[](const std::string& name) const {
146  const Argument* arg = findArg(name);
147  if (arg == nullptr) {
148  throw std::out_of_range("Argument not found");
149  }
150  return *arg;
151  }
152 
158  std::string help() const;
159 
169  void parse(int argc, const char* const argv[]);
170 
171 private:
179  const Argument* findArg() const;
180 
189  template<typename Ident>
190  const Argument* findArg(Ident ident) const;
191 
200  void parse(std::vector<std::string>& argv) const;
201 
208  void set_arg_value(const Argument* arg, const std::string& value) const;
209 };
210 
211 }
Argument parser class.
Definition: Parser.hpp:46
ArgumentParser & addConstraint(Arg &&constr)
Add the given constraint, to the parser.
void parse(int argc, const char *const argv[])
Parses the given arguments as they are presented on main() (see the parsing rules in the description ...
Simple argument class.
Definition: Argument.hpp:48
ArgumentSet m_constraints
Stored argument constraints.
Definition: Parser.hpp:52
const Argument * findArg() const
Find positional argument (see Argument::matches()), either the first one that can be set...
ArgumentParser(Args &&...args)
Construct a new ArgumentParser.
std::string m_programName
Program name as displayed in help text.
Definition: Parser.hpp:56
void set_arg_value(const Argument *arg, const std::string &value) const
Helper function to set the value of an Argument of which takes_value() returns true.
const std::string & program_name() const
Returns the program name.
Definition: Parser.hpp:87
virtual ~ArgumentParser()
ArgumentParser destructor.
Definition: Parser.hpp:69
const Argument & operator[](char flag) const
Get the Argument given the flag.
Definition: Parser.hpp:131
std::vector< ArgumentSet > m_argSets
Collection ArgumentSets (groups)
Definition: Parser.hpp:49
ArgumentParser & addAll(Args &&...args)
Add all given arguments, or constraints, to the parser.
ArgumentParser & program_name(const std::string &programName)
Set the program name.
Definition: Parser.hpp:78
std::string help() const
Generate a help string for the user to see.
Simple set of arguments.
Definition: ArgumentConstraint.hpp:194
const Argument & operator[](const std::string &name) const
Get the Argument given the flag.
Definition: Parser.hpp:145
ArgumentParser & add(Arg &&arg)
Add the given argument, or constraint, to the parser.