[英]How to get unlimited multiple options from typer.Option with custom devider
我想在typer.Option
中使用自定義分隔符划分無限值。 以下是我需要的示例:
$ python main.py add -C "first one","second one","third"
和 output 應該是:
["first one","second one","third"]
有什么辦法可以用 typer 和 python 做到這一點嗎?
要獲得無限值的選項,您可以獲得一個字符串並用分隔符將其拆分並獲得一個列表。
你可以試試這個:
import typer
app = typer.Typer()
@app.callback()
def main():
pass
@app.command()
def add(elements: str = typer.Option(..., help="List of elements separated by a comma")):
elements_splited = [element.strip() for element in elements.split(",")]
typer.echo(elements_splited)
if __name__ == "__main__":
app()
所以:
python main.py 添加 --elements "el1, el2, el3, el4"
你會得到:
['el1', 'el2', 'el3', 'el4']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.