rougail/doc/variable/choice.md

187 lines
3.7 KiB
Markdown
Raw Normal View History

2021-05-01 18:32:45 +02:00
# Les variables à choix
2021-05-05 15:27:06 +02:00
## Une variable à choix
2021-05-01 18:32:45 +02:00
Il est possible d'imposer une liste de valeur pour une variable particulière :
2022-11-03 22:17:43 +01:00
```xml
2021-05-01 18:32:45 +02:00
<variable name="my_variable" type="choice">
2021-12-11 16:33:55 +01:00
<choice>val1</choice>
<choice>val2</choice>
<choice>val3</choice>
2021-05-01 18:32:45 +02:00
</variable>
```
2022-11-02 22:52:50 +01:00
En YAML :
2022-11-03 22:17:43 +01:00
```yml
2022-11-02 22:52:50 +01:00
- variable:
name: my_variable
type: choice
choice:
- text: val1
- text: val2
- text: val3
```
2021-05-05 15:27:06 +02:00
Dans ce cas, seules les valeurs proposées sont possibles pour cette variable.
Cette variable n'est pas obligatoire dont il est possible de mettre la valeur "None".
2021-05-01 18:32:45 +02:00
2021-05-05 15:27:06 +02:00
Si la variable est obligatoire ou si une valeur est précisée (la variable passe obligatoire) alors la valeur "None" n'est plus autorisé :
2021-05-01 18:32:45 +02:00
2022-11-03 22:17:43 +01:00
```xml
2021-05-01 18:32:45 +02:00
<variable name="my_variable" type="choice">
2021-12-11 16:33:55 +01:00
<choice>val1</choice>
<choice>val2</choice>
<choice>val3</choice>
<value>val1</value>
2021-05-01 18:32:45 +02:00
</variable>
```
2021-05-05 15:27:06 +02:00
2022-11-02 22:52:50 +01:00
En YAML :
2022-11-03 22:17:43 +01:00
```yml
2022-11-02 22:52:50 +01:00
- variable:
name: my_variable
type: choice
choice:
- text: val1
- text: val2
- text: val3
value:
- text: val1
```
2021-05-05 15:27:06 +02:00
## Un variable à choix typée
Par défaut les choix sont de type "string". Il est possible de préciser des nombres, des booléens ou la valeur None :
2022-11-03 22:17:43 +01:00
```xml
2021-05-05 15:27:06 +02:00
<variable name="my_variable" type="choice">
2021-12-11 16:33:55 +01:00
<choice>val1</choice>
<choice type="string">val2</choice>
<choice type="number">3</choice>
<choice type="boolean">True</choice>
<choice type="nil"/>
2021-05-05 15:27:06 +02:00
</variable>
```
2022-11-02 22:52:50 +01:00
En YAML :
2022-11-03 22:17:43 +01:00
```yml
2022-11-02 22:52:50 +01:00
- variable:
name: my_variable
type: choice
choice:
- text: val1
- type: string
text: val2
- type: number
2022-11-04 19:42:16 +01:00
text: 3
2022-11-02 22:52:50 +01:00
- type: boolean
2022-11-03 22:16:52 +01:00
text: true
2022-11-02 22:52:50 +01:00
- type: 'nil'
```
2021-05-05 15:27:06 +02:00
Comme vu précédement ajouter la valeur None n'est pas utile parce qu'elle est automatiquement ajouté si la variable n'est pas obligatoire.
## Ajouter une option à une variable à choix existante
Pour ajouter un choix à une variable à choix existante, rien de plus simple, juste redéfinir la variable en ajoutant le choix voulu :
2022-11-03 22:17:43 +01:00
```xml
2021-05-05 15:27:06 +02:00
<variable name="my_variable" redefine="True">
2021-12-11 16:33:55 +01:00
<choice>val4</choice>
2021-05-05 15:27:06 +02:00
</variable>
```
2022-11-02 22:52:50 +01:00
En YAML :
2022-11-03 22:17:43 +01:00
```yml
2022-11-02 22:52:50 +01:00
- variable:
name: my_variable
2022-11-03 22:16:52 +01:00
redefine: true
2022-11-02 22:52:50 +01:00
choice:
- text: val4
```
2021-05-05 15:27:06 +02:00
## Redéfinir une option à choix
Si on veut supprimer un choix ou redéfinir complètement la liste, il faut redéfinir cette variable et ajouter l'attribut "remove_choice" à "True" :
2022-11-03 22:17:43 +01:00
```xml
2021-05-05 15:27:06 +02:00
<variable name="my_variable" redefine="True" remove_choice="True">
2021-12-11 16:33:55 +01:00
<choice>val1</choice>
<choice>val2</choice>
2021-05-05 15:27:06 +02:00
</variable>
```
2022-11-02 22:52:50 +01:00
En YAML :
2022-11-03 22:17:43 +01:00
```yml
2022-11-02 22:52:50 +01:00
- variable:
name: my_variable
2022-11-03 22:16:52 +01:00
redefine: true
remove_choice: true
2022-11-02 22:52:50 +01:00
choice:
- text: val1
- text: val2
```
2021-05-05 15:27:06 +02:00
Dans ce cas toutes les anciens choix ne seront plus possible. Seuls les nouveaux le seront.
## Un variable à choix provenant d'une variable
Une variable à valeur multiple peut servir de source des choix :
2022-11-03 22:17:43 +01:00
```xml
2021-05-05 15:27:06 +02:00
<variable name="my_variable" type="choice">
2021-12-11 16:33:55 +01:00
<choice type="variable">other_variable</choice>
2021-05-05 15:27:06 +02:00
</variable>
```
2022-11-02 22:52:50 +01:00
En YAML :
2022-11-03 22:17:43 +01:00
```yml
2022-11-02 22:52:50 +01:00
- variable:
name: my_variable
type: choice
choice:
- type: variable
text: other_variable
```
2021-05-05 15:27:06 +02:00
Dans ce cas, toutes les valeurs de la variable seront des choix utilisables par l'utilisateur.
Seul un choice de type "variable" est possible par variable.
## Un variable à choix provenant d'une fonction
2022-11-03 22:17:43 +01:00
```xml
2021-05-05 15:27:06 +02:00
<variable name="my_variable" type="choice">
2022-11-02 22:52:50 +01:00
<choice type="function" name="range">
2021-12-11 16:33:55 +01:00
<param type="number">0</param>
<param type="number">10</param>
</choice>
<value type="number">9</value>
2021-05-05 15:27:06 +02:00
</variable>
```
2022-11-02 22:52:50 +01:00
En YAML :
2022-11-03 22:17:43 +01:00
```yml
2022-11-02 22:52:50 +01:00
- variable:
name: my_variable
type: choice
choice:
- type: function
name: range
param:
- type: number
2022-11-04 19:42:16 +01:00
text: 0
2022-11-02 22:52:50 +01:00
- type: number
2022-11-04 19:42:16 +01:00
text: 10
2022-11-02 22:52:50 +01:00
value:
- type: number
2022-11-04 19:42:16 +01:00
text: 9
2022-11-02 22:52:50 +01:00
```