CITGuru / PyInquirer

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

Passing Multiple Arguments to Choices Function #98

Closed JeroenSchmidt closed 4 years ago

JeroenSchmidt commented 4 years ago

Is it possible to write a function that generates a list of choices that takes in more then one argument (other then answers)?

I want to do somthing like this:

from PyInquirer import prompt

def generate_list(answers, arg2):
                return([x for x in range(0,arg2)])

def main():
                arg2=10
                questions = [
                                {
                                "type":"list",
                                "name":"tables",
                                "message":"Which tables would you like to map?",
                                "choices":generate_list(answer,arg2)
                                }
                ]
                answers = prompt(questions )
rafarui commented 4 years ago

Is it possible to write a function that generates a list of choices that takes in more then one argument (other then answers)?

I want to do somthing like this:

from PyInquirer import prompt

def generate_list(answers, arg2):
                return([x for x in range(0,arg2)])

def main():
                arg2=10
                questions = [
                                {
                                "type":"list",
                                "name":"tables",
                                "message":"Which tables would you like to map?",
                                "choices":generate_list(answer,arg2)
                                }
                ]
                answers = prompt(questions )

there are two ways.

from functools import partial

def generate_list(answers, arg2):
    return([x for x in range(0,arg2)])

def main():
    arg2=10

    questions = [
        {
        "type":"list",
        "name":"tables",
        "message":"Which tables would you like to map?",
        "choices": lambda x: generate_list(x, arg2=arg2)
        }
    ]
    answers = prompt(questions )

or using from functools import partial

from functools import partial

def generate_list(answers, arg2):
    return([x for x in range(0,arg2)])

def main():
    arg2=10
    _generate_list = partial(generate_list, arg2=arg2)

    questions = [
        {
        "type":"list",
        "name":"tables",
        "message":"Which tables would you like to map?",
        "choices":_generate_list
        }
    ]
    answers = prompt(questions )
JeroenSchmidt commented 4 years ago

@rafarui Thanks a lot, that's exactly what I needed.

CITGuru commented 4 years ago

I can see this is settled, I will be closing this