A simple command-line argument parser library for C/C++ programs.
src: contains all the source files, Makefile, and bash script required to build thelibArgParsing.sofile.testing: test driver program to validate operation of the ArgParsing library.
- C++17
- Make
- Navigate into
src. - Issue the command
sh Initialize.shto prepare the environment before creating the library file. - Issue the command
make. This will generate the shared object file inres/libArgParsing.so. - You can now copy the shared object file
res/libArgParsing.soto a desired location for your project. - If you intend to integrate
ArgParsingto a C++ project, you should copy the header filesrc/ArgParsing.hppto a desired location for your project. - If you intend to integrate
ArgParsingto a C project, you should copy the header filesrc/ArgParsing_C.hto a desired location for your project.
The file testing/ArgParsingExample.cpp is an integration example on how the ArgParsing library can be used within a C++ program.
Below you will find the publicly exposed methods belonging to the ArgParsing class.
- Input arguments: none.
- Output: a static reference to the singleton ArgParsing object.
- Only one instance of the
ArgParsingclass is allowed to exist throughout the execution of the program.
- Input arguments:
input_argc: the number of C strings in program's argv to be passed to theArgParsingobject.input_argv: an array of pointers to C strings to be passed to theArgParsingobject.
- Output: return 0 if no errors occurred. Return -1 if
input_argcis less than 1 orinput_argvis anullptr. - This method will assign the pointer to the array of strings containing the program's argument list (argv) and how many of them are in the list (argc) to the
ArgParsinginstance.
- Input arguments:
arg_table_ptr: a pointer to an array ofAPTableEntryobjects that will be used to defineArgParsing's argument table.n_entries: the number of elements in the array ofAPTableEntryobjects.
- Output: return 0 if no errors occurred. Return -1 if an error was encountred plus an informational message is reported.
- This method will initialize an argument table in the
ArgParsingobject. This table of arguments will be used to validate the program's command line arguments and hold their values once parsed and validated. - This method performs validation to prevent duplicate argument identifiers, use of reserved keywords, etc.
- Input arguments:
arg_table: a reference to a vector ofAPTableEntryobjects that will be used to defineArgParsing's argument table.
- Output: return 0 if no errors occurred. Return -1 if an error was encountred plus an informational message.
- Method overloading, calls
set_arg_table(APTableEntry* arg_table_ptr, size_t n_entries).
- Input arguments: none.
- Output: return 0 if no errors occurred. Return -1 if an error was encountred plus an informational message is reported.
- This method will parse the program's command line arguments set by
set_input_args()method and and validate them against the argument table establishedset_arg_table()method. - When an error is encontered, the
ArgParsingobject will switch to an error state and reason for error is set. - An informational message is reported describing the cause of the error.
- Input arguments:
arg_key: a string containing the argument identifier.is_abbr_input: a flag indicating whether the argument identifier string is in abbreviated or full form.
- Output: the argument value on its corresponding data type that
APDataTypemaps will be returned ifArgParsing's argument table has an entry with an argument identifier that matches the input argument. APDataTypemappings described below:
APDataType |
C++ data type |
|---|---|
TEXT |
std::string |
FLAG |
bool |
UNSIGNED_INT |
uint64_t |
SIGNED_INT |
int64_t |
- Input arguments:
arg_key: a string containing the argument identifier.is_abbr_input: a flag indicating whether the argument identifier string is in abbreviated or full form.
- Output: the size in bytes of the argument's current value.
- If the argument is of type
APDataType::FLAG,APDataType::UNSIGNED_INT, orAPDataType::SIGNED_INTthen returned value is the length in bytes of its corresponding base C++ data type. If the argument is of typeAPDataType::TEXT, the return value is the number of characters that forms the string.
The file testing/ArgParsingCExample.c is an integration example on how the ArgParsing library can be used within a C program.
Below you will find the C interface functions to interact with the ArgParsing class.
- Input arguments: none.
- Output: a pointer to
ArgParsing_Cobject. - The returned
ArgParsing_Cpointer points to a statically allocatedArgParsingobject.
- Input arguments:
apc: a pointer to aArgParsing_Cobject.input_argc: the number of C strings in program's argv to be passed to theArgParsingobject.input_argv: an array of pointers to C strings to be passed to theArgParsingobject.
- Output: return 0 if no errors occurred. Return -1 if
input_argcis less than 1 orinput_argvis anullptr. - This function calls the
ArgParsingmethodint set_input_args(int input_argc, char** input_argv).
int ArgParsing_C_set_arg_table(ArgParsing_C* apc, APTableEntry_C* input_arg_table, size_t n_entries)
- Input arguments:
apc: a pointer to aArgParsing_Cobject.input_arg_table: a pointer to an array ofAPTableEntry_Cobjects that will be used to defineArgParsing's argument table.n_entries: the number of elements in the array ofAPTableEntry_Cobjects.
- Output: return 0 if no errors occurred. Return -1 if an error was encountred plus an informational message is reported.
- Iterates through each element in the input array, converting them into
APTableEntryobjects, and placing these new objects into a vector. - This function calls the
ArgParsingmethodint ArgParsing::set_arg_table(std::vector<APTableEntry>& arg_table).
- Input arguments:
apc: a pointer to aArgParsing_Cobject.
- Output: return 0 if no errors occurred. Return -1 if an error was encountred plus an informational message is reported.
- This function calls the
ArgParsingmethodint parse().
int ArgParsing_C_get_value_TEXT(ArgParsing_C* apc, const char* arg_key, bool is_abbr_input, char* output_buffer, size_t len_output_buffer)
- Input arguments:
apc: a pointer to aArgParsing_Cobject.arg_key: a string containing the argument identifier.is_abbr_input: a flag indicating whether the argument identifier string is in abbreviated or full form.output_buffer: a chunk of memory allocated by the caller.len_output_buffer: the size in bytes of the chunk of memory allocated by the caller.
- Output: return 0 if the argument value string copy is successful. Return -1 if the output buffer length + 1 byte is smaller than the argument's value string, or if the argument's value string length is 0.
- This function calls the
ArgParsingmethodtemplate<typename T> get_arg_value(std::string arg_key, bool is_abbr_input)withTbeingstd::string. - It is important that
output_bufferis large enough to hold the argument's value in full, plus an extra byte of the null terminator character. - To verify the argument string value length, the caller may invoke
ArgParsing_C_get_arg_value_bytesizeto retrieve the number of characters that form the argument's value string. - It is highly recommended that
output_bufferis initialized with zeros.
- Input arguments:
apc: a pointer to aArgParsing_Cobject.arg_key: a string containing the argument identifier.is_abbr_input: a flag indicating whether the argument identifier string is in abbreviated or full form.
- Output: if the argument exists, the
boolvalue associated with the argument is returned. - This function calls the
ArgParsingmethodtemplate<typename T> get_arg_value(std::string arg_key, bool is_abbr_input)withTbeingbool.
uint64_t ArgParsing_C_get_value_UNSIGNED_INT(ArgParsing_C* apc, const char* arg_key, bool is_abbr_input)
- Input arguments:
apc: a pointer to aArgParsing_Cobject.arg_key: a string containing the argument identifier.is_abbr_input: a flag indicating whether the argument identifier string is in abbreviated or full form.
- Output: if the argument exists, the
uint64_tvalue associated with the argument is returned. - This function calls the
ArgParsingmethodtemplate<typename T> get_arg_value(std::string arg_key, bool is_abbr_input)withTbeinguint64_t.
int64_t ArgParsing_C_get_value_SIGNED_INT(ArgParsing_C* apc, const char* arg_key, bool is_abbr_input)
- Input arguments:
apc: a pointer to aArgParsing_Cobject.arg_key: a string containing the argument identifier.is_abbr_input: a flag indicating whether the argument identifier string is in abbreviated or full form.
- Output: if the argument exists, the
int64_tvalue associated with the argument is returned. - This function calls the
ArgParsingmethodtemplate<typename T> get_arg_value(std::string arg_key, bool is_abbr_input)withTbeingint64_t.
size_t ArgParsing_C_get_arg_value_bytesize(ArgParsing_C* apc, const char* arg_key, bool is_abbr_input)
- Input arguments:
apc: a pointer to aArgParsing_Cobject.arg_key: a string containing the argument identifier.is_abbr_input: a flag indicating whether the argument identifier string is in abbreviated or full form.
- Output: the size in bytes of the argument's current value.
- This function calls the
ArgParsingmethodsize_t get_arg_value_bytesize(std::string arg_id, bool is_abbr_input).
- Fixed bug when last argument in
argvis not aAPDataType::FLAGand no value is provided,ArgParsingshould report an error with reasonAPErrRsn::EXPECTING_VALUE. - Additional validation done in
int set_arg_table(APTableEntry* arg_table_ptr, size_t n_entries). - Updated Makefiles.
- Support for argument default values.
- Definition of the
template<typename T> APTableEntry(std::string, std::string, T)constructor method to allow setting default argument values in C++ and C interface. - The error codes
APErrRsn::MISSING_REQUIRED_ARG,APErrRsn::MUST_BE_FLAG, andAPErrRsn::REPEATED_ARGUMENTmessages now display abbreviated form identifier, if and only if it has been definied in the argument table, and full form identifier. - Miscellaneous bug fixes and general code clean up.
- Refactored
int ArgParsing_C_get_value_TEXT(ArgParsing_C*, const char*, bool, char*, size_t), fix issue to allow handling longerAPDataType::TEXTargument types by copying the value into a caller's allocated buffer. - Implemented method
size_t get_arg_value_bytesize(std::string, bool). - Implemented
size_t ArgParsing_C_get_arg_value_bytesize(ArgParsing_C*, const char*, bool)as part of the C API. - Fix bug missing
stdint.hinclude.
- Implemented Meyers' Singleton for
ArgParsingclass. - Updates to
ArgParsingExample.cppreflecting changes to support Meyers' Singleton implementation. - Definition of API for integration with C projects, including the following functions:
ArgParsing_C* ArgParsing_C_get_instance()void ArgParsing_C_set_input_args(ArgParsing_C*, int, char**)int ArgParsing_C_set_arg_table(ArgParsing_C*, APTableEntry_C*, size_t)int ArgParsing_C_parse(ArgParsing_C*)const char* ArgParsing_C_get_value_TEXT(ArgParsing_C*, const char*, bool)bool ArgParsing_C_get_value_FLAG(ArgParsing_C*, const char*, bool)uint64_t ArgParsing_C_get_value_UNSIGNED_INT(ArgParsing_C*, const char*, bool)int64_t ArgParsing_C_get_value_SIGNED_INT(ArgParsing_C*, const char*, bool)
- Miscellaneous fixes and code clean up.
- Definiton of data types for argument values:
APDataType::TEXTAPDataType::FLAGAPDataType::UNSIGNED_INTAPDataType::SIGNED_INT
- Store argument values in their equivalent C++ data type rather than just
std::string. - Refactored getter method using templates:
T get_arg_value(std::string, bool).
- Initial release.