virsh: Introduce virshEnumComplete()

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@redhat.com>
Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Michal Privoznik 2022-03-12 05:37:50 +01:00
parent c21e271d36
commit d2e8718f3a
2 changed files with 31 additions and 0 deletions

View File

@ -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

View File

@ -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);