aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/argparser.c47
-rw-r--r--src/argparser.h32
-rw-r--r--src/test.c10
3 files changed, 72 insertions, 17 deletions
diff --git a/src/argparser.c b/src/argparser.c
index 6efbf7a..ec1da0a 100644
--- a/src/argparser.c
+++ b/src/argparser.c
@@ -38,6 +38,8 @@ static void* map_get(args_Map* map, char* key);
static void map_put(args_Map* map, char* key, void* value);
static void _map_free(void** level, long level_size);
static void** map_free(args_Map* map);
+static void noop_2(char* used, char* std);
+static void noop_3(char* used, char* std, char* value);
/**
@@ -205,11 +207,12 @@ void args_dispose()
/**
* Creates, but does not add, a option that takes no arguments
*
+ * @param trigger Function to invoked when the option is used, with the used option and the standard option
* @param standard The index of the standard alternative name
- * @param alternatives... The alterntive names, end with `null`
+ * @param alternatives... The alternative names, end with `null`
* @return The created option
*/
-args_Option args_new_argumentless(int standard, char* alternatives, ...)
+args_Option args_new_argumentless(void (*trigger)(char*, char*), int standard, char* alternatives, ...)
{
long count = 1;
args_Option rc;
@@ -226,6 +229,7 @@ args_Option args_new_argumentless(int standard, char* alternatives, ...)
rc.help = null;
rc.argument = "NOTHING";
rc.alternatives_count = count;
+ rc.trigger = trigger == null ? noop_2 : trigger;
rc.alternatives = (char**)malloc(count * sizeof(char*));
va_start(args, alternatives);
@@ -242,12 +246,13 @@ args_Option args_new_argumentless(int standard, char* alternatives, ...)
/**
* Creates, but does not add, a option that takes one argument per use
*
+ * @param trigger Function to invoked when the option is used, with the used option, the standard option and the used value
* @param argument The new of the argument
* @param standard The index of the standard alternative name
- * @param alternatives... The alterntive names, end with `null`
+ * @param alternatives... The alternative names, end with `null`
* @return The created option
*/
-args_Option args_new_argumented(char* argument, int standard, char* alternatives, ...)
+args_Option args_new_argumented(void (*trigger)(char*, char*, char*), char* argument, int standard, char* alternatives, ...)
{
long count = 1;
args_Option rc;
@@ -264,6 +269,7 @@ args_Option args_new_argumented(char* argument, int standard, char* alternatives
rc.help = null;
rc.argument = argument == null ? "ARG" : argument;
rc.alternatives_count = count;
+ rc.triggerv = trigger == null ? noop_3 : trigger;
rc.alternatives = (char**)malloc(count * sizeof(char*));
va_start(args, alternatives);
@@ -280,12 +286,13 @@ args_Option args_new_argumented(char* argument, int standard, char* alternatives
/**
* Creates, but does not add, a option that takes all following arguments
*
+ * @param trigger Function to invoked when the option is used, with the used option and the standard option
* @param argument The new of the argument
* @param standard The index of the standard alternative name
- * @param alternatives... The alterntive names, end with `null`
+ * @param alternatives... The alternative names, end with `null`
* @return The created option
*/
-args_Option args_new_variadic(char* argument, int standard, char* alternatives, ...)
+args_Option args_new_variadic(void (*trigger)(char*, char*), char* argument, int standard, char* alternatives, ...)
{
long count = 1;
args_Option rc;
@@ -302,6 +309,7 @@ args_Option args_new_variadic(char* argument, int standard, char* alternatives,
rc.help = null;
rc.argument = argument == null ? "ARG" : argument;
rc.alternatives_count = count;
+ rc.trigger = trigger == null ? noop_2 : trigger;
rc.alternatives = (char**)malloc(count * sizeof(char*));
va_start(args, alternatives);
@@ -1603,3 +1611,30 @@ static void** map_free(args_Map* map)
return args_map_values;
}
+
+/**
+ * Dummy trigger
+ *
+ * @param used The used option alternative
+ * @param std The standard option alternative
+ */
+static void noop_2(char* used, char* std)
+{
+ (void) used;
+ (void) std;
+}
+
+/**
+ * Dummy trigger
+ *
+ * @param used The used option alternative
+ * @param std The standard option alternative
+ * @param value The used value
+ */
+static void noop_3(char* used, char* std, char* value)
+{
+ (void) used;
+ (void) std;
+ (void) value;
+}
+
diff --git a/src/argparser.h b/src/argparser.h
index a2fff4e..41040ad 100644
--- a/src/argparser.h
+++ b/src/argparser.h
@@ -57,6 +57,23 @@ typedef struct
*/
char* help;
+ /**
+ * Invoked when the option is used
+ *
+ * @param The used option alternative
+ * @param The standard option alternative
+ */
+ void (*trigger)(char*, char*);
+
+ /**
+ * Invoked when the option is used
+ *
+ * @param The used option alternative
+ * @param The standard option alternative
+ * @param The used value
+ */
+ void (*triggerv)(char*, char*, char*);
+
} args_Option;
@@ -187,31 +204,34 @@ extern void args_dispose(void);
/**
* Creates, but does not add, a option that takes no arguments
*
+ * @param trigger Function to invoked when the option is used, with the used option and the standard option
* @param standard The index of the standard alternative name
- * @param alternatives... The alterntive names, end with `null`
+ * @param alternatives... The alternative names, end with `null`
* @return The created option
*/
-extern args_Option args_new_argumentless(int standard, char* alternatives, ...);
+extern args_Option args_new_argumentless(void (*trigger)(char*, char*), int standard, char* alternatives, ...);
/**
* Creates, but does not add, a option that takes one argument per use
*
+ * @param trigger Function to invoked when the option is used, with the used option, the standard option and the used value
* @param argument The new of the argument
* @param standard The index of the standard alternative name
- * @param alternatives... The alterntive names, end with `null`
+ * @param alternatives... The alternative names, end with `null`
* @return The created option
*/
-extern args_Option args_new_argumented(char* argument, int standard, char* alternatives, ...);
+extern args_Option args_new_argumented(void (*trigger)(char*, char*, char*), char* argument, int standard, char* alternatives, ...);
/**
* Creates, but does not add, a option that takes all following arguments
*
+ * @param trigger Function to invoked when the option is used, with the used option and the standard option
* @param argument The new of the argument
* @param standard The index of the standard alternative name
- * @param alternatives... The alterntive names, end with `null`
+ * @param alternatives... The alternative names, end with `null`
* @return The created option
*/
-extern args_Option args_new_variadic(char* argument, int standard, char* alternatives, ...);
+extern args_Option args_new_variadic(void (*trigger)(char*, char*), char* argument, int standard, char* alternatives, ...);
/**
diff --git a/src/test.c b/src/test.c
index c02c80c..0dbb420 100644
--- a/src/test.c
+++ b/src/test.c
@@ -43,12 +43,12 @@ int main(int argc, char** argv)
"You should have received a copy of the GNU Affero General Public License\n"
"along with this library. If not, see <http://www.gnu.org/licenses/>.", 0, 1, 0);
- args_add_option(args_new_argumentless(1, "-h", "-?", "--help", NULL), "Prints this help message\n(and exits)");
- args_add_option(args_new_argumentless(0, "--hello", NULL), "Prints the text: hello world");
- args_add_option(args_new_argumentless(0, "++hidden", NULL), 0);
+ args_add_option(args_new_argumentless(NULL, 1, "-h", "-?", "--help", NULL), "Prints this help message\n(and exits)");
+ args_add_option(args_new_argumentless(NULL, 0, "--hello", NULL), "Prints the text: hello world");
+ args_add_option(args_new_argumentless(NULL, 0, "++hidden", NULL), 0);
- args_add_option(args_new_argumented("LINE", 0, "-l", "--line", NULL), "Prints the choosen line");
- args_add_option(args_new_variadic("LINE", 0, "--l", "--lines", NULL), "Prints the choosen lines");
+ args_add_option(args_new_argumented(NULL, "LINE", 0, "-l", "--line", NULL), "Prints the choosen line");
+ args_add_option(args_new_variadic(NULL, "LINE", 0, "--l", "--lines", NULL), "Prints the choosen lines");
args_parse(argc, argv);
args_support_alternatives();