refactor: 统一CLI输出格式为标准JSON

This commit is contained in:
OpenClaw Agent
2026-02-11 01:10:45 +08:00
parent 06f279451a
commit df15f9a6fb
5 changed files with 134 additions and 50 deletions
+19 -7
View File
@@ -204,7 +204,6 @@ async def main():
parser.add_argument('--type', '-t', required=True, choices=['video', 'subtitle', 'article'], help='内容类型')
parser.add_argument('--output', '-o', default='./output', help='输出目录')
parser.add_argument('--quality', '-q', type=int, default=32, help='视频质量: 16=360P, 32=480P, 64=720P, 80=1080P')
parser.add_argument('--json', action='store_true', help='JSON输出')
args = parser.parse_args()
@@ -218,13 +217,23 @@ async def main():
else:
result = await downloader.extract_article(args.id.replace('cv', ''))
if args.json:
print(json.dumps(result, ensure_ascii=False, indent=2))
# 统一输出格式
if result.get("success"):
output = {
"success": True,
"type": args.type,
"filename": Path(result.get('filepath', '')).name if result.get('filepath') else None,
"filepath": str(Path(result.get('filepath', '')).resolve()) if result.get('filepath') else None,
"message": result.get('message', '处理成功')
}
else:
print(f"\n{result['message']}")
if result.get('filepath'):
print(f"文件: {result['filepath']}")
output = {
"success": False,
"error": result.get('message', '处理失败'),
"detail": f"ID: {args.id}, 类型: {args.type}"
}
print(json.dumps(output, ensure_ascii=False, indent=2))
return result
@@ -233,5 +242,8 @@ if __name__ == '__main__':
result = asyncio.run(main())
sys.exit(0 if result.get("success") else 1)
except KeyboardInterrupt:
print("\n已取消")
print(json.dumps({"success": False, "error": "用户取消"}, ensure_ascii=False, indent=2))
sys.exit(1)
except Exception as e:
print(json.dumps({"success": False, "error": str(e)}, ensure_ascii=False, indent=2))
sys.exit(1)