Validating input with inquirer
<p>I'm trying to check if the length of my input is valid like this:</p>

<pre><code>questions = [
    inquirer.Text('b_file', message='.GBK File',
                  validate=lambda file: len(str(file))),
    inquirer.Text('e_file', message='.XLS File',
                  validate=lambda file: len(str(file)))]
</code></pre>

<p>But isn't working. Is saying that is not a valid input:</p>

<pre><code>&gt;&gt;&gt; import inquirer
&gt;&gt;&gt; questions = [
...     inquirer.Text('b_file', message='.GBK File',
...                   validate=lambda file: len(str(file))),
...     inquirer.Text('e_file', message='.XLS File',
...                   validate=lambda file: len(str(file)))]
&gt;&gt;&gt; answers = inquirer.prompt(questions)
[?] .GBK File: foo
&gt;&gt; "foo" is not a valid b_file.
</code></pre>

<p>The function used for <code>validate</code> must take <strong>two</strong> arguments; the first is a dictionary with previously given answers, and the second is the current answer.</p>

<p>The <a href="https://github.com/magmax/python-inquirer/blob/master/inquirer/questions.py#L115-L121" rel="nofollow">code to handle validation</a> catches <em>all</em> exceptions and turns those into validation errors, so using a lambda with just one argument will always result in validation failing.</p>

<p>Make your lambda accept the answers dictionary too; you can ignore the value given:</p>

<pre><code>questions = [
    inquirer.Text('b_file', message='.GBK File',
                  validate=lambda answers, file: len(str(file))),
    inquirer.Text('e_file', message='.XLS File',
                  validate=lambda answers, file: len(str(file)))]
</code></pre>

<p>With that change, the questions work:</p>

<pre><code>&gt;&gt;&gt; import inquirer
&gt;&gt;&gt; questions = [
...     inquirer.Text('b_file', message='.GBK File',
...                   validate=lambda answers, file: len(str(file))),
...     inquirer.Text('e_file', message='.XLS File',
...                   validate=lambda answers, file: len(str(file)))]
&gt;&gt;&gt; answers = inquirer.prompt(questions)
[?] .GBK File: foo
[?] .XLS File: bar
&gt;&gt;&gt; pprint(answers)
{'b_file': 'foo', 'e_file': 'bar'}
</code></pre>

python