Managing command line arguments will lead to no more disagreements
This repository provides a single source file to seamlessly manage command line arguments, compliant with the most widely compatible C90 and C++11 standards.
Using a UA_Parser
one can define needed arguments and manage their traits (simple flag, required argument, multiple parameters, etc) and ultimately check if parsing is successful and retrieve stored values.
#include <stdio.h>
#define UNARGUABLE_IMPLEMENTATION
#include "unarguable.h"
int main(int argc, const char *argv[]) {
UA_Parser *parser = ua_parser_create();
UA_Argument *arg;
UA_ArgValues *argValue;
const char *wrongArg = NULL;
ua_parser_add_argument(parser, UA_ARGUMENT_BOTH, "s", "save", 1, true);
ua_parser_populate_arguments(parser, argc, argv);
arg = ua_parser_get_argument(parser, "s");
UA_PRINT_ARGUMENT(arg, "s");
if ((wrongArg = ua_parser_is_complete(parser)) != NULL) {
fprintf(stderr, "First wrong argument is: '%s'\n", wrongArg);
return 1;
}
argValue = ua_argument_get_values(arg);
printf("Argument --save is: %s\n", argValue->items[0]);
return 0;
}