Choose your operating system:
Windows
macOS
Linux
以下范例中包含样本命令,在打印出Fibonacci序列后将成功退出。按照此范例步骤,向自动化项目添加新命令(或脚本):
步骤
必要步骤
:继续前确保自动化项目范围中拥有空白
*.cs
源文件。
-
首先命名源文件,在本范例中将其命名为
SimpleScript.cs
。 -
现在,将以下代码复制到脚本中:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AutomationTool; namespace SampleScript.Automation { // 使用[Help()]属性记载命令及其参数。 [Help("Sample script printing a Fibonacci sequence of values.")] [Help("Usage:SampleScript -Terms=<N>")] [Help("Terms=<N>", "N (int) represents how many terms to compute, and it must be greater than or equal to 1.")] // BuildCommand是所有命令的基类。 public class SampleCommand :BuildCommand { public override void ExecuteBuild() { // 本范例使用ParseParamInt()方法获取命令参数。ParseParam()获取布尔值,ParseParamValue获取字符串。 int NumTerms = ParseParamInt("Terms"); if (NumTerms < 1) { throw new AutomationException("Invalid number of terms specified.Enter -help for syntax."); } else { LogInformation("Fibonnacci sequence:"); int TermA = 1; int TermB = 1; for (int TermIdx = 0; TermIdx < NumTerms; TermIdx++) { LogInformation(" {0}", TermA); int NextTerm = TermA + TermB; TermA = TermB; TermB = NextTerm; } } } } }
-
最后,打开命令行并导航到
Engine\Build\BatchFiles
,然后输入RunUAT.bat SampleCommand -terms=4
。
最终结果
应出现以下输出: