Skip to content

Commit 4893743

Browse files
committed
fix(cli): 完善旧用法兼容,astr -j/--log等flag自动路由到子命令
- astr -j "你好" → 自动路由到 astr send -j "你好" - astr -t 60 "你好" → 自动路由到 astr send -t 60 "你好" - astr --log → 自动路由到 astr log - 更新帮助文本,展示新旧两种用法
1 parent 11b0ea7 commit 4893743

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

astrbot/cli/client/__main__.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,21 +430,22 @@ def fix_git_bash_path(message: str) -> str:
430430

431431
EPILOG = """使用示例:
432432
发送消息:
433-
astr 你好 直接发送(兼容旧用法)
434-
astr send 你好 发送消息给 AstrBot
433+
astr 你好 发送消息给 AstrBot
434+
astr send 你好 同上(显式子命令)
435435
astr send /help 查看内置命令帮助
436-
echo "你好" | astr send 从标准输入读取
436+
echo "你好" | astr 从标准输入读取
437437
438438
获取日志:
439439
astr log 获取最近 100 行日志
440+
astr --log 同上(兼容旧用法)
440441
astr log --lines 50 获取最近 50 行
441442
astr log --level ERROR 只显示 ERROR 级别
442443
astr log --pattern "CLI" 只显示包含 "CLI" 的日志
443444
astr log -j 以 JSON 格式输出日志
444445
445446
高级选项:
446-
astr send -j "测试" 输出原始 JSON 响应
447-
astr send -t 60 "长时间任务" 设置超时时间为 60 秒
447+
astr -j "测试" 输出原始 JSON 响应
448+
astr -t 60 "长时间任务" 设置超时时间为 60 秒
448449
449450
连接说明:
450451
自动从 data/.cli_connection 检测连接类型(Unix Socket 或 TCP)
@@ -462,10 +463,21 @@ def format_epilog(self, ctx: click.Context, formatter: click.HelpFormatter) -> N
462463
for line in self.epilog.split("\n"):
463464
formatter.write(line + "\n")
464465

466+
# send 子命令的 option 前缀,用于识别 astr -j "你好" 等旧用法
467+
_send_opts = {"-j", "--json", "-t", "--timeout", "-s", "--socket"}
468+
# --log 旧用法映射到 log 子命令
469+
_log_flag = {"--log"}
470+
465471
def parse_args(self, ctx: click.Context, args: list[str]) -> list[str]:
466-
# 兼容旧用法: astr 你好 等价于 astr send 你好
467-
if args and args[0] not in self.commands and not args[0].startswith("-"):
468-
args = ["send"] + args
472+
if args:
473+
first = args[0]
474+
if first in self._log_flag:
475+
# astr --log ... → astr log ...
476+
args = ["log"] + args[1:]
477+
elif first not in self.commands:
478+
if not first.startswith("-") or first in self._send_opts:
479+
# astr 你好 / astr -j "你好" → astr send ...
480+
args = ["send"] + args
469481
return super().parse_args(ctx, args)
470482

471483

0 commit comments

Comments
 (0)