Skip to content

Instantly share code, notes, and snippets.

@Pinacolada64
Created August 14, 2021 07:16
Show Gist options
  • Select an option

  • Save Pinacolada64/bfd74605db286a561aa8f6fa432c68b2 to your computer and use it in GitHub Desktop.

Select an option

Save Pinacolada64/bfd74605db286a561aa8f6fa432c68b2 to your computer and use it in GitHub Desktop.
def tada_parser(last_command=None):
logging.info("tada_parser() called")
if last_command is None:
last_command = "help"
print(f"Return: {last_command}")
command_line = input("Command: ")
if not command_line:
logging.info("Empty command line, setting to '{last_command}")
command_line = last_command
logging.info(f"You typed: {command_line}")
last_command = command_line
params = command_line.split(' ')
logging.info("last_command = %s" % last_command)
logging.info("base_command = %s" % params[0])
logging.info("params: %s\n" % len(params[1:]))
# figure out how many params (1-nums) are in the list "command_line"
if params > 0:
for num in 1 to len(params):
param_string = params[num]
logging.info("%2d. %s", num, param_string)
# n = 0
# for num in command_line.count():
# print(f"{num}. {command_line[num]}")
# is the command typed in the command dict?
if command_line[0] in commands:
return last_command
else:
logging.info("command '%s' not in dict", command_line[0])
return None
def game_help(topic=None):
"""
This displays help for the game.
topics will be supported (like "Bad Hombre Rating", or "BHR" for short).
"""
print("help")
print("quit")
def dispatch(routine):
if callable(routine[0]):
print(routine[0](__doc__))
else:
print(f"{routine[0]} is not a valid command.\n")
last_command = None
if __name__ == '__main__':
"""https://stackoverflow.com/questions/9759534/how-to-break-from-the-while-loop-for-python-string-type"""
import logging
logging.basicConfig(level="INFO")
"""Commands are in a dict. key=command to type, value=function to call"""
commands = {("h", "help"): "game_help", ("l", "look"): "look"}
command_line = None
while True:
command_line = tada_parser()
print(f"command_line[0]={command_line[0]}")
if command_line[0] == "quit":
break
print(f"command line: {command_line}")
# print(repr(command_line))
print("back into the loop\n")
print("'quit' received")
# dispatch(command_line[0]) # pick first word out of command line list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment