Argx 1.0.0-build
Simple argument parser made in C++
 
Loading...
Searching...
No Matches
argx::Argx Class Reference

#include <Argx.hpp>

Public Member Functions

 Argx ()
 
 Argx (const std::string &id, int argc, char *argv[])
 Create Argx with the specific id, argc and argv
 
 ~Argx ()
 
void add (ARGXOptions option) const
 Add param options.
 
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.
 
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.
 
int findParam (const std::string &id)
 Find parameter index.
 
int getArgc () const
 Get the main options from the main() function as argc.
 
std::string getID () const
 Get Argx ID.
 
std::vector< std::string > getMainArgs () const
 Get main arguments from main() function argv
 
std::vector< ARGXOptionsgetOptions () const
 Get main set options as ARGXOptions.
 
ARGXParam getParam (const std::string &id)
 Get the param from id
 
bool getSubParam (const argx::ARGXParam &param, const std::string &id)
 Get the sub-param from id
 
bool paramExists (const std::string &id)
 Get if param exists in the param options.
 

Private Attributes

std::string id
 

Static Private Attributes

static unsigned int mainArgc
 
static std::vector< std::string > * mainArgs = nullptr
 
static std::vector< ARGXOptionsoptions
 

Detailed Description

Definition at line 17 of file Argx.hpp.

Constructor & Destructor Documentation

◆ Argx() [1/2]

argx::Argx::Argx ( const std::string & id,
int argc,
char * argv[] )

Create Argx with the specific id, argc and argv

Parameters
idSet the ID of the Argx
argcSet the argc of the main() function
argvSet the argv of the main() function

Definition at line 23 of file Argx.cpp.

24 : id(id)
25 {
26 this->mainArgs = new std::vector<std::string>(argv, argv + argc);
27 this->mainArgc = argc;
28 }
static unsigned int mainArgc
Definition Argx.hpp:25
static std::vector< std::string > * mainArgs
Definition Argx.hpp:23
std::string id
Definition Argx.hpp:20

References id, mainArgc, and mainArgs.

◆ Argx() [2/2]

argx::Argx::Argx ( )

Definition at line 30 of file Argx.cpp.

31 { }

◆ ~Argx()

argx::Argx::~Argx ( )

Definition at line 33 of file Argx.cpp.

34 {
35 delete this->mainArgs; this->mainArgs = nullptr;
36 }

References mainArgs.

Member Function Documentation

◆ add()

void argx::Argx::add ( ARGXOptions option) const

Add param options.

Parameters
optionAdd the option to the main params

Definition at line 38 of file Argx.cpp.

39 {
40 ARGXError error = {
41 .type = "success",
42 .code = 0
43 };
44
45 this->options.emplace_back(option);
46 }
static std::vector< ARGXOptions > options
Definition Argx.hpp:22
struct argx::ARGXError ARGXError

References options.

◆ compareArgs()

bool argx::Argx::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.

Parameters
optionsReturn ARGXOptions vector
idID to find
Returns
bool

Definition at line 293 of file Argx.cpp.

294 {
295 // Skip program name ( as arg )
296 for (size_t i = 1 ; i < argv.size() ; ++i)
297 {
298 const std::string &arg = argv[i];
299
300 bool found = false;
301 for (const auto &option : options)
302 {
303 if (option.sparam == arg || option.param == arg)
304 {
305 found = true;
306 break;
307 }
308 }
309 if (!found)
310 {
311 return false; // Unknown argument
312 }
313 }
314 return true; // All arguments are valid
315 }

References options.

◆ createDocs()

std::string argx::Argx::createDocs ( ARGXStyle style,
const std::string & title,
const std::string & mainInfo )

Create documentation for the parameters with the specific style, title and main information.

Parameters
styleSet the style using ARGXStyle
titleTitle for docs
Maininformation
Returns
std::string Documentation as a string

Definition at line 225 of file Argx.cpp.

226 {
227 std::string contentStr;
228
229 if (style == ARGXStyle::Professional)
230 {
231 for (const auto &x : this->options)
232 {
233 // Main option header line
234 contentStr += "ID: " + x.id + "\n";
235 contentStr += "[ " + x.sparam + " | " + x.param;
236
237 if (x.hasSubParams && !x.subParams.empty())
238 {
239 contentStr += " [ ";
240
241 for (size_t i = 0; i < x.subParams.size(); ++i)
242 {
243 const auto &sub = x.subParams[i];
244
245 contentStr += sub.param;
246
247 if (i < x.subParams.size() - 1) contentStr += " | ";
248 else if (i <= x.subParams.size()) contentStr += ' ';
249 }
250
251 contentStr += "] ] ";
252 }
253
254 else contentStr += " ] ";
255
256 contentStr += x.info + "\n";
257
258 // Print all sub-options with sparam and param, aligned with ideographic spaces if there are
259 if (x.hasSubParams && !x.subParams.empty())
260 {
261 for (const auto &sub : x.subParams)
262 {
263 // Create ideographic spaces matching the length of main param for alignment
264 std::wstring wideSpaces(x.param.size(), L'\u3000');
265 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
266 std::string spacing = converter.to_bytes(wideSpaces);
267
268 contentStr += spacing + " [ " + sub.sparam + " | " + sub.param + " ] " + sub.info + "\n";
269 }
270 }
271 }
272 }
273
274 else if (style == ARGXStyle::Simple)
275 {
276 for (const auto &x : this->options)
277 {
278 contentStr += x.sparam + ", " + x.param + " - " + x.info + "\n";
279
280 if (x.hasSubParams && !x.subParams.empty())
281 {
282 for (const auto &sub : x.subParams)
283 {
284 contentStr += " " + sub.sparam + ", " + sub.param + " - " + sub.info + "\n";
285 }
286 }
287 }
288 } // ARGXStyle
289
290 return title + "\n" + mainInfo + "\n" + contentStr;
291 }

References options, argx::Professional, and argx::Simple.

◆ findParam()

int argx::Argx::findParam ( const std::string & id)

Find parameter index.

Definition at line 48 of file Argx.cpp.

49 {
50 // First check if it's a main parameter
51 for (size_t i = 0; i < this->options.size(); i++)
52 {
53 if (this->options[i].id == id)
54 {
55 // Check if this main parameter exists in arguments
56 for (const std::string &arg : *this->mainArgs)
57 {
58 if (arg == this->options[i].param || arg == this->options[i].sparam)
59 {
60 return static_cast<int>(i);
61 }
62 }
63 }
64 }
65
66 // Then look for sub-parameters
67 for (const auto &opt : this->options)
68 {
69 // Check if the parent option exists in the arguments
70 bool parentExists = false;
71
72 for (const std::string &arg : *this->mainArgs)
73 {
74 if (arg == opt.param || arg == opt.sparam)
75 {
76 parentExists = true;
77 break;
78 }
79 }
80
81 if (parentExists)
82 {
83 // Find the index of the requested sub-parameter
84 for (size_t i = 0; i < opt.subParams.size(); i++)
85 {
86 if (opt.subParams[i].id == id) return static_cast<int>(i);
87 }
88 }
89 }
90
91 return -1; // Not found
92 }

References mainArgs, and options.

Referenced by getSubParam(), and paramExists().

◆ getArgc()

int argx::Argx::getArgc ( ) const

Get the main options from the main() function as argc.

Returns
int Number of params including the executable param

Definition at line 320 of file Argx.cpp.

321 { return this->mainArgc; }

References mainArgc.

◆ getID()

std::string argx::Argx::getID ( ) const

Get Argx ID.

Returns
std::string Argx ID

Definition at line 326 of file Argx.cpp.

327 { return this->id; }

References id.

◆ getMainArgs()

std::vector< std::string > argx::Argx::getMainArgs ( ) const

Get main arguments from main() function argv

Returns
std::vector<std::string> Vector of strings for main arguments from argv

Definition at line 317 of file Argx.cpp.

318 { return *this->mainArgs; }

References mainArgs.

◆ getOptions()

std::vector< ARGXOptions > argx::Argx::getOptions ( ) const

Get main set options as ARGXOptions.

Returns
std::vector<ARGXOptions> Options to return

Definition at line 323 of file Argx.cpp.

324 { return this->options; }

References options.

◆ getParam()

ARGXParam argx::Argx::getParam ( const std::string & id)

Get the param from id

Parameters
idThe ID to get
Returns
ARGXParam Returnted parameter to get

Definition at line 103 of file Argx.cpp.

104 {
105 if (this->mainArgc <= 1) return {};
106
107 ARGXParam result;
108
109 // First, check if this is a top-level option
110 for (const auto &opt : this->options)
111 {
112 if (opt.id == id)
113 {
114 // Find the position of the main option in arguments
115 int mainOptionPos = -1;
116
117 for (size_t i = 0; i < this->mainArgs->size(); ++i)
118 {
119 if ((*this->mainArgs)[i] == opt.param || (*this->mainArgs)[i] == opt.sparam)
120 {
121 result.exists = true;
122 mainOptionPos = i;
123 break;
124 }
125 }
126
127 if (result.exists)
128 {
129 if (opt.hasSubParams)
130 {
131 // Check each sub-parameter
132 for (const auto &sub : opt.subParams)
133 {
134 bool subMatched = false;
135
136 // Look for sub-parameters after the main option
137 for (size_t i = mainOptionPos + 1; i < this->mainArgs->size(); ++i)
138 {
139 if ((*this->mainArgs)[i] == sub.param || (*this->mainArgs)[i] == sub.sparam)
140 {
141 subMatched = true;
142 break;
143 }
144 }
145
146 result.subExists.push_back(subMatched);
147 }
148 }
149
150 return result;
151 }
152 }
153 }
154
155 // If not found as top-level, check if it's a sub-parameter
156 for (const auto &opt : this->options)
157 {
158 // Find if the parent option exists and get its position
159 size_t parentPos = -1;
160
161 for (size_t i = 0; i < this->mainArgs->size(); ++i)
162 {
163 if ((*this->mainArgs)[i] == opt.param || (*this->mainArgs)[i] == opt.sparam)
164 {
165 parentPos = i;
166 break;
167 }
168 }
169
170 if (parentPos > -1 && opt.hasSubParams)
171 {
172 // Check if the requested sub-parameter exists after the parent
173 for (const auto &sub : opt.subParams)
174 {
175 if (sub.id == id)
176 {
177 for (size_t i = parentPos + 1; i < this->mainArgs->size(); ++i)
178 {
179 if ((*this->mainArgs)[i] == sub.param || (*this->mainArgs)[i] == sub.sparam)
180 {
181 result.exists = true;
182 break;
183 }
184 }
185
186 if (!result.exists && parentPos + 1 < this->mainArgs->size())
187 {
188 std::string nextArg = (*this->mainArgs)[parentPos + 1];
189
190 if (nextArg == sub.param || nextArg == sub.sparam) result.exists = true;
191 }
192
193 // Handle any sub-sub-parameters if they exist
194 if (result.exists && sub.hasSubParams)
195 {
196 for (const auto &subsub : sub.subParams)
197 {
198 bool subsubMatched = false;
199
200 for (size_t i = 0; i < this->mainArgs->size(); ++i)
201 {
202 if ((*this->mainArgs)[i] == subsub.param || (*this->mainArgs)[i] == subsub.sparam)
203 {
204 subsubMatched = true;
205 break;
206 }
207 }
208
209 result.subExists.push_back(subsubMatched);
210 }
211 }
212
213 return result;
214 }
215 }
216 }
217 }
218
219 return result;
220 }
struct argx::ARGXParam ARGXParam

References argx::ARGXParam::exists, mainArgc, mainArgs, options, and argx::ARGXParam::subExists.

◆ getSubParam()

bool argx::Argx::getSubParam ( const argx::ARGXParam & param,
const std::string & id )

Get the sub-param from id

Parameters
paramOriginal param
idThe ID to get
Returns
bool

Definition at line 222 of file Argx.cpp.

223 { return this->paramExists(id) && param.subExists[this->findParam(id)]; }
bool paramExists(const std::string &id)
Get if param exists in the param options.
Definition Argx.cpp:94
int findParam(const std::string &id)
Find parameter index.
Definition Argx.cpp:48
std::vector< bool > subExists
Definition types.hpp:51

References findParam(), paramExists(), and argx::ARGXParam::subExists.

◆ paramExists()

bool argx::Argx::paramExists ( const std::string & id)

Get if param exists in the param options.

Parameters
idID to get
Returns
bool

Definition at line 94 of file Argx.cpp.

95 {
96 if (this->findParam(id) >= 0) return true;;
97
98 return false;
99 }

References findParam().

Referenced by getSubParam().

Member Data Documentation

◆ id

std::string argx::Argx::id
private

Definition at line 20 of file Argx.hpp.

Referenced by Argx(), and getID().

◆ mainArgc

unsigned int argx::Argx::mainArgc
staticprivate

Definition at line 25 of file Argx.hpp.

Referenced by Argx(), getArgc(), and getParam().

◆ mainArgs

std::vector< std::string > * argx::Argx::mainArgs = nullptr
staticprivate

Definition at line 23 of file Argx.hpp.

Referenced by Argx(), ~Argx(), findParam(), getMainArgs(), and getParam().

◆ options

std::vector< ARGXOptions > argx::Argx::options
staticprivate

Definition at line 22 of file Argx.hpp.

Referenced by add(), compareArgs(), createDocs(), findParam(), getOptions(), and getParam().


The documentation for this class was generated from the following files: