BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
argparse.h
Go to the documentation of this file.
1
61#pragma once
62#include <string>
63#include <vector>
64#include <map>
65
66namespace argparse {
67
71struct Argument {
72 std::string _name;
73 std::string _short_alias;
74 std::string _help;
75
85
87
91 void ShowHelp() const;
92};
93
97struct Var {
98 int _val = 0;
99 std::string _str = "";
100
104 void Clear() {
105 _val = 0;
106 _str.clear();
107 }
108};
109
114 std::vector<Argument> _args;
115 std::map<std::string, Var> _vars;
116
117public:
128
135 int GetVarInt(std::string name_, int if_not_found_);
136
143 std::string GetVarStr(std::string name_, std::string if_not_found_);
144
145private:
152 bool _SearchName(std::string name_, Argument& dest_);
153
160 bool _SearchShortAlias(std::string short_alias_, Argument& dest_);
161
162private:
163 std::string _cur_name;
164
171 Result _Action(const Argument& dest, const std::string& it_);
172
173public:
179 Result ParseArgs(const std::vector<std::string>& args);
180
181 bool _showed_help = false;
182
186 void ShowHelp();
187
196 int AddArgument(
197 std::string name_,
198 std::string short_alias_,
199 std::string help_,
201 );
202
207};
208
209} // namespace argparse
Class for parsing command line arguments.
Definition argparse.h:113
int GetVarInt(std::string name_, int if_not_found_)
Get the integer value of a variable.
Definition argparse.cpp:111
bool _showed_help
Flag indicating whether help was shown.
Definition argparse.h:181
Result
Enumeration of possible results from parsing arguments.
Definition argparse.h:121
@ UNKNOWN_FLAG
Unknown flag encountered.
Definition argparse.h:124
@ SHOWED_HELP
Help message was shown.
Definition argparse.h:123
@ BAD_PARAM
Bad parameter provided.
Definition argparse.h:125
@ UNKNOWN_VAR
Unknown variable encountered.
Definition argparse.h:126
@ OK
Parsing successful.
Definition argparse.h:122
void ShowHelp()
Display help information for all arguments.
Definition argparse.cpp:102
Result ParseArgs(const std::vector< std::string > &args)
Parse command line arguments.
Definition argparse.cpp:58
int AddArgument(std::string name_, std::string short_alias_, std::string help_, Argument::Action action_=Argument::store_true)
Add an argument to the parser.
Definition argparse.cpp:141
ArgumentParser()
Constructor for ArgumentParser.
Definition argparse.cpp:181
std::string GetVarStr(std::string name_, std::string if_not_found_)
Get the string value of a variable.
Definition argparse.cpp:116
Structure representing a command line argument.
Definition argparse.h:71
Action
Enumeration of possible actions for an argument.
Definition argparse.h:79
@ do_nothing
No action.
Definition argparse.h:80
@ show_help
Show help message.
Definition argparse.h:83
@ store_true
Store true (1)
Definition argparse.h:82
@ store_false
Store false (0)
Definition argparse.h:81
void ShowHelp() const
Display help information for the argument.
Definition argparse.cpp:54
std::string _help
Help description for the argument.
Definition argparse.h:74
Action _action
Action associated with the argument.
Definition argparse.h:86
std::string _name
Full name of the argument (e.g., –help)
Definition argparse.h:72
std::string _short_alias
Short alias for the argument (e.g., -h)
Definition argparse.h:73
Structure representing a variable associated with an argument.
Definition argparse.h:97
void Clear()
Clear the variable values.
Definition argparse.h:104
std::string _str
String value of the variable.
Definition argparse.h:99
int _val
Integer value of the variable.
Definition argparse.h:98