Last active
December 8, 2023 19:22
-
-
Save jefftriplett/b946af97ccc1a59c0fb116f506c5a353 to your computer and use it in GitHub Desktop.
What `hello.py world` looks like in vanilla python, as a django management command, and using the Typer library to show a basic example of using Python Types.
This file contains 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
import sys | |
def main(): | |
# Check if a name argument is provided | |
if len(sys.argv) > 1: | |
name = sys.argv[1] | |
else: | |
name = "World" # Default name if no argument is provided | |
print(f"Hello, {name}!") | |
if __name__ == "__main__": | |
main() |
This file contains 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
from django.core.management.base import BaseCommand, CommandError | |
import argparse | |
class Command(BaseCommand): | |
def add_arguments(self, parser): | |
parser.add_argument('name', nargs='?', type=str, default='World') | |
def handle(self, *args, **options): | |
name = options['name'] | |
print(f'Hello, {name}!') |
This file contains 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
import typer | |
def main(name: str = "World"): | |
print(f"Hello, {name}!") | |
if __name__ == "__main__": | |
typer.run(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment