Argx 1.2.2-build
Simple yet powerful argument parser made in C++
Loading...
Searching...
No Matches
Argx.hpp
Go to the documentation of this file.
1/* inc/argx.hpp
2 * Owned and created by: pcannon09
3 */
4
5#pragma once
6
7#include "macros.hpp"
8
9#if __cplusplus >= 201402L || defined(ARGX_AS_PYTHON_PACKAGE)
10#include <string>
11#include <vector>
12
13#include "../inc/types.hpp"
14
15namespace argx
16{
17 class Argx
18 {
19 private:
20 std::string id;
21
22 static std::vector<ARGXOptions> options;
23 static std::vector<std::string> *mainArgs;
24
25 static unsigned int mainArgc;
26
27 public:
28
29#if defined(ARGX_AS_PYTHON_PACKAGE)
30 /**
31 * @brief Create Argx with the specific `id` and `args` as a python package
32 * @param id Set the ID of the Argx
33 * @param args Set the `argv` of the `main()` function with the specific list contents in python
34 */
35 Argx(const std::string &id, const std::vector<std::string> &args);
36
37 /**
38 * @brief Call the deconstructor of the Argx option (SPECIFIC FOR PYTHON)
39 */
40 void destroy();
41#else
42 /**
43 * @brief Create Argx with the specific `id`, `argc` and `argv`
44 * @param id Set the ID of the Argx
45 * @param argc Set the `argc` of the `main()` function
46 * @param argv Set the `argv` of the `main()` function
47 */
48 Argx(const std::string &id, int argc, char *argv[]);
49#endif
50
51 Argx();
52
53 /**
54 * @brief Deconstruct allocated objects
55 */
56 ~Argx();
57
58 /**
59 * @brief Format to a positive number if number is negative for a correct execution of code
60 * @param _int Number to convert
61 * @return int Positive value number
62 */
63 static int formatWrongArgs(const int &_int);
64
65 /**
66 * @brief Get argument position with specified `arg`
67 * @param arg Find argument
68 * @return int Return position of found `arg` from the options
69 */
70 int getArgPos(const std::string &arg);
71
72 /**
73 * @brief Add param options
74 * @param option Add the option to the main params
75 */
76 void add(ARGXOptions option) const;
77
78 /**
79 * @brief Get the param from `id`
80 * @param id The ID to get
81 * @return ARGXParam Returnted parameter to get
82 */
83 ARGXParam getParam(const std::string& id);
84
85 /**
86 * @brief Create documentation for the parameters with the specific style, title and main information
87 * @param style Set the style using ARGXStyle
88 * @param title Title for docs
89 * @param Main information
90 * @return std::string Documentation as a string
91 */
92 std::string createDocs(ARGXStyle style, const std::string &title, const std::string &mainInfo);
93
94 /**
95 * @brief Normal parameter or sub-paramter to its corresponding ID
96 * @param param Parameter value or name
97 * @return std::string ID of the param or sub-param
98 */
99 std::string paramToID(const std::string &param);
100
101 /**
102 * @brief Find parameter and sub-parameter index
103 * @param id ID to find
104 * @return int Index
105 */
106 int findParam(const std::string& id);
107
108 /**
109 * @brief Get the main options from the `main()` function as argc
110 * @return int Number of params including the executable param
111 */
112 int getArgc() const;
113
114 /**
115 * @brief Get argument using ID
116 * @param arg Argument to find
117 * @return int Argument position
118 */
119 int getArgIDPos(const std::string &arg);
120
121 /**
122 * @brief Get the incorrect arguments and sub-arguments that were not registered
123 * @param argv Main arguments from argv
124 * @return int Argument position
125 */
126 int getWrongArgs(const std::vector<std::string> &argv);
127
128 /**
129 * @brief Get if param exists in the param options
130 * @param id ID to get
131 * @return bool
132 */
133 bool paramExists(const std::string &id);
134
135 /**
136 * @brief Check if the `tag` exists in option with ID of `id`
137 * @param id ID from option
138 * @param tag Tag to find
139 * @return bool Return false if there is no match, else, return true
140 */
141 bool hasTag(const std::string &id, const std::string &tag);
142
143 /**
144 * @brief Get if sub-param exists in the param options
145 * @param id ID to get
146 * @return bool
147 */
148 bool subParamExists(const std::string &id);
149
150 /**
151 * @brief Compare if `options` contains the required `id`, if the ID does not exist, return false
152 * @param options Return ARGXOptions vector
153 * @param id ID to find
154 * @return bool
155 */
156 bool compareArgs(std::vector<ARGXOptions> options, std::vector<std::string> argv);
157
158 /**
159 * @brief Get Options from specified ID
160 * @param id ID to find
161 * @param ARGXOptions Option information
162 */
163 ARGXOptions getOption(const std::string &id);
164
165 /**
166 * @brief Get the sub-param from `id`
167 * @param param Original param
168 * @param id The ID to get
169 * @return bool
170 */
171 bool getSubParam(const argx::ARGXParam &param, const std::string &id);
172
173 /**
174 * @brief Get sub-parameter values, starting from the first value found until the first found value that corresponds to a registered parameter
175 * @param id ID to find to get sub-value
176 * @return std::vector<std::string> Values found from first to last
177 */
178 std::vector<std::string> getSubValue(const std::string &id);
179
180 /**
181 * @brief Get main arguments from `main()` function `argv`
182 * @return std::vector<std::string> Vector of strings for main arguments from `argv`
183 */
184 std::vector<std::string> getMainArgs() const;
185
186 /**
187 * @brief Get main set options as ARGXOptions
188 * @return std::vector<ARGXOptions> Options to return
189 */
190 std::vector<ARGXOptions> getOptions() const;
191
192 /**
193 * @brief Get Argx ID
194 * @return std::string Argx ID
195 */
196 std::string getID() const;
197 };
198}
199
200#else
201# error "Must compile with C++14 support or newer"
202# pragma message("Current standard is " ARGX_TOSTRING(__cplusplus))
203#endif
std::string paramToID(const std::string &param)
Normal parameter or sub-paramter to its corresponding ID.
Definition Argx.cpp:66
Argx(const std::string &id, int argc, char *argv[])
Create Argx with the specific id, argc and argv
Definition Argx.cpp:34
ARGXParam getParam(const std::string &id)
Get the param from id
Definition Argx.cpp:171
std::vector< std::string > getSubValue(const std::string &id)
Get sub-parameter values, starting from the first value found until the first found value that corres...
Definition Argx.cpp:580
static unsigned int mainArgc
Definition Argx.hpp:25
std::vector< ARGXOptions > getOptions() const
Get main set options as ARGXOptions.
Definition Argx.cpp:639
static std::vector< std::string > * mainArgs
Definition Argx.hpp:23
std::vector< std::string > getMainArgs() const
Get main arguments from main() function argv
Definition Argx.cpp:633
std::string getID() const
Get Argx ID.
Definition Argx.cpp:642
int getArgPos(const std::string &arg)
Get argument position with specified arg
Definition Argx.cpp:79
int getArgIDPos(const std::string &arg)
Get argument using ID.
Definition Argx.cpp:53
int getArgc() const
Get the main options from the main() function as argc.
Definition Argx.cpp:636
bool paramExists(const std::string &id)
Get if param exists in the param options.
Definition Argx.cpp:142
bool hasTag(const std::string &id, const std::string &tag)
Check if the tag exists in option with ID of id
Definition Argx.cpp:159
int findParam(const std::string &id)
Find parameter and sub-parameter index.
Definition Argx.cpp:96
~Argx()
Deconstruct allocated objects.
Definition Argx.cpp:48
std::string createDocs(ARGXStyle style, const std::string &title, const std::string &mainInfo)
Create documentation for the parameters with the specific style, title and main information.
Definition Argx.cpp:293
bool subParamExists(const std::string &id)
Get if sub-param exists in the param options.
Definition Argx.cpp:149
void add(ARGXOptions option) const
Add param options.
Definition Argx.cpp:93
static std::vector< ARGXOptions > options
Definition Argx.hpp:22
int getWrongArgs(const std::vector< std::string > &argv)
Get the incorrect arguments and sub-arguments that were not registered.
Definition Argx.cpp:395
static int formatWrongArgs(const int &_int)
Format to a positive number if number is negative for a correct execution of code.
Definition Argx.cpp:482
bool compareArgs(std::vector< ARGXOptions > options, std::vector< std::string > argv)
Compare if options contains the required id, if the ID does not exist, return false.
Definition Argx.cpp:489
bool getSubParam(const argx::ARGXParam &param, const std::string &id)
Get the sub-param from id
Definition Argx.cpp:290
std::string id
Definition Argx.hpp:20
ARGXOptions getOption(const std::string &id)
Get Options from specified ID.
Definition Argx.cpp:572
Definition Argx.hpp:16
ARGXStyle
Definition types.hpp:10