-
-
Notifications
You must be signed in to change notification settings - Fork 826
✨ Add option to use Enum names instead of values on the commandline #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
afc9cf4
2e079f2
b0f534e
2a18e31
5769c4a
f4a99b2
0ce6ade
5d63dbd
9a00cd9
2c9192f
ad046fc
a77dddd
dfaf7b3
d77ac5b
c39a5ea
b77a29f
8312bb8
b5648ed
e24f446
0644919
478d183
eee3a4c
b5bd589
e2053b1
221a865
7b59934
5759360
87ae5ad
e13a083
adb7c03
9ad26c2
6ae6c9b
bfae3ef
825b477
7f4f4e1
c0107fb
857e2ea
4558ebf
8582aee
727f06c
2a5e8c4
b33239e
3d6df84
a18aea0
82ae00a
d516837
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from enum import Enum | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class SuperHero(str, Enum): | ||
| hero1 = "Superman" | ||
| hero2 = "Spiderman" | ||
| hero3 = "Wonder woman" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| names: tuple[str, str, str, SuperHero] = typer.Argument( | ||
| ("Harry", "Hermione", "Ron", "hero3"), | ||
| enum_by_name=True, | ||
| case_sensitive=False, | ||
| help="Select 4 characters to play with", | ||
| ), | ||
| ): | ||
| for name in names: | ||
| if isinstance(name, Enum): | ||
| print(f"Hello {name.value}") | ||
| else: | ||
| print(f"Hello {name}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from enum import Enum | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class SuperHero(str, Enum): | ||
| hero1 = "Superman" | ||
| hero2 = "Spiderman" | ||
| hero3 = "Wonder woman" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| names: Annotated[ | ||
| tuple[str, str, str, SuperHero], | ||
| typer.Argument( | ||
| enum_by_name=True, | ||
| help="Select 4 characters to play with", | ||
| case_sensitive=False, | ||
| ), | ||
| ] = ("Harry", "Hermione", "Ron", "hero3"), | ||
| ): | ||
| for name in names: | ||
| if isinstance(name, Enum): | ||
| print(f"Hello {name.value}") | ||
| else: | ||
| print(f"Hello {name}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from enum import Enum | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A quick note that this tutorial file isn't actually referenced from anywhere within the tutorial markdown files. |
||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Food(str, Enum): | ||
| f1 = "Eggs" | ||
| f2 = "Bacon" | ||
| f3 = "Cheese" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(user: tuple[str, int, bool, Food] = typer.Option((None, None, None, Food.f1))): | ||
| username, coins, is_wizard, food = user | ||
| if not username: | ||
| print("No user provided") | ||
| raise typer.Abort() | ||
| print(f"The username {username} has {coins} coins") | ||
| if is_wizard: | ||
| print("And this user is a wizard!") | ||
| print(f"And they love eating {food.value}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from enum import Enum | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Food(str, Enum): | ||
| f1 = "Eggs" | ||
| f2 = "Bacon" | ||
| f3 = "Cheese" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| user: Annotated[tuple[str, int, bool, Food], typer.Option()] = ( | ||
| None, | ||
| None, | ||
| None, | ||
| Food.f1, | ||
| ), | ||
| ): | ||
| username, coins, is_wizard, food = user | ||
| if not username: | ||
| print("No user provided") | ||
| raise typer.Abort() | ||
| print(f"The username {username} has {coins} coins") | ||
| if is_wizard: | ||
| print("And this user is a wizard!") | ||
| print(f"And they love eating {food.value}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from enum import Enum | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A quick note that this tutorial file isn't actually referenced from anywhere within the tutorial markdown files. |
||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Food(str, Enum): | ||
| f1 = "Eggs" | ||
| f2 = "Bacon" | ||
| f3 = "Cheese" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| user: tuple[str, int, bool, Food] = typer.Option( | ||
| (None, None, None, "f1"), enum_by_name=True | ||
| ), | ||
| ): | ||
| username, coins, is_wizard, food = user | ||
| if not username: | ||
| print("No user provided") | ||
| raise typer.Abort() | ||
| print(f"The username {username} has {coins} coins") | ||
| if is_wizard: | ||
| print("And this user is a wizard!") | ||
| print(f"And they love eating {food.value}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from enum import Enum | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Food(str, Enum): | ||
| f1 = "Eggs" | ||
| f2 = "Bacon" | ||
| f3 = "Cheese" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| user: Annotated[tuple[str, int, bool, Food], typer.Option(enum_by_name=True)] = ( | ||
| None, | ||
| None, | ||
| None, | ||
| "f1", | ||
| ), | ||
| ): | ||
| username, coins, is_wizard, food = user | ||
| if not username: | ||
| print("No user provided") | ||
| raise typer.Abort() | ||
| print(f"The username {username} has {coins} coins") | ||
| if is_wizard: | ||
| print("And this user is a wizard!") | ||
| print(f"And they love eating {food.value}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import enum | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Access(enum.IntEnum): | ||
| private = 1 | ||
| protected = 2 | ||
| public = 3 | ||
| open = 4 | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(access: Access = typer.Option("private", enum_by_name=True)): | ||
| typer.echo(f"Access level: {access.name} ({access.value})") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import enum | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Access(enum.IntEnum): | ||
| private = 1 | ||
| protected = 2 | ||
| public = 3 | ||
| open = 4 | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(access: Annotated[Access, typer.Option(enum_by_name=True)] = "private"): | ||
| typer.echo(f"Access level: {access.name} ({access.value})") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from enum import Enum | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Food(str, Enum): | ||
| f1 = "Eggs" | ||
| f2 = "Bacon" | ||
| f3 = "Cheese" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(groceries: list[Food] = typer.Option(["f1", "f3"], enum_by_name=True)): | ||
| print(f"Buying groceries: {', '.join([f.value for f in groceries])}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from enum import Enum | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class Food(str, Enum): | ||
| f1 = "Eggs" | ||
| f2 = "Bacon" | ||
| f3 = "Cheese" | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| groceries: Annotated[list[Food], typer.Option(enum_by_name=True)] = ["f1", "f3"], | ||
| ): | ||
| print(f"Buying groceries: {', '.join([f.value for f in groceries])}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import enum | ||
| import logging | ||
|
|
||
| import typer | ||
|
|
||
|
|
||
| class LogLevel(enum.Enum): | ||
| debug = logging.DEBUG | ||
| info = logging.INFO | ||
| warning = logging.WARNING | ||
|
|
||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(log_level: LogLevel = typer.Option("warning", enum_by_name=True)): | ||
| typer.echo(f"Log level set to: {logging.getLevelName(log_level.value)}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick note that this tutorial file isn't actually referenced from anywhere within the tutorial markdown files.