Skip to content

Commit 786d387

Browse files
committed
codecrafters submit [skip ci]
1 parent afaa512 commit 786d387

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

app/main.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@
44
import re
55
import shlex
66

7-
COMMANDS = ['exit', 'echo', 'type', 'pwd', 'cd', 'cat', 'exe']
7+
COMMANDS = {
8+
'exit': 'exit_command',
9+
'echo': 'echo_command',
10+
'type': 'type_command',
11+
'pwd': 'pwd_command',
12+
'cd': 'cd_command',
13+
'cat': 'cat_command',
14+
'exe': 'exe_command'
15+
}
816

917
path_variable = os.environ.get("PATH")
1018
path_list = path_variable.split(':')
1119

12-
def cat(splitted):
20+
def cat_command(splitted, command):
1321
"""
1422
This function handles the 'cat' command. It attempts to read files from
1523
the specified directories and prints their contents if they exist.
1624
"""
25+
splitted = shlex.split(command[4:])
1726
result = []
1827
for path in splitted:
1928
directory, target_file = os.path.split(path)
@@ -44,11 +53,12 @@ def cat(splitted):
4453
else:
4554
print("No files were found.")
4655

47-
def exe(splitted):
56+
def exe_command(splitted_command, command):
4857
"""
4958
This function handles the 'exe' command. It attempts to open and read the
5059
contents of the specified file.
5160
"""
61+
splitted = splitted_command[-1]
5262
try:
5363
with open(splitted, 'r') as file:
5464
contents = file.read()
@@ -58,18 +68,19 @@ def exe(splitted):
5868
except Exception as e:
5969
print(f"An error occurred: {e}")
6070

61-
def pwd():
71+
def pwd_command(splitted_command, command):
6272
"""
6373
This function handles the 'pwd' command. It prints the current working directory.
6474
"""
6575
current_path = os.getcwd()
6676
print(current_path)
6777

68-
def cd(new_path):
78+
def cd_command(splitted_command, command):
6979
"""
7080
This function handles the 'cd' command. It attempts to change the current
7181
directory to the specified path.
7282
"""
83+
new_path = splitted_command[1]
7384
if new_path == '~':
7485
home_directory = os.environ.get('HOME')
7586
os.chdir(home_directory)
@@ -78,15 +89,15 @@ def cd(new_path):
7889
else:
7990
print(f"cd: {new_path}: No such file or directory")
8091

81-
def echo(command):
92+
def echo_command(splitted_command, command):
8293
"""
8394
This function handles the 'echo' command. It splits the command and prints
8495
the arguments passed to echo.
8596
"""
8697
x = shlex.split(command, posix=True)
8798
print(" ".join(x[1:]))
8899

89-
def type_command(splitted_command):
100+
def type_command(splitted_command, command):
90101
"""
91102
This function handles the 'type' command. It checks whether the specified
92103
command is a shell builtin or a file and prints the corresponding message.
@@ -104,8 +115,15 @@ def type_command(splitted_command):
104115
main()
105116
print(f"{splitted_command[1]}: not found")
106117

118+
def exit_command(splitted_command, command):
119+
"""
120+
This function handles the 'exit' command and exits the program if the argument is '0'.
121+
"""
122+
if len(splitted_command) == 2 and splitted_command[1] == "0":
123+
exit(0)
124+
107125
def main():
108-
126+
109127
sys.stdout.write("$ ")
110128

111129
# Wait for user input
@@ -117,25 +135,10 @@ def main():
117135
if "_" in splitted_command[0] and len(splitted_command) == 2:
118136
# Execute a subprocess if the command is a valid executable
119137
subprocess.run([splitted_command[0], splitted_command[1]])
120-
elif splitted_command[0] not in COMMANDS:
138+
elif splitted_command[0] in COMMANDS:
139+
globals()[COMMANDS[splitted_command[0]]](splitted_command, command)
140+
else:
121141
print(f"{splitted_command[0]}: command not found")
122-
elif splitted_command[0] == "exe":
123-
splitted = splitted_command[-1]
124-
exe(splitted)
125-
elif splitted_command[0] == "cat":
126-
splitted = shlex.split(command[4:])
127-
cat(splitted)
128-
elif splitted_command[0] == "pwd":
129-
pwd()
130-
elif splitted_command[0] == "cd":
131-
new_path = splitted_command[1]
132-
cd(new_path)
133-
elif splitted_command[0] == "exit" and len(splitted_command) == 2 and splitted_command[1] == "0":
134-
exit(0)
135-
elif splitted_command[0] == "echo":
136-
echo(command)
137-
elif splitted_command[0] == "type":
138-
type_command(splitted_command)
139142

140143
main()
141144

0 commit comments

Comments
 (0)