CITGuru / PyInquirer

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

Using lambda and not classes to validate inputs #171

Open b-grimaud opened 2 years ago

b-grimaud commented 2 years ago

Hi,

I was wondering if and how is it possible to use lamda functions as validator for data input. In my opinion writing classes derived from another module's class, with its own object type, and that in the end only contain one single function which executes a simple test, seems a bit heavy and convoluted.

It apparently is possible to do so, but as far as I could tell there are two provided examples using lambdas as validators (editor and checkbox), and neither of them work (see #161 and #70).

So, is it actually possible to use lambdas, and if so, how ?

Thanks !

guedesfelipe commented 2 years ago

168 This pull request solves the checkbox validation issue

you can use this repo (https://github.com/guedesfelipe/PyInquirer) or make the changes on your machine, as the repository maintainer is no longer maintaining the project anymore #159

Example:

questions = [
    {
        'type': 'checkbox',
        'qmark': '📄',
        'message': 'Select Files',
        'name': 'csv_files',
        'choices': get_files(),
        'validate': lambda answers: 'You must select more than one file to join' \
            if len(answers) < 2 else True
    },
    {
        'type': 'input',
        'qmark': '✏️ ',
        'name': 'output_name',
        'message': 'Output file name',
        'validate': lambda answers: 'You need to put some name' \
            if len(answers.strip()) == 0 else True
    }
]

answers = prompt(questions)