Comments (2)
As far as i understand the intended way to use a list type parameter would be like this:
$ python app.py --command arg1 --command arg2
['arg1', 'arg2']
If you really want to use comma separated strings you could do the following:
from typing import Annotated
import typer
def comma_list(raw: str) -> list[str]:
return raw.split(",")
def main(
command: Annotated[list, typer.Option(parser=comma_list)],
):
print(command)
if __name__ == "__main__":
typer.run(main)
$ python potato/main.py --command "hello","world"
['hello', 'world']
Note that using list[str]
instead of list
for the command
parameter seems to attempt some type conversion magic resulting in a nested list so this isn't perfect. Personally I would just keep it simple and do something like this instead:
import typer
def main(commands: str = typer.Option()):
cmd_list = commands.split(",")
print(cmd_list)
if __name__ == "__main__":
typer.run(main)
from typer.
I implemented something quite similar in my cli
where it takes multiple values as part of an argument in a standard unix fashion i.e separated by spaces.
Essentially extending on what @heiskane is suggesting you do for Option
you can define an argument as follows:
from typing import Optional, List
from typing_extensions import Annotated
import typer
@app.command("ack")
async def acknowledge(
ids: Annotated[List[int], typer.Argument(help="alarm id")], #multiple ids
message: Annotated[
Optional[str],
typer.Option(
"-m",
"--message",
help="comment to add to history",
),
] = None,
):
for id in ids:
print(id)
See issue on my project for more detail.
from typer.
Related Issues (20)
- Documentation is misleading. new `typer.run` behavior HOT 2
- See if rich 13.x is compatible HOT 4
- Using `some_type | None` syntax for type annotations causes error in python 3.11 HOT 15
- TAB completion is giving local directory files where command is called. HOT 7
- Support for bytes in Options and Arguments HOT 3
- Is it possible to include a Prolog in `--help` HOT 2
- Get the typer output with html format to provide it to termynal HOT 5
- how to use typer on class method __init__ with self argument, got this error: Error: Missing argument 'SELF'. HOT 2
- Support for localization of messages HOT 1
- Auto-completion when application works in 2 modes (GUI, CLI) HOT 1
- DOC: Documentation of passing multiple values in "option" vs "argument" isn't sufficiently explicit HOT 2
- SIGINT from docker is ignored HOT 4
- 🚀 Roadmap HOT 3
- Source distribution of 0.11.0 is missing the `docs_src` folder HOT 1
- 🐛 Upgrading from `typer<0.12.0` to `typer==0.12.0` breaks the install by partially removing the package/module files HOT 4
- Rich markup in generated Markdown docs HOT 1
- Add CI configs to run tests on Windows and MacOS
- Rich markup in Zsh completion help lines HOT 1
- 22
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typer.