From 96ade0d16b65816684cb75b14522de0b393c65c2 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 4 Aug 2014 07:22:17 +0200 Subject: misc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libargparser/argparser.h | 185 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 165 insertions(+), 20 deletions(-) (limited to 'src/libargparser/argparser.h') diff --git a/src/libargparser/argparser.h b/src/libargparser/argparser.h index af8477a..9809f7a 100644 --- a/src/libargparser/argparser.h +++ b/src/libargparser/argparser.h @@ -21,6 +21,17 @@ #include +#include + + + +#ifndef ARGS_CONST +# define ARGS_CONST __attribute__((const)) +#endif + +#ifndef ARGS_PURE +# define ARGS_PURE __attribute__((pure)) +#endif @@ -54,6 +65,29 @@ typedef enum args_option_type +/** + * Tristate type + */ +typedef enum args_tristate + { + /** + * False + */ + FALSE, + + /** + * True + */ + TRUE, + + /** + * Automatic + */ + AUTO + + } args_tristate_t; + + typedef struct args_option { /** @@ -146,6 +180,17 @@ typedef struct args_settings */ int alternative; + /** + * Whether to all arguments after the first file + * should also be parsed as files + */ + int stop_at_first_file; + + /** + * Whether to use colours + */ + args_tristate_t use_colours; + /** * The name of the executed command, will be freed by the parser */ @@ -182,14 +227,17 @@ typedef struct args_settings FILE* help_out; /** - * Abbreviated option expander, `null` for disabled + * Abbreviated option expander, `NULL` for disabled * - * @param argument The option that not recognised - * @param options All recognised options - * @param count The number of elements in `options` - * @return The only possible expansion, otherwise `NULL` + * @param argument The option that not recognised + * @param options All recognised options, order by order of appearance in the help, i.e. by inclusion + * @param standards The corresponding standard option for options in `options`, as a consequence of + * the order in `options` all identical values (will have identical address) in + * `standards` will directly follow eachother + * @param count The number of elements in `options` and `standards` + * @return The only possible expansion, otherwise `NULL` */ - const char* (*abbreviations)(const char* stub, const char** options, size_t count); + const char* (*abbreviations)(const char* stub, const char** options, const char** standards, size_t count); } args_settings_t; @@ -215,7 +263,7 @@ typedef struct args_state size_t unrecognised_count; /** - * The concatenation of `files` with blankspaces as delimiters, `null` if no files + * The concatenation of `files` with blankspaces as delimiters, `NULL` if no files */ char* message; @@ -225,13 +273,112 @@ typedef struct args_state char** files; /** - * The number of elements in `args_files` + * The number of elements in `files` */ size_t files_count; + /** + * Options, in order + */ + args_option* options; + + /** + * Number of elements in `options` + */ + size_t options_count; + + /** + * All recognised options + */ + const char** all_options; + + /** + * The standard argument for all recognised options, + * if `all_options_standard[i] == all_options_standard[j]`, + * then `all_options[i]` and `all_options[j]` are synonyms + */ + const char** all_options_standard; + + /** + * Number of elements in `all_options` and `all_options_standard` + */ + size_t all_options_count; + + /** + * Queue of objects that needs to be freed on dispose + */ + void** freequeue; + + /** + * The number of elements in `freequeue` + */ + ssize_t freeptr; + } args_state_t; +/** + * Argument parser class + */ +typedef struct args_parser +{ + /** + * Settings for argument parser + */ + args_settings_t settings; + + /** + * The state of the parser + */ + args_state_t state; + +} args_parser_t; + + + + +/** + * Initialise an argument parser + * + * @param parser The memory address of the parser to initialise + * @return Zero on success, -1 on error, `errno` will be set accordingly + */ +int args_initialise(args_parser_t* parser); + +/** + * Disposes of all resources, run this when you are done + * + * @param parser The parser + */ +void args_dispose(parser_t* parser); + +/** + * Checks the correctness of the number of used non-option arguments + * + * @param parser The parser + * @param min The minimum number of files + * @return Whether the usage was correct + */ +int args_test_files_min(args_parser_t* parser, size_t min); + +/** + * Checks the correctness of the number of used non-option arguments + * + * @param parser The parser + * @param max The maximum number of files + * @return Whether the usage was correct + */ +int args_test_files_max(args_parser_t* parser, size_t max); + +/** + * Checks the correctness of the number of used non-option arguments + * + * @param parser The parser + * @param min The minimum number of files + * @param max The maximum number of files + * @return Whether the usage was correct + */ +int args_test_files(args_parser_t* parser, size_t min, size_t max); /** * Dummy trigger @@ -240,7 +387,7 @@ typedef struct args_state * @param used The used option alternative * @param standard The standard option alternative */ -void args_noop_trigger(void* user_data, const char* used, const char* standard); +void args_noop_trigger(void* user_data, const char* used, const char* standard) ARGS_CONST; /** * Dummy trigger @@ -250,7 +397,7 @@ void args_noop_trigger(void* user_data, const char* used, const char* standard); * @param standard The standard option alternative * @param value The used value */ -void args_noop_trigger_v(void* user_data, const char* used, const char* standard, char* value); +void args_noop_trigger_v(void* user_data, const char* used, const char* standard, char* value) ARGS_CONST; /** * Stickless evaluator to always evaluates to false @@ -259,7 +406,7 @@ void args_noop_trigger_v(void* user_data, const char* used, const char* standard * @param argument The next argument * @return Whether the argument can be used without being sticky */ -int args_no_stickless(void* user_data, const char* value); +int args_no_stickless(void* user_data, const char* value) ARGS_CONST; /** * Default stickless evaluator @@ -268,7 +415,7 @@ int args_no_stickless(void* user_data, const char* value); * @param argument The next argument * @return Whether the argument can be used without being sticky */ -int args_default_stickless(void* user_data, const char* argument); +int args_default_stickless(void* user_data, const char* argument) ARGS_CONST; /** * Evalutator for end argument of variadic options that always evalutes to false @@ -277,20 +424,18 @@ int args_default_stickless(void* user_data, const char* argument); * @param value The next argument * @return Whether the argument can be used without being sticky */ -int args_no_variadic_end(void* user_data, char* value); +int args_no_variadic_end(void* user_data, char* value) ARGS_CONST; /** * The standard abbrevation expander * - * @param argument The option that not recognised - * @param options All recognised options - * @param count The number of elements in `options` - * @return The only possible expansion, otherwise `null` + * @param argument The option that not recognised + * @param standards The corresponding standard option for options in `options` + * @param count The number of elements in `options` and `standards` + * @return The only possible expansion, otherwise `NULL` */ -const char* args_standard_abbreviations(const char* argument, const char** options, size_t count); +const char* args_standard_abbreviations(const char* argument, const char** options, const char** standards, size_t count); #endif - - -- cgit v1.2.3-70-g09d2