aboutsummaryrefslogtreecommitdiffstats
path: root/src/libargparser/argparser.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/libargparser/argparser.h')
-rw-r--r--src/libargparser/argparser.h185
1 files changed, 165 insertions, 20 deletions
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 <stddef.h>
+#include <stdio.h>
+
+
+
+#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
{
/**
@@ -147,6 +181,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
*/
char* program;
@@ -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
-
-