Lightweight commandline parser single-header library in C++
- Define
DEFINE_CMDLINE_FIELDSmacro with the corresponding fields for your command line arguments:
#define DEFINE_CMDLINE_FIELDS(Define) \
Define(int, arg1, "-d", true); \
Define(bool, arg2, "-e", false); \
Define(char*, arg3, "-p", true); \
Define(float, arg4, "-f", true);Parameters:
Type of the argument
Name of the argument (the name is arbitrary)
The flag for which is searched
Whether or not to use the next argument for the value
- Call the
DEFINE_CMDLINEmacro:
DEFINE_CMDLINE();- Define command line variable of type CmdLine:
CmdLine CommandLine;- Call the
ParseCmdLinefunction:
bool bSuccess = ParseCmdLine(argc, argv, &CommandLine, sizeof(CmdLine), &Translator, sizeof(Translator);Parameters:
Number of arguments (passed to main by argc)
Pointer to arguments (passed to main by argv)
Pointer to CmdLine variable we just created
Size of CmdLine
Pointer to Translator (variable defined by the DEFINE_CMDLINE macro)
Size of Translator
Return value:
true on success, false on failure (failures only happen when a commandline arg is flagged to use the next arg as the value but there is no next arg).
See Example.cpp for a ready-to-use solution.
Any contributions are welcome.