30 #define CAG_OPTION_PRINT_DISTANCE 4
31 #define CAG_OPTION_PRINT_MIN_INDENTION 20
34 size_t *accessor_length, FILE *destination)
37 *accessor_length += fprintf(destination,
"=%s", option->
value_name);
42 size_t *accessor_length, FILE *destination)
44 const char *access_letter;
46 if (access_letter != NULL) {
47 while (*access_letter) {
49 *accessor_length += fprintf(destination,
"-%c", *access_letter);
52 *accessor_length += fprintf(destination,
", -%c", *access_letter);
60 size_t *accessor_length, FILE *destination)
64 *accessor_length += fprintf(destination,
"--%s", option->
access_name);
66 *accessor_length += fprintf(destination,
", --%s", option->
access_name);
74 size_t option_index, indention, result;
79 for (option_index = 0; option_index < option_count; ++option_index) {
81 option = &
options[option_index];
95 if (indention > result) {
106 size_t option_index, indention, i, accessor_length;
112 for (option_index = 0; option_index < option_count; ++option_index) {
113 option = &
options[option_index];
117 fputs(
" ", destination);
123 for (i = accessor_length; i < indention; ++i) {
124 fputs(
" ", destination);
127 fputs(
" ", destination);
130 fprintf(destination,
"\n");
135 size_t option_count,
int argc,
char **argv)
140 context->
argc = argc;
141 context->
argv = argv;
148 char *name,
size_t name_size)
167 if (strncmp(option->
access_name, name, name_size) == 0) {
212 context->
value = ++(*c);
218 if (context->
argc > context->
index + 1) {
243 while (**c && **c !=
'=') {
258 if (option == NULL) {
291 if (option == NULL) {
317 int a_index, shift_index, shift_count, left_index, right_index;
319 shift_count = option - start;
323 if (shift_count == 0) {
329 for (a_index = option; a_index < end; ++a_index) {
332 tmp = context->
argv[a_index];
336 for (shift_index = 0; shift_index < shift_count; ++shift_index) {
337 left_index = a_index - shift_index;
338 right_index = a_index - shift_index - 1;
339 context->
argv[left_index] = context->
argv[right_index];
343 context->
argv[a_index - shift_count] = tmp;
348 context->
index = end - shift_count;
353 return *c ==
'-' && *(c + 1) !=
'\0';
358 int next_index, next_option_index;
362 next_index = context->
index;
363 next_option_index = next_index;
367 c = context->
argv[next_option_index];
376 c = context->
argv[++next_option_index];
386 return next_option_index;
392 int old_index, new_index;
398 context->
value = NULL;
403 old_index = context->
index;
405 if (new_index >= 0) {
406 context->
index = new_index;
453 return context->
value;
459 return context->
index;
static void cag_option_parse_access_letter(cag_option_context *context, char **c)
static void cag_option_shift(cag_option_context *context, int start, int option, int end)
char cag_option_get(const cag_option_context *context)
Gets the identifier of the option.
void cag_option_print(const cag_option *options, size_t option_count, FILE *destination)
Prints all options to the terminal.
int cag_option_get_index(const cag_option_context *context)
Gets the current index of the context.
static const cag_option * cag_option_find_by_name(cag_option_context *context, char *name, size_t name_size)
const char * cag_option_get_value(const cag_option_context *context)
Gets the value from the option.
static int cag_option_find_next(cag_option_context *context)
static size_t cag_option_get_print_indention(const cag_option *options, size_t option_count)
#define CAG_OPTION_PRINT_DISTANCE
#define CAG_OPTION_PRINT_MIN_INDENTION
void cag_option_prepare(cag_option_context *context, const cag_option *options, size_t option_count, int argc, char **argv)
Prepare argument options context for parsing.
static const cag_option * cag_option_find_by_letter(cag_option_context *context, char letter)
static void cag_option_print_name(const cag_option *option, bool *first, size_t *accessor_length, FILE *destination)
bool cag_option_fetch(cag_option_context *context)
Fetches an option from the argument list.
static void cag_option_print_value(const cag_option *option, size_t *accessor_length, FILE *destination)
static bool cag_option_is_argument_string(const char *c)
static void cag_option_print_letters(const cag_option *option, bool *first, size_t *accessor_length, FILE *destination)
static void cag_option_parse_value(cag_option_context *context, const cag_option *option, char **c)
static void cag_option_parse_access_name(cag_option_context *context, char **c)
static struct cag_option options[]
const struct cag_option * options
const char * access_letters