Skip to content

Commit

Permalink
Added argparse for command-line argument handling
Browse files Browse the repository at this point in the history
This commit fixes/changes various problems
  • Loading branch information
veddevv authored Nov 4, 2024
1 parent 86ffe82 commit e5ce823
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions vgde.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import logging
import re
from argparse import ArgumentParser

# Configure logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
Expand Down Expand Up @@ -43,7 +44,7 @@ def sanitize_game_name(game_name):
if len(game_name) > 100:
raise InvalidInputError("Invalid input. Game name is too long.")

if not re.match("^[a-zA-Z0-9\s]+$", game_name):
if not re.match(r"^[a-zA-Z0-9\s]+$", game_name):
raise InvalidInputError("Invalid input. Game name contains invalid characters.")

return game_name
Expand Down Expand Up @@ -139,15 +140,18 @@ def main():
Main function to run the script.
Prompts the user to enter the name of a game and displays its information.
"""
parser = ArgumentParser(description="Fetch game information from RAWG API.")
parser.add_argument('game_name', type=str, help="The name of the game to search for.")
args = parser.parse_args()

try:
check_api_key()
except MissingAPIKeyError as e:
logging.error(f"API key error: {e}")
sys.exit(1)

try:
game_name = input("Enter the name of the game: ")
sanitized_game_name = sanitize_game_name(game_name)
sanitized_game_name = sanitize_game_name(args.game_name)
game_info = get_game_info(sanitized_game_name)
display_game_info(game_info)
except InvalidInputError as e:
Expand Down

0 comments on commit e5ce823

Please sign in to comment.