class utils::ConsoleArgs
Overview
Class to parse and conveniently hold console flags and parameters. More…
#include <ConsoleArgs.hpp> class ConsoleArgs { public: // fields static size_t MAX_FLAG_PREFIX_DASHES = 2; // construction ConsoleArgs(int argc, const char* argv[]); // methods size_t CountFlags() const; bool HasFlag(std::string flag_name) const; bool HasParam(const std::string& flag_name, const std::string& param_name) const; const std::vector<std::string>& GetParams(const std::string& flag_name) const; void ForEachFlag(const std::function<void(const std::string&, const std::vector<std::string>&)>& LoopBody) const; };
Detailed Documentation
Class to parse and conveniently hold console flags and parameters.
Console arguments are parsed to the flags and flag parameters.
Given a following console input MyApp.exe -v 123 -X --in-files file1.txt file2.txt this class will interpret arguments as following:
“v”, “X”, and “in-files” are flags, “123” is a parameter of flag “v”, “file1.txt” and “file2.txt” are first and second parameters of flag “in-files” respectively.
Fields
static size_t MAX_FLAG_PREFIX_DASHES = 2
The maximal number of dashes in the argument prexif, to consider it a flag.
Construction
ConsoleArgs(int argc, const char* argv[])
Construct an instance of this class from console arguments. The first argument is the executable name and always ignored.
Parameters:
std::invalid_argument |
when parameter arguments are specified before any flag arguments. |
Methods
size_t CountFlags() const
Returns the number of flag arguments.
bool HasFlag(std::string flag_name) const
Check if there exists a flag with the specified name. The prefix dashes are ignored.
bool HasParam(const std::string& flag_name, const std::string& param_name) const
Check if a flag has a parameter with a specified name.
const std::vector<std::string>& GetParams(const std::string& flag_name) const
Returns parameters of a specified flag.
Parameters:
std::invalid_argument |
when there are no parameters defined for the flag. |
void ForEachFlag(const std::function<void(const std::string&, const std::vector<std::string>&)>& LoopBody) const
Executes a specified function for each flag in the arguments list.
The following example will print all flags:
args.ForEachFlag([](const std::string &flag, const std::vector<std::string> ¶ms){ std::cout << flag << std::endl; });
Parameters:
loop_body |
The function executed for each flag. The first argument is flag name; the second is a list of flag parameters. |