Commit 5b7ba786 by Etienne Samson

examples: add a helper for boolean-style options

parent 8e063eb6
......@@ -184,6 +184,25 @@ static int match_int_internal(
return 1;
}
int match_bool_arg(int *out, struct args_info *args, const char *opt)
{
const char *found = args->argv[args->pos];
if (!strcmp(found, opt)) {
*out = 1;
return 1;
}
if (!strncmp(found, "--no-", strlen("--no-")) &&
!strcmp(found + strlen("--no-"), opt + 2)) {
*out = 0;
return 1;
}
*out = -1;
return 0;
}
int is_integer(int *out, const char *str, int allow_negative)
{
return match_int_internal(out, str, allow_negative, NULL);
......
......@@ -91,6 +91,15 @@ extern int match_int_arg(
int *out, struct args_info *args, const char *opt, int allow_negative);
/**
* Check current `args` entry against a "bool" `opt` (ie. --[no-]progress).
* If `opt` matches positively, out will be set to 1, or if `opt` matches
* negatively, out will be set to 0, and in both cases 1 will be returned.
* If neither the positive or the negative form of opt matched, out will be -1,
* and 0 will be returned.
*/
extern int match_bool_arg(int *out, struct args_info *args, const char *opt);
/**
* Basic output function for plain text diff output
* Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`)
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment