开始
内置处理器通常使用的是未开放的接口,如直接调用处理器管理器的方法;这些在用户处理器中是不会使用的,但可以由脚本提供。
目前有五个内置处理器:
- Echo 常用来查看关键字的值和测试。
- Exit 退出程序
- Hist 管理历史模块使用的本地文件
- Meow 获取当前注册的处理器及调用处理器的Echo方法
- Exec 接受一条命令并执行
这五个处理器都具有独特的作用,用户处理器可以调用它们的方法获得更多的灵活。
实现
下面,一一介绍它们的设计与实现。
Echo
它的目的很简单,实现也十分简单:只是将参数都连接起来,方便调试。
#Include ../baseHandle.ahk class Echo extends BaseHandle { static nullable := true static Handle(parsed) { for v in parsed.params t .= '-' v ' ' for k, v in parsed.kvparams.OwnProps() IsArray(v) ? t .= Format('-{}=[{}] ', k, v.Join()) : t .= Format('-{}={} ', k, v) t .= Array(parsed.target, parsed.extra*).Join(A_Space) return this.Succ(t) } static Echo() => ' ( echo 内置命令。 echo [target] 回显输入。 )' }
Exit
这个命令实际使用的很少,我一直都是Esc直接退出,而不会敲这个命令。
它的实现借助于调用方,会返回一个标志表示执行完命令后退出程序;这在其他处理器中也十分常用,并不独特。
#Include ../baseHandle.ahk class Exit_ extends BaseHandle { static nullable := true static Handle(*) => this.Succ('bye.', 'x') static Echo() => ' ( exit 内置命令。 exit [] 退出脚本。 )' }
Hist
此命令用来管理命令历史,只支持两种操作:打开本地文件和清空本地文件。
#Include ../../core/historyMgr.ahk #Include ../baseHandle.ahk class Hist extends baseHandle { static Handle(parsed) { switch parsed.target { case 'open': History.OpenFile() case 'clear': History.Clear() default: return this.Fail('无效的操作') } return this.Succ('ok', 'x') } static Echo() => ' ( hist 内置命令。 hist [open|clear] open 打开历史命令文件。 clear 清空历史命令。 )' }
Meow
当没有执行目标时,显示所有注册的处理器;否则回显处理器的帮助信息。
#Include ../baseHandle.ahk #Include ../../core/handleMgr.ahk class Meow extends BaseHandle { static nullable := true static Handle(parsed) { if Mgr.h.Has(parsed.target) return this.Succ(Mgr.h.Get(parsed.target).Echo()) switch parsed.target { case '': return this.Succ(Mgr.h.Keys.Join(',') '(' Mgr.h.Count ')') case 'conf': return Mgr.h.Has(e := parsed.Extra[1]) ? this.Succ(Mgr.h.Get(e).Conf()) : this.Fail('不存在的处理器:' e) default: return this.Fail('不支持的操作') } } static Echo() => ' ( meow 内置命令。 meow [cmd] 无目标将回显所有注册命令; 否则,显示目标帮助文档。 )' }
Exec
它调用
Mgr
的方法来实现功能
处理器的主要目的是给用户提供接口
此命令可以执行一条已注册的命令,也就是说可以编程式的运行命令了。
它也可以调用异步处理器,但是会转化成同步调用。
#Include ../baseHandle.ahk #Include ../../core/handleMgr.ahk class Exec extends BaseHandle { static Handle(parsed) { if (r := Mgr.Check(parsed.target)).valid { if r.handler.Prototype.__Class = 'Exec' return this.Fail('Recursive calls(exec)') echo := Mgr.Call(r.handler, r.parsed) if echo.extra = MeowTool.asyncFlag { res := '' echo.r.done := (_, _r, flag) => res := { flag: flag, r: _r } echo.r.succ := (_, _r) => res := { flag: true, r: _r } echo.r.fail := (_, _r) => res := { flag: false, r: _r } echo.r.Call() return this.Done(res.r, res.flag) } else return this.Succ(echo.r) } return this.Fail(r.msg) } static Echo() => ' ( exec 内置命令。 exit target 执行一条注册命令,不可调用自己。 )' }
使用示例
这是一个自定义处理器使用此内置处理器的示例。
它增强了内置的exec命令,可以使用符号来连接命令,达到灵活的命令组合效果:
;
表示顺序执行|
表示将上条命令结果作为下条目标&
表示上条命令才执行^
表示上条命令失败才执行
例子:echo t | echo & echo done!
#Include ../buildin/exec.ahk #Include ../baseHandle.ahk class Exec2 extends BaseHandle { static Handle(parsed) { target := parsed.raw.substring(parsed.which.Length + 1) ; 避免频繁的转义 cs := target.toCharArray() ec := '\', qc := "'", i := 1, _q := false, s := '', r := [], cmds := [], ops := [] while i <= cs.Length { esc := false if cs[i] = ec esc := !esc, i++ if !esc and cs[i] = qc { _q := !_q, i++ continue } if !_q and !esc { switch cs[i] { case '|': cmds.Push(s.Trim()), ops.Push(0), s := '' case '&': cmds.Push(s.Trim()), ops.Push(1), s := '' case '^': cmds.Push(s.Trim()), ops.Push(2), s := '' case ';': cmds.Push(s.Trim()), ops.Push(3), s := '' default: s .= cs[i] } } else s .= cs[i] i++ } if !(s := Trim(s)) return this.Fail('empty element(' i - 1 ')') cmds.Push(s) last := Exec.Handle({ target: cmds[1] }), _i := 2 r.Push(Format('{}({})-{}', last.flag ? 'Y' : 'N', r.Length + 1, last.r)) for i, v in ops { switch v { case 0: last := Exec.Handle({ target: cmds[_i++] ' ' last.r }) case 1: if !last.flag { r.Push('Interrupted') return this.Succ(r.Join('`n')) } last := Exec.Handle({ target: cmds[_i++] }) case 2: if last.flag { r.Push('Interrupted') return this.Succ(r.Join('`n')) } last := Exec.Handle({ target: cmds[_i++] }) case 3: last := Exec.Handle({ target: cmds[_i++] }) } r.Push(Format('{}({})-{}', last.flag ? 'Y' : 'N', r.Length + 1, last.r)) } return this.Succ(r.Join('`n')) } static Echo() => ' ( e e target 增强内置Exec命令。 )' }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期