commandline.hh 3.2 KB
Newer Older
1
2
3
#ifndef RADIX_RADIXCOMMAND_COMMANDLINE_HH_
#define RADIX_RADIXCOMMAND_COMMANDLINE_HH_

4
#include <ostream>
5
6
7
8
9
10
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

11
#include "radixcore/visibility.hh"
12

13
14
15
16
namespace radix
{
class RADIX_PUBLIC CommandLine
{
17
18
 public:
  CommandLine(int argc, char **argv);
19

20
21
22
23
24
25
26
27
  /**
   * @brief addOption Adds official option to this commandline
   * @param name key
   * @param description description of option
   * @param required whether it is required or not
   */
  void addOption(const std::string &name, const std::string &description,
                 bool required = false);
28

29
30
  // dumps all options to \code out
  void printParsedLine(std::ostream &out) const;
31

32
33
  // dumps the optional and required options to \code out
  void help(std::ostream &out) const;
34

35
36
37
38
  // does an option exist in this commandline
  bool exists(const std::string &name) const;
  // does an option exist in this commandline
  bool existsNotEmpty(const std::string &name) const;
39

40
41
  const std::vector<std::string> &arguments() const;
  void setArguments(const std::vector<std::string> &args);
42

43
44
  std::string executable() const;
  void setExecutable(const std::string &executable);
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  void declareArgument(const std::string &id,
                       const std::string &description = std::string());
  /**
   * @brief get retrieve option value as type \code return_type
   * @param name key of command line option
   * @return \code return_type
   */
  template <typename return_type>
  return_type get(const std::string &name) const;
  /**
   * @brief get retrieve option value as type \code return_type
   * @param name key of command line option
   * @param defaultValue default return value in the event name was not provided
   * @return \code return_type
   */
  template <typename return_type>
  return_type get(const std::string &name, return_type defaultValue) const;
63

64
65
66
67
68
69
70
  /**
   * @brief arg retrieve argument as type \code return_type
   * @param i int index of argument [0,N)
   * @return \code return_type
   */
  template <typename return_type>
  return_type arg(size_t i) const;
71

72
73
74
75
76
77
78
79
80
81
82
83
  /**
   * @brief validate Checks for existince of required options
   * @param errors list of errors
   * @return success/failure
   */
  bool validate(std::vector<std::string> &errors) const;
  /**
   * @brief validate Checks for existince of required options
   * @param std::ostream& stream for errors
   * @return success/failure
   */
  bool validate(std::ostream &out) const;
84

85
86
87
88
89
90
91
92
93
 private:
  int mArgc;
  char **mArgv;
  std::string mExecutable;
  std::vector<std::string> mArgs;
  std::vector<std::pair<std::string, std::string>> mDeclArgs;
  std::unordered_map<std::string, std::tuple<std::string, std::string, bool>>
      mData;
};  // class CommandLine
94
95
96

//
// Specialized get/arg method declarations
97
98
99
100
101
102
103
104
template <>
std::string RADIX_PUBLIC CommandLine::get(const std::string &name) const;
template <>
std::string RADIX_PUBLIC CommandLine::get(const std::string &name,
                                          std::string defaultValue) const;
template <>
std::string RADIX_PUBLIC CommandLine::arg(size_t i) const;
}  // namespace radix
105
106
#include "radixcommand/commandline.i.hh"
#endif /** RADIX_RADIXCOMMAND_COMMANDLINE_HH_ */