A lightweight and structured argument parsing library for C
This document explains how to use the public API of ArgParser-C, including function descriptions and minimal usage examples
Basic Usage
1. Initialize the Parser
APC_ArgParser apc_init(int argc, char *argv[]);
Initializes a new argument parser instance using the argc and argv values provided to main()
Example
int main(
int argc,
char *argv[])
{
}
APC_ArgParser apc_init(int argc, char *argv[])
Initialize parser with argc and argv from the main() function.
int main(int argc, char *argv[])
This prepares the parser and stores argument data internally
2. Define Arguments
Arguments are described using APC_ArgInfo.
APC_ArgInfo apc_initInfo(void);
Initializes an argument descriptor with safe defaults:
- required defaults to false
- aliases is automatically constructed
- All pointers are initialized to NULL
Example
info.
param =
"--version";
info.
help =
"Version information";
bool apc_add(APC_ArgParser *argpar, APC_ArgInfo info)
Setup Arg Information to the argpar.
APC_ArgInfo apc_initInfo(void)
Initialize information for params.
3. Register Arguments
bool apc_add(APC_ArgParser *argpar, APC_ArgInfo info);
Registers a configured APC_ArgInfo structure with the parser
Returns:
- true on success
- false on failure
Example with Aliases
info.
help =
"Show this help";
cvec_push(&info.
aliases,
const char *,
"-?");
4. Query Arguments
bool apc_get(APC_ArgParser *argpar, const char *id);
Checks whether a registered argument is present in argv.
Returns:
- true if the argument exists
- false otherwise
Example
{
printf("Version: %i.%i.%i\n",
}
bool apc_get(APC_ArgParser *argpar, const char *id)
Get if there is an argument present.
#define APC_VERSION_MINOR
#define APC_VERSION_MAJOR
#define APC_VERSION_PATCH
5. Generate Help Text
`char *apc_generateHelp(APC_ArgParser *argpar, const char *title, const char *topInfo, const char *lowerInfo);
Generates a formatted help message based on registered arguments
Parameters:
- title — Help title
- topInfo — Optional description or introduction
- lowerInfo — Optional footer text (license, notes, etc...)
Returns:
- Dynamically allocated string (must be freed)
Example
{
const char *license =
"\nVery cool license\n";
&parser,
"APC Help",
"ArgParser-C demonstration program\n",
license);
printf("%s\n", helpMsg);
}
char * apc_generateHelp(APC_ArgParser *argpar, const char *title, const char *topInfo, const char *lowerInfo)
Automatically generate help and pass it to the string to return.
6. Cleanup
void apc_destroy(APC_ArgParser *argpar);
Frees all allocated resources used by the parser
Example
return 0;
}
void apc_destroy(APC_ArgParser *argpar)
Free all allocated data for argument parser.
Public API Reference
Below is a summary of the non-private (public) API functions
apc_init
Initializes the argument parser
apc_initInfo
Creates a clean APC_ArgInfo structure
apc_add
Registers an argument definition
apc_get
Checks if a registered argument exists
apc_generateHelp
Automatically builds formatted help text
const char *title,
const char *topInfo,
const char *lowerInfo);
Memory ownership: The returned string must be released using APC_FREE().
apc_destroy
Destroys the parser and frees all internal allocations
Summary
Typical usage:
- Initialize parser (apc_init)
- Create argument definitions that can be modified (apc_initInfo)
- Register arguments (apc_add)
- Query arguments (apc_get)
- Optionally generate help (apc_generateHelp)
- Destroy parser (apc_destroy)
The API is intentionally minimal and explicit, designed to remain simple, predictable, and suitable for low-level C projects