Steppable 0.0.1
A CAS project written from scratch in C++
Loading...
Searching...
No Matches
argParse.hpp
1/**************************************************************************************************
2 * Copyright (c) 2023-2025 NWSOFT *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy *
5 * of this software and associated documentation files (the "Software"), to deal *
6 * in the Software without restriction, including without limitation the rights *
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8 * copies of the Software, and to permit persons to whom the Software is *
9 * furnished to do so, subject to the following conditions: *
10 * *
11 * The above copyright notice and this permission notice shall be included in all *
12 * copies or substantial portions of the Software. *
13 * *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20 * SOFTWARE. *
21 **************************************************************************************************/
22
37
38#pragma once
39
40#include <map>
41#include <regex>
42#include <string>
43#include <unordered_map>
44#include <vector>
45
51{
53 using PosArgs = std::vector<std::string>;
54
56 // language=RegExp
57 [[maybe_unused]] const std::regex KEYWORD_ARG_REGEX(R"(^-([a-zA-Z]*):(-?[0-9]+)$)"); // NOLINT(cert-err58-cpp)
58
60 // language=RegExp
61 [[maybe_unused]] const std::regex SWITCH_REGEX(R"(^([-+])([a-zA-Z]*)$)"); // NOLINT(cert-err58-cpp)
62
80 {
81 private:
85 std::unordered_map<std::string, bool> switches;
87 std::map<std::string, std::string> switchDescriptions;
88
90 std::vector<std::string> posArgs; // Names are used for error messages only.
92 std::map<char, std::string> posArgDescriptions;
94 std::vector<bool> posArgIsNumber;
95
100 std::unordered_map<std::string, int> keywordArgs;
102 std::map<std::string, std::string> keywordArgDescriptions;
103
105 int argc;
106
108 std::vector<std::string> argv;
109
111 std::string programName;
112
113 public:
120 ProgramArgs(int _argc, const char** argv);
121
128 void parseArgs();
129
136 void addSwitch(const std::string& name, bool defaultValue, const std::string& description = "");
137
147 void addPosArg(char name,
148 const std::string& description = "",
149 bool requiresNumber = true); // Positional arguments are always required and ordered
150
157 void addKeywordArg(const std::string& name, int defaultValue, const std::string& description = "");
158
167 [[nodiscard]] std::string getPosArg(size_t index) const;
168
177 int getKeywordArgument(const std::string& name);
178
186 bool getSwitch(const std::string& name);
187
196 void printUsage(const std::string& reason = "") const;
197 };
198} // namespace steppable::__internals::utils
void parseArgs()
This function is used to parse the command-line arguments and store them in the appropriate class mem...
Definition argParse.cpp:170
void addPosArg(char name, const std::string &description="", bool requiresNumber=true)
This function is used to add a positional argument to the class.
Definition argParse.cpp:53
bool getSwitch(const std::string &name)
This function is used to get the value of a switch.
Definition argParse.cpp:149
std::map< std::string, std::string > keywordArgDescriptions
This map is used to store the descriptions of all keyword arguments specified.
Definition argParse.hpp:102
int getKeywordArgument(const std::string &name)
This function is used to get the value of a keyword argument.
Definition argParse.cpp:141
std::vector< bool > posArgIsNumber
This map stores whether the positional arguments are required to be numbers.
Definition argParse.hpp:94
std::string getPosArg(size_t index) const
This function is used to get the value of a positional argument.
Definition argParse.cpp:133
std::vector< std::string > posArgs
This vector is used to store the values of all positional arguments specified.
Definition argParse.hpp:90
std::vector< std::string > argv
This stores the arguments passed to the program.
Definition argParse.hpp:108
void addSwitch(const std::string &name, bool defaultValue, const std::string &description="")
This function is used to add a switch to the class.
Definition argParse.cpp:41
std::map< std::string, std::string > switchDescriptions
This map is used to store the descriptions of all switches specified.
Definition argParse.hpp:87
std::unordered_map< std::string, int > keywordArgs
This map is used to store the values of all keyword arguments specified. Keys are keyword argument na...
Definition argParse.hpp:100
std::unordered_map< std::string, bool > switches
This map is used to store the information of all switches specified. Keys are switch names,...
Definition argParse.hpp:85
std::map< char, std::string > posArgDescriptions
This map is used to store the descriptions of all positional arguments specified.
Definition argParse.hpp:92
ProgramArgs(int _argc, const char **argv)
The constructor of the ProgramArgs class, which copies the arguments passed to the program to the cla...
Definition argParse.cpp:158
void addKeywordArg(const std::string &name, int defaultValue, const std::string &description="")
This function is used to add a keyword argument to the class.
Definition argParse.cpp:47
void printUsage(const std::string &reason="") const
This function is used to print the possible command-line arguments. Usually called when the user spec...
Definition argParse.cpp:59
std::string programName
This stores the name of the program.
Definition argParse.hpp:111
int argc
This stores the number of arguments passed to the program.
Definition argParse.hpp:105
The namespace containing utility functions for the Steppable library.
Definition argParse.cpp:40
const std::regex SWITCH_REGEX(R"(^([-+])([a-zA-Z]*)$)")
This is the correct format of a switch.
const std::regex KEYWORD_ARG_REGEX(R"(^-([a-zA-Z]*):(-?[0-9]+)$)")
This is the correct format of a keyword argument.
std::vector< std::string > PosArgs
This is the type of the positional arguments. It is equivalent to a vector of string_views.
Definition argParse.hpp:53
Untitled