gpt4 book ai didi

autoit - 调试 AutoIt 脚本或获取当前执行的脚本行号

转载 作者:行者123 更新时间:2023-12-02 03:24:06 27 4
gpt4 key购买 nike

我的 AutoIt 脚本发送一系列点击和按键来自动化旧的闭源应用程序。

它有错误,所以我想知道如何调试 AutoIt 脚本。或者至少输出脚本的行号(以显示实时执行的代码)。

最佳答案

How can I debug AutoIt code?

语法

Au3检查

根据 Documentation - Intro - AutoIt Syntax Checker (Au3Check) :

Checks the script for syntax errors.

示例:

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7

在执行和编译之前报告(几乎)每个非运行时错误。

运行时

当前行

… show me which code is executed in real time …

根据 Documentation - Function Reference - AutoItSetOption() :

TrayIconDebug If enabled shows the current script line in the tray icon tip to help debugging.

示例:

AutoItSetOption("TrayIconDebug", 1)

编译脚本

#AutoIt3Wrapper_Run_Debug_Mode=Y 使用控制台调试运行脚本。

由于前置包含文件,已编译脚本的行号(在错误消息中)与原始 .au3 文件不匹配。根据Documentation - SciTE4AutoIt3 - Au3Stripper :

If there are no errors, run Au3Stripper to create an Stripped source file Scriptname_Stripped in the same folder containing all the source (the script and any #include files)

示例:

#AutoIt3Wrapper_Run_Au3Stripper=y

编译脚本的错误消息中的行号与 scriptname_stripped.au3 中的行号匹配现在(编译后出现)。

错误代码和返回值

  • ConsoleWrite() 。添加#AutoIt3Wrapper_Change2CUI=y如果要从编译的脚本中读取。
    • Macros of use包括@error , @extended@ScriptLineNumber
    • SciTE4AutoIt3 > Tools > Trace: Add Trace Lines为每一行插入这样的命令。显示当前@error代码(以及相关行)。
    • SciTE4AutoIt3 > Tools > Debug to Console (Alt + D) 为当前选定的行插入这样的命令。显示@ScriptLineNumber ,返回值和@error代码。再次执行原始行以获得其返回值(可能是不需要的)。
  • VarGetType() 返回the internal type representation of a variant .
  • _ArrayDisplay() 查看数组变量的结构和内容。
  • 可能 log to filedatabase如果需要远程调试。
  • 为了简化而记录 UDF ( example ) 时出错。

断言

根据 Documentation - User Defined Function Reference - _Assert() :

Display a message if assertion fails

Related 。对于回归测试和验证边缘情况和假设很有用(或者在意外情况下简单地中止)。相关功能包括:

错误处理

通常函数返回(或组合) return value-error code 。非0错误代码值(有时为负)表示失败。返回值(如果这样使用)在 0 之间交替。 (失败)和1 (成功)。

出现错误时进行处理。脚本可以自行调试以进行正确的错误处理。避免逐步嵌套If -声明。示例:

Global Enum  $RANDOM_FLT, _
$RANDOM_INT

Global Enum $ERROR_OK, _
$ERROR_AIR, _
$ERROR_WATER, _
$ERROR_FOOD, _
$ERROR_ENUM

Global $g_aError[$ERROR_ENUM]
$g_aError[$ERROR_OK] = 'survived'
$g_aError[$ERROR_AIR] = 'no air'
$g_aError[$ERROR_WATER] = 'no water'
$g_aError[$ERROR_FOOD] = 'no food'

Global Const $g_sMsgConsole = 'error:%i - %s\n'

While True
Live()

If @error Then
ConsoleWrite(StringFormat($g_sMsgConsole, @error, $g_aError[@error])); And one of following:
ContinueLoop
; ExitLoop
; Return; If in Local scope.
; Exit @error
EndIf

; Continuation conditional to successful execution of Live():
ConsoleWrite(StringFormat($g_sMsgConsole, @error, $g_aError[@error]))
WEnd

Func Live()
Local Const $iError = Random($ERROR_OK, $ERROR_ENUM - 1, $RANDOM_INT), _
$iExtended = 0, _
$iReturn = $iError ? 0 : 1

Return SetError($iError, $iExtended, $iReturn)
EndFunc

或者more specifically :

Live()

Switch @error

Case $ERROR_AIR
; Handle specific error here.

Case $ERROR_WATER
; Handle specific error here.

Case $ERROR_FOOD
; Handle specific error here.

EndSwitch

返回值启用如下结构:

If Not SomeFunction() Then
; Handle here.
EndIf
; Continuation of script here.

将错误代码与短信相关联可能会有所帮助。如果在多个相关函数之间共享,这些函数透明地返回遇到的错误代码(例如,某些 FindOnPage()$ERR_PAGELOAD 依赖项返回 LoadPage()),则特别有用。

关于autoit - 调试 AutoIt 脚本或获取当前执行的脚本行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34109244/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com