google / google/python-fire

Allow choices restriction

Abierto
#384 8 comentarios 22 reacciones 0 asignados Ver en GitHub
enhancement
Lenguaje dominante
Python
Estrellas
28.2k
Forks
1.5k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

Hello, and thank you for this great CLI!
Recently I get to a situation when I would like to restrict the options for a given argument similar to build-in `argparse` does with its option `choices` (see docs: https://docs.python.org/3/library/argparse.html#choices). Then I was checking Fire docs but could not find anything similar to it...
Checking alternative CLI packages I found a way that is quite simple but still elegant and would well fit the Fire style. It is leveraging python `Enum` class:
```py
from enum import Enum
import fire

class Direction(str, Enum):
up = "up"
down = "down"
left = "left"
right = "right"

def main(move: Direction = Direction.left):
print(f"Moving in given direction: {move.value}")

if __name__ == "__main__":
fire.Fire(main)
```
For clarification, the example above is borrowed and adjusted from [Typer/enum](https://typer.tiangolo.com/tutorial/parameter-types/enum/)

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.