[英]How to get unlimited multiple options from typer.Option with custom devider
I want to divide unlimited values with a custom divider in typer.Option
.我想在
typer.Option
中使用自定义分隔符划分无限值。 below is an example of what I need:以下是我需要的示例:
$ python main.py add -C "first one","second one","third"
and output should be:和 output 应该是:
["first one","second one","third"]
is there any way to do this with typer and python?有什么办法可以用 typer 和 python 做到这一点吗?
to have an option with unlimited values, you can get a string and split it by a separator and get a list.要获得无限值的选项,您可以获得一个字符串并用分隔符将其拆分并获得一个列表。
You can try this:你可以试试这个:
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()
So:所以:
python main.py add --elements "el1, el2, el3, el4"
python main.py 添加 --elements "el1, el2, el3, el4"
You will get:你会得到:
['el1', 'el2', 'el3', 'el4']
['el1', 'el2', 'el3', 'el4']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.