From e4ab8a6c4093ae4fc8bd05e1db2ef5003a61e87f Mon Sep 17 00:00:00 2001 From: oneachina Date: Sat, 29 Mar 2025 12:48:29 +0800 Subject: [PATCH 1/3] fix: resolve unterminated string error in command execution -> getattr --- main.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 63be3a8..5fcec31 100644 --- a/main.py +++ b/main.py @@ -117,9 +117,9 @@ def __init__(self, parent=None): def process_command(self, line_text): try: - result: list | tuple = line_text.split(' ') - command: str = result[0] - args: list | tuple = result[1:] + result = line_text.split(' ') + command = result[0] + args = result[1:] if command in self.COMMANDS: method_name = self.COMMANDS[command] @@ -127,18 +127,22 @@ def process_command(self, line_text): sys.exit() else: try: - processed_args = [] - for arg in args: - processed_args.append(f'"{str(arg)}"') - eval(f"self.{method_name}({ \ - ', '.join(processed_args)})") - except NameError as e: + method = getattr(self, method_name) + # 直接传递参数列表,无需字符串处理 + method(*args) + except AttributeError: self.error([CMD_NOT_FOUND, COLON, command]) except TypeError as e: - if re.search(r'takes \d+ positional argument', str(e)): - self.error([command, COLON, LARGE_ARG]) - elif re.search(r'missing \d+ required positional argument', str(e)): - self.error([command, COLON, MISS_ARG]) + error_msg = str(e) + if "positional argument" in error_msg: + if "missing" in error_msg: + self.error([command, COLON, MISS_ARG]) + elif "takes" in error_msg: + self.error([command, COLON, LARGE_ARG]) + else: + self.error([str(e)]) + except Exception as e: + self.error([str(e)]) elif self.in_path(command): if os.name == 'nt': executable = 'cmd' @@ -251,8 +255,7 @@ def help(self): ls: 列出目录下的文件和目录 - ls = dir help: 查看本消息 - help -日志保存位置在{self.home_directory}\.pcmd\下 -""" +日志保存位置在{self.home_directory}""" + r"""\.pcmd\下""" self.print(info) def cd(self, dir_name='.'): From 06d2723897ee5d8be49e8bf02fa1723bae82e564 Mon Sep 17 00:00:00 2001 From: oneachina Date: Sat, 29 Mar 2025 12:51:36 +0800 Subject: [PATCH 2/3] update README.md --- README.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/README.md b/README.md index d979a28..22b88ed 100644 --- a/README.md +++ b/README.md @@ -57,15 +57,6 @@ python3 main.py 日志存放位于`C:\users\你的用户名\.pcmd`下 #### Warning! - 无法自定义日志位置 -- cd 有部分问题 例子: -```bash -cd c:\ -unterminated string literal (detected at line 1); perhaps you escaped the end quote? (, line 1) -``` -请使用 -```bash -cd c:\\ -``` ### 文件说明 From 498b031f942ca96553032cb4c496f9f936faa0e5 Mon Sep 17 00:00:00 2001 From: oneachina Date: Sat, 29 Mar 2025 12:51:51 +0800 Subject: [PATCH 3/3] update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 22b88ed..2e90b7e 100644 --- a/README.md +++ b/README.md @@ -42,15 +42,15 @@ git clone https://github.com/CodeCrafter-TL/python-cmd.git #### 安装必需包 在 clone 之后的工作目录中执行如下命令: ```bash -pip3 install -r requirements.txt +pip install -r requirements.txt ``` 如果安装较慢,可以使用如下命令切换为阿里云镜像: ```bash -pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple +pip config set global.index-url https://mirrors.aliyun.com/pypi/simple ``` 等待安装完成后,使用如下命令即可开始使用 Python cmd: ```bash -python3 main.py +python main.py ``` #### 日志说明