-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.py
93 lines (79 loc) · 2.88 KB
/
shell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
"""
AuthorInformation
name (GitHub Username): DumbestPerson224
Repo (GitHub): https://github.com/DumbestPerson224/LinuxShellManager
"""
class CommandInterpreter:
def __init__(self):
"""Map commands to the corresponding method"""
self.commands = {"echo": self.handle_echo, "exit": self.handle_exit, "list": self.listcontent,
"help": self.list_commands}
"`isRunning` and `osMode` must only be booleans"
self.isRunning = True
self.osMode = False
def handle_echo(self, inp):
"""
FunctionInformation
Does: This function handles output
Parameter (any): inp
Outputs (any): inp
Type (string): Do not change
name (handle_echo): Do not change
"""
print(inp)
def list_commands(self):
"""
Function Information
Outputs (string): commands
FunctionName (Do not change): list_commands
Parameters (None): Do not change
"""
for command in self.commands:
print(command)
def listcontent(self):
"""
FunctionInformation
Outputs (no type): every file and folder in the current directory
Parameters (None): Do not change
FunctionName listcontent
"""
for filesAndFolders in os.listdir():
print(filesAndFolders)
def handle_exit(self):
"""
FunctionInformation
name (handle_exit): Do not change
Outputs (Do not change): Exitting.
isRunning (Do not change): False
"""
print("Exiting..")
self.isRunning = False
def handle_input(self, inp):
"""
FunctionInformation
name (handle_input): Do not change
Parameter inp
Returns inp as a string input stripped from any white space
Type (Do not change): any
"""
return input(str(inp.strip(" ")))
def validate_command(self, command) -> str:
"""
FunctionInformation:
name (validate_input): Do not change name
type (str): Do not change type
info (This boring function here takes in a paremeter called command and checks if it is a valid command): The information of the function
parameter (string): command
NoValidCommand (output): (The user input in caps) is not valid
"""
if command in self.commands and not command.startswith("echo"):
self.commands[command]()
elif command.startswith("echo"):
self.commands[command[4::].strip()]
else:
print(f"{command.upper()} is not valid")
commandInterpreter = CommandInterpreter()
while commandInterpreter.isRunning:
command = commandInterpreter.handle_input("> ").lower()
commandInterpreter.validate_command(command)