简体   繁体   English

如何使用自定义开发者从 typer.Option 获得无限多个选项

[英]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.

相关问题 Python Typer 和 CLI 选项的多个输入 - Python Typer and multiple inputs to CLI option Python:如何删除 Typer CLI 上的默认选项? - Python: How to remove default options on Typer CLI? 从“typer”python || 读取多个单词为 arguments 如何使用“typer”“python”从cli读取未知数量的带有空格的字符串 - read multiple words as arguments from "typer" python || how to read a unknown number of strings with spaces from cli using "typer" "python" "如何获得 typer.Typer 应用程序根目录的 --version?" - How can I get a --version to the root of a typer.Typer application? 如何从多个 tkinter 选项菜单中获取值 - How can I get the values from multiple tkinter Option Menus 尝试从多个选项中选择 select,然后添加正确的选项 - 非常困惑 - Trying to select from multiple options, then add the correct option - very confused 从 twitter api 获取无限的推文 - Get unlimited tweets from twitter api 如何在Jira中获取自定义字段的选项? - How to get the options for a custom field in Jira? 如何从无限的Popen.stdout对象获取最后N行 - How to get the last N lines from an unlimited Popen.stdout object 当没有选项时,硒如何从下拉列表中选择选项 - how selenium select the option from dropdown list when there have no options
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM