add help command

This commit is contained in:
James Greenwood 2025-08-30 12:08:07 +01:00
parent 5349cd2fc9
commit 1146ef44e1
2 changed files with 14 additions and 1 deletions

View File

@ -7,6 +7,7 @@ class FifthStack:
def __init__(self): def __init__(self):
self.stack: list[int] = [] self.stack: list[int] = []
self.commands: dict[str, Callable] = { self.commands: dict[str, Callable] = {
"help": self.help,
"push": self.push, "push": self.push,
"pop": self.pop, "pop": self.pop,
"swap": self.swap, "swap": self.swap,
@ -38,6 +39,10 @@ class FifthStack:
print(f"ERROR: division by zero") print(f"ERROR: division by zero")
self.stack.extend([a, b]) self.stack.extend([a, b])
def help(self):
print("Available commands:", ", ".join(cmd.upper() for cmd in self.commands.keys()))
print("Available operations:", ", ".join(self.binary_ops.keys()))
def push(self, value): def push(self, value):
try: try:
self.stack.append(int(value)) self.stack.append(int(value))
@ -80,6 +85,7 @@ class FifthStack:
def main(): def main():
fifth = FifthStack() fifth = FifthStack()
fifth.help()
while True: while True:
print(f"stack is {fifth.stack}") print(f"stack is {fifth.stack}")

View File

@ -147,7 +147,14 @@ def test_execute_dup_command():
assert stack.stack == [7, 7] assert stack.stack == [7, 7]
# Test edge cases # Help and edge cases
def test_help(capsys):
stack = FifthStack()
stack.help()
captured = capsys.readouterr()
assert "Available commands:" in captured.out
assert "Available operations:" in captured.out
def test_execute_empty_command(): def test_execute_empty_command():
stack = FifthStack() stack = FifthStack()
stack.execute("") # Should do nothing stack.execute("") # Should do nothing