We have plenty of completers which iterate over all values of given enum and do nothing more than translate every member into string (using corresponding virXXXTypeToString()). Introduce a convenience function so that callers can pass just VIR_XXX_LAST and virXXXTypeToString and the rest is taken care of. Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- tools/virsh-completer.c | 27 +++++++++++++++++++++++++++ tools/virsh-completer.h | 4 ++++ 2 files changed, 31 insertions(+) diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 3d77be3121..e5610d0fe8 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -57,6 +57,33 @@ */ +/** + * virshEnumComplete: + * @last: The number of element in enum (pass VIR_XXX_LAST) + * @intToStr: integer to string conversion (pass virXXXTypeToString) + * + * Convenient function to generate completions across all values + * of given enum. The enum, or values we want to generate, must + * start at 0 and be continuous until @last. + * + * Returns: string list of completions. + */ +char ** +virshEnumComplete(unsigned int last, + const char *(*intToStr)(int)) +{ + char **ret = NULL; + size_t i; + + ret = g_new0(char *, last + 1); + + for (i = 0; i < last; i++) + ret[i] = g_strdup(intToStr(i)); + + return ret; +} + + /** * virshCommaStringListComplete: * @input: user input so far diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index 1d7affbb23..131678dfbc 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -32,6 +32,10 @@ #include "virsh-completer-snapshot.h" #include "virsh-completer-volume.h" +char ** +virshEnumComplete(unsigned int last, + const char *(*intToStr)(int)); + char ** virshCommaStringListComplete(const char *input, const char **options); -- 2.34.1