-
-
Notifications
You must be signed in to change notification settings - Fork 746
✨ 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
Open
sirex
wants to merge
33
commits into
fastapi:master
Choose a base branch
from
sirex:enum-names
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
afc9cf4
Add option to use Enum names
sirex 2e079f2
Fix black issues
sirex b0f534e
Fix isort issues
sirex 2a18e31
Merge branch 'master' into enum-names
svlandeg 5769c4a
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] f4a99b2
rename 004 to 005
svlandeg 0ce6ade
Merge branch 'enum-names' of https://github.com/sirex/typer into enum…
svlandeg 5d63dbd
restore the original 003 as 004
svlandeg 9a00cd9
Merge branch 'master' into enum-names
svlandeg 2c9192f
Merge branch 'master' into enum-names
svlandeg ad046fc
Merge branch 'master' into enum-names
svlandeg a77dddd
fix issues
svlandeg dfaf7b3
rename to enum_by_name and fix the convertor code order
svlandeg d77ac5b
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] c39a5ea
Add console example for IntEnum
svlandeg b77a29f
Merge remote-tracking branch 'upstream_sirex/enum-names' into enum-names
svlandeg 8312bb8
Fix default values
svlandeg b5648ed
Add additional unit tests combining enums with list/tuple
svlandeg e24f446
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 0644919
pass along enum_by_name parameter to generate_X_convertor functions
svlandeg 478d183
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] eee3a4c
add Annotated versions of the new tests
svlandeg b5bd589
Merge remote-tracking branch 'upstream_sirex/enum-names' into enum-names
svlandeg e2053b1
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 221a865
ignore 006 tutorial just like 003 (mutable default argument)
svlandeg 7b59934
update enum.md to use the annotated versions as well
svlandeg 5759360
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 87ae5ad
add tests with Argument
svlandeg e13a083
Merge remote-tracking branch 'upstream_sirex/enum-names' into enum-names
svlandeg adb7c03
fix printing of enum value (needed for Python 3.11 and 3.12)
svlandeg 9ad26c2
remove lowercasing from generator function - should be done with case…
svlandeg 6ae6c9b
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] bfae3ef
fix hl_lines
svlandeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
docs_src/multiple_values/arguments_with_multiple_values/tutorial003.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from enum import Enum | ||
from typing import Tuple | ||
|
||
import typer | ||
|
||
|
||
class SuperHero(str, Enum): | ||
hero1 = "Superman" | ||
hero2 = "Spiderman" | ||
hero3 = "Wonder woman" | ||
|
||
|
||
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__": | ||
typer.run(main) |
32 changes: 32 additions & 0 deletions
32
docs_src/multiple_values/arguments_with_multiple_values/tutorial003_an.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from enum import Enum | ||
from typing import Tuple | ||
|
||
import typer | ||
from typing_extensions import Annotated | ||
|
||
|
||
class SuperHero(str, Enum): | ||
hero1 = "Superman" | ||
hero2 = "Spiderman" | ||
hero3 = "Wonder woman" | ||
|
||
|
||
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__": | ||
typer.run(main) |
25 changes: 25 additions & 0 deletions
25
docs_src/multiple_values/options_with_multiple_values/tutorial002.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from enum import Enum | ||
from typing import Tuple | ||
|
||
import typer | ||
|
||
|
||
class Food(str, Enum): | ||
f1 = "Eggs" | ||
f2 = "Bacon" | ||
f3 = "Cheese" | ||
|
||
|
||
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__": | ||
typer.run(main) |
33 changes: 33 additions & 0 deletions
33
docs_src/multiple_values/options_with_multiple_values/tutorial002_an.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from enum import Enum | ||
from typing import Tuple | ||
|
||
import typer | ||
from typing_extensions import Annotated | ||
|
||
|
||
class Food(str, Enum): | ||
f1 = "Eggs" | ||
f2 = "Bacon" | ||
f3 = "Cheese" | ||
|
||
|
||
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__": | ||
typer.run(main) |
29 changes: 29 additions & 0 deletions
29
docs_src/multiple_values/options_with_multiple_values/tutorial003.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from enum import Enum | ||
from typing import Tuple | ||
|
||
import typer | ||
|
||
|
||
class Food(str, Enum): | ||
f1 = "Eggs" | ||
f2 = "Bacon" | ||
f3 = "Cheese" | ||
|
||
|
||
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__": | ||
typer.run(main) |
33 changes: 33 additions & 0 deletions
33
docs_src/multiple_values/options_with_multiple_values/tutorial003_an.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from enum import Enum | ||
from typing import Tuple | ||
|
||
import typer | ||
from typing_extensions import Annotated | ||
|
||
|
||
class Food(str, Enum): | ||
f1 = "Eggs" | ||
f2 = "Bacon" | ||
f3 = "Cheese" | ||
|
||
|
||
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__": | ||
typer.run(main) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import enum | ||
import logging | ||
|
||
import typer | ||
|
||
|
||
class LogLevel(enum.Enum): | ||
debug = logging.DEBUG | ||
info = logging.INFO | ||
warning = logging.WARNING | ||
|
||
|
||
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__": | ||
typer.run(main) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import enum | ||
import logging | ||
|
||
import typer | ||
from typing_extensions import Annotated | ||
|
||
|
||
class LogLevel(enum.Enum): | ||
debug = logging.DEBUG | ||
info = logging.INFO | ||
warning = logging.WARNING | ||
|
||
|
||
def main(log_level: Annotated[LogLevel, typer.Option(enum_by_name=True)] = "warning"): | ||
typer.echo(f"Log level set to: {logging.getLevelName(log_level.value)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import enum | ||
|
||
import typer | ||
|
||
|
||
class Access(enum.IntEnum): | ||
private = 1 | ||
protected = 2 | ||
public = 3 | ||
open = 4 | ||
|
||
|
||
def main(access: Access = typer.Option("private", enum_by_name=True)): | ||
typer.echo(f"Access level: {access.name} ({access.value})") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import enum | ||
|
||
import typer | ||
from typing_extensions import Annotated | ||
|
||
|
||
class Access(enum.IntEnum): | ||
private = 1 | ||
protected = 2 | ||
public = 3 | ||
open = 4 | ||
|
||
|
||
def main(access: Annotated[Access, typer.Option(enum_by_name=True)] = "private"): | ||
typer.echo(f"Access level: {access.name} ({access.value})") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.