main
import os
from cd import cd, path
from ls import *
while True:
cmd = input("$ ")
if 'cd' in cmd:
cmd_c0 = cmd.strip('cd ')
path = cd(cmd_c0)
if cmd.startswith('ls'):
# cmd_path = cmd.strip('ls ')
args, cmd_path = get_arg(cmd)
if args:
if cmd_path == '':
ls_path = path
else:
ls_path = cmd_path
# print(cmd_path)
for arg in args:
if arg == '-d':
d_(ls_path)
if arg == '-l':
l_(ls_path)
else:
if cmd_path:
dir_dic = os.listdir(cmd_path)
else:
dir_dic = os.listdir(path)
ls = " ".join(dir_dic)
print(ls)
ls 模块
import os
def get_arg(cmd) -> str:
arg_ls = []
cmd_arg_ls = cmd.split(' ')[1:]
cmd_arg_ls.append('')
for arg in cmd_arg_ls:
if arg.startswith('-'):
arg_ls.append(arg)
cmd_arg_ls.remove(arg)
# print(arg_ls)
# print(cmd_arg_ls)
return arg_ls, cmd_arg_ls[0]
def d_(path):
dir_path = os.path.dirname(path)
dir_name = dir_path.split('\\')[-1]
# print(dir_path)
print(dir_name)
# return dir_name
def l_(path):
return os.system(f'powershell dir {path}')
总体思路
总的思路来说是比较简单的,拿到输入的参数执行相应的功能。在获取参数的函数里,是通过分割参数实现的。需要注意的是关于路径的处理(本身没有输入路径参数怎么处理,输入了又怎么处理?),还需要考虑如何实现相关参数的功能。
用以致学