Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage documentation enhancements #467

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,21 @@ questions = [
]
```

The value can be a `function`, with the next sign:
The value can be a `function`, with the following signature:

```python
def get_message(answers): return str()
def get_message(answers: dict) -> str:
```

Example:

```python
from inquirer import text

def get_message(answers):
return "What's your name?"

Text(name='name', message= get_message)
text(name='name', message= get_message)
```

Where `answers` is the dictionary with previous answers.
Expand All @@ -89,10 +91,10 @@ If the `message` is too long for the terminal, it will be cut to fit.

Stores the default value to be used as answer. This allow the user just to press `Enter` to use it. It is optional, using `None` if there is no input and no default value.

As in `message`, you can use a new format string or a function with the sign:
As in `message`, you can use a new format string or a function with the signature:

```python
def get_default(answers): return str()
def get_default(answers: dict) -> str():
```

Where `answers` is a `dict` containing all previous answers.
Expand All @@ -105,10 +107,10 @@ Remember that it should be a list for `Checkbox` questions.

It contains the list of selectable answers.

Its value can be a `list` of strings, new format style strings or pairs(tuples) or a `function` that returns that list, with the sign:
Its value can be a `list` of strings, new format style strings or pairs(tuples) or a `function` that returns that list, with the signature:

```python
def get_choices(answers): return list(str())
def get_choices(answers: dict) -> list[str]:
```

If any of the list values is a pair, it should be a tuple like: `(label, value)`. Then the `label` will be shown but the `value` will be returned.
Expand All @@ -117,10 +119,10 @@ As before, the `answers` is a `dict` containing the previous answers.

### validate

Optional attribute that allows the program to check if the answer is valid or not. It requires a `boolean` value or a `function` with the sign:
Optional attribute that allows the program to check if the answer is valid or not. It requires a `boolean` value or a `function` with the signature:

```python
def validate(answers, current): return boolean()
def validate(answers: dict, current: str) -> bool:
```

Where `answers` is a `dict` with previous answers again and `current` is the current answer.
Expand All @@ -131,7 +133,7 @@ inside the validation function, but be aware that if the validation passes you s
Example:

```python
from inquirer import errors
from inquirer import errors, text
import random

def validation_function(answers, current):
Expand All @@ -141,25 +143,27 @@ def validation_function(answers, current):
return True


Text('nothing', "Moody question", validate=validation_function)
Text('age', "How old are you?", validate=lambda _, c: 0 <= c < 120)
text('nothing', "Moody question", validate=validation_function)
text('age', "How old are you?", validate=lambda _, c: 0 <= int(c) < 120)
```

### ignore

Questions are statically created and some of them may be optional depending on other answers. This attribute allows to control this by hiding the question.

It's value is `boolean` or a `function` with the sign:
It's value is `boolean` or a `function` with the signature:

```python
def ignore(answers): return boolean()
def ignore(answers: dict) -> bool:
```

where `answers` contains the `dict` of previous answers again.

Example:

```python
import inquirer

questions = [
inquirer.Text("name", message="What's your name?"),
inquirer.Text(
Expand Down
Loading