Add mandatory option
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
11
src/option.c
11
src/option.c
@@ -88,6 +88,13 @@ option_option(void *data, int argc, char **argv)
|
|||||||
return i - 1;
|
return i - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int k = 0; opts[k].name; k++) {
|
||||||
|
if (opts[k].mandatory && !opts[k].set) {
|
||||||
|
gt_print("option `%s' is mandatory\n", opts[k].name);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return argc;
|
return argc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +112,7 @@ option_usage(struct option *opts, int slen)
|
|||||||
len = 0;
|
len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
len += gt_print(" [%s", opts[k].name);
|
len += gt_print(" %s%s", opts[k].mandatory ? "" : "[", opts[k].name);
|
||||||
|
|
||||||
if (opts[k].call == option_option) {
|
if (opts[k].call == option_option) {
|
||||||
len += option_usage((struct option *)opts[k].data, slen + len);
|
len += option_usage((struct option *)opts[k].data, slen + len);
|
||||||
@@ -113,7 +120,7 @@ option_usage(struct option *opts, int slen)
|
|||||||
len += gt_print(" ARG");
|
len += gt_print(" ARG");
|
||||||
}
|
}
|
||||||
|
|
||||||
len += gt_print("]");
|
len += gt_print(opts[k].mandatory ? "" : "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ struct option {
|
|||||||
char *name;
|
char *name;
|
||||||
void *data;
|
void *data;
|
||||||
int (*call) (void *, int, char **);
|
int (*call) (void *, int, char **);
|
||||||
|
int mandatory;
|
||||||
int set;
|
int set;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user