Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加命令行参数识别 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from tqdm import tqdm
import json # Import the json module

import getopt
import sys

def calculate_md5(file_path, block_size=8192):
md5 = hashlib.md5()
with open(file_path, 'rb') as f:
Expand Down Expand Up @@ -38,14 +41,34 @@ def save_to_json(file_list, output_file):
json.dump(json_data, f, indent=2)

def main():
# 获取用户输入的文件夹目录
directory = input("请输入文件夹目录:")

# 询问用户是否生成绝对路径
use_absolute_path = input("生成绝对路径(A)还是相对路径(R)? (A/R): ").upper() == "A"
command_mode = False
directory = ''
use_absolute_path = False
output_format = "JSON"

# 命令行参数获取
opts,args = getopt.getopt(sys.argv[1:],'cd:at',['command','directory=','absolute','txt'] )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用getopt.getopt来解析命令行参数。这里的参数定义应该与用户可能输入的选项相匹配。当前的实现已经覆盖了需求中提到的所有功能选项。然而,对于未来可能的扩展性和易用性考虑,可以考虑添加一个帮助选项(例如-h--help),用于显示可用命令行选项的说明。

+ opts, args = getopt.getopt(sys.argv[1:], 'hcd:at', ['help', 'command', 'directory=', 'absolute', 'txt'])
+ for opt_name, opt_value in opts:
+     if opt_name in ('-h', '--help'):
+         print("Usage: ...")  # 提供详细的使用说明
+         sys.exit()

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
opts,args = getopt.getopt(sys.argv[1:],'cd:at',['command','directory=','absolute','txt'] )
opts, args = getopt.getopt(sys.argv[1:], 'hcd:at', ['help', 'command', 'directory=', 'absolute', 'txt'])
for opt_name, opt_value in opts:
if opt_name in ('-h', '--help'):
print("Usage: ...") # 提供详细的使用说明
sys.exit()

for opt_name,opt_value in opts:
if opt_name in ('-c','--command'):
command_mode=True
if opt_name in ('-d','--directory'):
directory = opt_value
if opt_name in ('-a','--absolute'):
use_absolute_path=True
if opt_name in ('-t','--txt'):
output_format = "TXT"
Comment on lines +45 to +60
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main函数中添加了command_mode标志和处理命令行参数的逻辑。这些更改支持了目录指定、输出格式选择和绝对路径生成的新命令行参数。需要注意的是,getopt.getopt函数的异常处理未被包含,当提供无效的命令行参数时,程序可能会抛出getopt.GetoptError异常。

+ try:
-    opts,args = getopt.getopt(sys.argv[1:],'cd:at',['command','directory=','absolute','txt'] )
+    opts, args = getopt.getopt(sys.argv[1:], 'cd:at', ['command', 'directory=', 'absolute', 'txt'])
+ except getopt.GetoptError as e:
+    print(e)
+    sys.exit(2)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
command_mode = False
directory = ''
use_absolute_path = False
output_format = "JSON"
# 命令行参数获取
opts,args = getopt.getopt(sys.argv[1:],'cd:at',['command','directory=','absolute','txt'] )
for opt_name,opt_value in opts:
if opt_name in ('-c','--command'):
command_mode=True
if opt_name in ('-d','--directory'):
directory = opt_value
if opt_name in ('-a','--absolute'):
use_absolute_path=True
if opt_name in ('-t','--txt'):
output_format = "TXT"
command_mode = False
directory = ''
use_absolute_path = False
output_format = "JSON"
# 命令行参数获取
try:
opts, args = getopt.getopt(sys.argv[1:], 'cd:at', ['command', 'directory=', 'absolute', 'txt'])
except getopt.GetoptError as e:
print(e)
sys.exit(2)
for opt_name,opt_value in opts:
if opt_name in ('-c','--command'):
command_mode=True
if opt_name in ('-d','--directory'):
directory = opt_value
if opt_name in ('-a','--absolute'):
use_absolute_path=True
if opt_name in ('-t','--txt'):
output_format = "TXT"


if command_mode != True:
# 获取用户输入的文件夹目录
directory = input("请输入文件夹目录:")

# 询问用户是否生成绝对路径
use_absolute_path = input("生成绝对路径(A)还是相对路径(R)? (A/R): ").upper() == "A"

# 询问用户选择生成JSON还是TXT
output_format = input("生成文件格式 (JSON/TXT): ").upper()
# 询问用户选择生成JSON还是TXT
output_format = input("生成文件格式 (JSON/TXT): ").upper()
Comment on lines +62 to +70
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在非命令行模式下,通过input函数获取用户输入。这部分逻辑确保了当不通过命令行参数运行脚本时,用户依然可以交互式地指定目录、是否生成绝对路径以及输出格式。然而,这里没有对用户输入进行任何验证,可能会导致程序在无效输入的情况下出现错误。

建议添加输入验证逻辑,确保用户输入的目录存在且可访问,以及用户选择的输出格式是支持的格式。



# 生成文件列表及MD5值
file_list_with_md5 = generate_file_list_with_md5(directory, use_absolute_path)
Expand Down