google / google/python-fire

Allow choices restriction

未關閉
#384 8 則留言 22 個 reaction 已指派 0 人 在 GitHub 檢視
enhancement
主要語言
Python
星號
28.2k
分支
1.5k
PR 合併指標
30 天內沒有已合併 PR

描述

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/)

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。