From e27d6106b86d72aefa3d9766f348b5f8fbe59caa Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 26 Jun 2013 00:37:48 +0200 Subject: add functions for creating options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/argparser.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/src/argparser.c b/src/argparser.c index fdcf84f..704174c 100644 --- a/src/argparser.c +++ b/src/argparser.c @@ -18,6 +18,7 @@ */ #include #include +#include /* Code style constants */ @@ -25,7 +26,7 @@ #define false 0 #define null 0 -/* Constants */ /* TODO */ +/* Constants */ #define ARGUMENTLESS 0 #define ARGUMENTED 1 #define VARIADIC 2 @@ -293,6 +294,83 @@ extern void args_dispose() } +/** + * Creates, but does not add, a option that takes no arguments + * + * @param standard The index of the standard alternative name + * @param count... The alterntive names + * @return The created option + */ +extern args_Option args_new_argumentless(int standard, int count, ...) +{ + args_Option rc; + va_list args; + long i; + rc.type = ARGUMENTLESS; + rc.help = null; + rc.argument = "NOTHING"; + rc.alternatives_count = count; + rc.alternatives = (char**)malloc(count * sizeof(char*)); + va_start(args, count); + for (i = 0; i < count; i++) + *(rc.alternatives + i) = va_arg(args, char*); + va_end(args); + if (standard < 0) + standard += rc.alternatives_count; + rc.standard = *(rc.alternatives + standard); +} + +/** + * Creates, but does not add, a option that takes one argument per use + * + * @param argument The new of the argument + * @param standard The index of the standard alternative name + * @param count... The alterntive names + * @return The created option + */ +extern args_Option args_new_argumented(char* argument, int standard, int count, ...) +{ + args_Option rc; + va_list args; + long i; + rc.type = ARGUMENTED; + rc.help = null; + rc.argument = argument == null ? "ARG" : argument; + rc.alternatives_count = count; + rc.alternatives = (char**)malloc(count * sizeof(char*)); + for (i = 0; i < count; i++) + *(rc.alternatives + i) = va_arg(args, char*); + if (standard < 0) + standard += rc.alternatives_count; + rc.standard = *(rc.alternatives + standard); +} + +/** + * Creates, but does not add, a option that takes all following arguments + * + * @param argument The new of the argument + * @param standard The index of the standard alternative name + * @param count... The alterntive names + * @return The created option + */ +extern args_Option args_new_variadic(char* argument, int standard, int count, ...) +{ + args_Option rc; + va_list args; + long i; + rc.type = VARIADIC; + rc.help = null; + rc.argument = argument == null ? "ARG" : argument; + rc.alternatives_count = count; + rc.alternatives = (char**)malloc(count * sizeof(char*)); + for (i = 0; i < count; i++) + *(rc.alternatives + i) = va_arg(args, char*); + if (standard < 0) + standard += rc.alternatives_count; + rc.standard = *(rc.alternatives + standard); +} + + /** * Gets an array of all options * -- cgit v1.2.3-70-g09d2