CITGuru / PyInquirer

A Python module for common interactive command line user interfaces
MIT License
1.91k stars 235 forks source link

Return separators with choices, too, when using checkboxes #141

Open Rubo3 opened 3 years ago

Rubo3 commented 3 years ago

I've ran into a situation in which I had to get from a checkbox list not only the choices, but also their separators. This was because I had to pick the possible choices from a nested dictionary, where the keys of the external dictionary were the separators and the keys of the internal dictionaries were their choices. Something like this:

{
    separator1: {
        possible_choice1,
        ...
        possible_choiceN
    },
    separator2: {
        possible_choiceN+1,
        ...
        possible_choiceN+M
    },
    ...
}

The problem here is that the prompt only returns the choices, not their separators! But I have to elaborate them on a nested separator-choice for-cycle, so with only a list of choices I simply can't. For instance, I mean I have to do something like this:

for separator in dictionary:
    for choice in dictionary[separator]:
        # do stuff

Do you think it is possible to have the prompt return the separators, too? I was thinking that maybe an optional argument could be added to the prompt function which, if passed, adds to the returned choices list their separators. Something like choice (separator), so that the string can be processed to obtain both the choice and the separator and a nested dictionary like the one above, but only with the user choices, can be created.

Rubo3 commented 3 years ago

Or maybe, to generalise, every separator could have its own ID (an index?), and every possible choice between that separator and the next one could have the same one. What do you think?