-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
添加命令行参数识别 #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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'] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在 + 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在非命令行模式下,通过 建议添加输入验证逻辑,确保用户输入的目录存在且可访问,以及用户选择的输出格式是支持的格式。 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# 生成文件列表及MD5值 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
file_list_with_md5 = generate_file_list_with_md5(directory, use_absolute_path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用
getopt.getopt
来解析命令行参数。这里的参数定义应该与用户可能输入的选项相匹配。当前的实现已经覆盖了需求中提到的所有功能选项。然而,对于未来可能的扩展性和易用性考虑,可以考虑添加一个帮助选项(例如-h
或--help
),用于显示可用命令行选项的说明。Committable suggestion