menu:operate:scenarios:syntax
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| menu:operate:scenarios:syntax [2020/04/28 11:09] – [if <condition>] bdorlandt | menu:operate:scenarios:syntax [2024/07/03 12:31] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{indexmenu_n> | ||
| + | ====== Scenario Syntax ====== | ||
| + | This article describes the syntax for the scenarios and all of its operators. | ||
| + | |||
| + | All of the syntax described needs to be placed under the **[scenario]** header. | ||
| + | |||
| + | ===== Variables ===== | ||
| + | Variables can store data, like a string or information returned from commands. This could either be a list or a hash. | ||
| + | |||
| + | Here is more information on [[menu: | ||
| + | |||
| + | ==== List Variables ==== | ||
| + | A standard variable is a list variable, containing zero or more items. It is of the form ''< | ||
| + | |||
| + | > Legacy scenario syntax used an equal sing " | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | Lists can be assigned as parameters for commands: | ||
| + | |||
| + | < | ||
| + | LogAction -n < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | If these variables contain more than one value, the first value of the list will be taken. | ||
| + | |||
| + | ==== Relations ==== | ||
| + | |||
| + | Relations can be resolved and their result will be parsed into the scenario. | ||
| + | |||
| + | < | ||
| + | < | ||
| + | log_action -n < | ||
| + | log_action -n < | ||
| + | log_action -n < | ||
| + | </ | ||
| + | |||
| + | Note that when resolving the scenario, the tasker first parses relations, and then it parses hash variables. So relations have priority over hash variables. | ||
| + | ==== Hash Variables ==== | ||
| + | > Before 7.1.0 the syntax was < | ||
| + | |||
| + | Some commands return hashes instead of lists (parse_cmd for example), denoted as ''< | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | |||
| + | " | ||
| + | |||
| + | To access variables of the simplest version, a hash of lists: | ||
| + | |||
| + | < | ||
| + | < | ||
| + | <aaa> := < | ||
| + | </ | ||
| + | |||
| + | '' | ||
| + | |||
| + | Hashes can be more complex than that, though. A hash of a hash can be accessed as follows: | ||
| + | |||
| + | < | ||
| + | < | ||
| + | log_action -n < | ||
| + | </ | ||
| + | |||
| + | Here, '' | ||
| + | |||
| + | To get a list of all keys, the keyword '' | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | foreach < | ||
| + | < | ||
| + | log_action -n < | ||
| + | endeach | ||
| + | </ | ||
| + | |||
| + | > Note: This example previously showed an incorrect assignment of a keyed hash value: ''< | ||
| + | |||
| + | |||
| + | Details on foreach can be found below, but note that a key in calling a hash variable can be either a literal or the contents of a list variable. If the length of this list variable is bigger than 1, the first item of this list is taken. | ||
| + | |||
| + | ==== Error ==== | ||
| + | |||
| + | If a scenario command fails, this is stored in the ''< | ||
| + | |||
| + | < | ||
| + | reachable -n < | ||
| + | if < | ||
| + | log_action -n < | ||
| + | stop | ||
| + | endif | ||
| + | log_action -n < | ||
| + | </ | ||
| + | |||
| + | More details on the error can be found in the job logs. | ||
| + | |||
| + | ===== Conditionals and Loops ===== | ||
| + | |||
| + | ==== if < | ||
| + | Conditional execution. This can be nested. Conditions have the form ''"< | ||
| + | ''< | ||
| + | |||
| + | Beyond that, variables can also be extended with extra operations: | ||
| + | |||
| + | < | ||
| + | if ! < | ||
| + | if not < | ||
| + | if count < | ||
| + | if < | ||
| + | </ | ||
| + | |||
| + | Note that ''< | ||
| + | |||
| + | Examples: | ||
| + | < | ||
| + | if < | ||
| + | if not < | ||
| + | if < | ||
| + | if < | ||
| + | if < | ||
| + | </ | ||
| + | |||
| + | Notes: | ||
| + | |||
| + | * Strings should be quoted in a condition and be on the right side | ||
| + | * < | ||
| + | * Unless it contains one or more of these special characters: '' | ||
| + | |||
| + | ==== else ==== | ||
| + | Follows an if-statement. If this if fails, the code below '' | ||
| + | |||
| + | ==== endif ==== | ||
| + | Close the current if or else condition. | ||
| + | |||
| + | ==== foreach < | ||
| + | |||
| + | Loop through a list and run code for each item in this list, marked between a '' | ||
| + | |||
| + | < | ||
| + | foreach < | ||
| + | # do something | ||
| + | endeach | ||
| + | </ | ||
| + | |||
| + | ''< | ||
| + | |||
| + | < | ||
| + | foreach < | ||
| + | # do something | ||
| + | endeach | ||
| + | </ | ||
| + | |||
| + | ==== endeach ==== | ||
| + | Marks the end of the current foreach loop. | ||
| + | |||
| + | ==== next ==== | ||
| + | Abort execution of the current foreach loop and move on to the next item in the list. | ||
| + | |||
| + | ==== last ==== | ||
| + | Jump out of the current foreach loop. | ||
| + | |||
| + | ===== List Operations ===== | ||
| + | |||
| + | Lists can be manipulated by simple operations. | ||
| + | |||
| + | Note these are some additional, advanced, commands that operate on lists and require options. They are included in the [[menu: | ||
| + | |||
| + | These include the commands: " | ||
| + | |||
| + | |||
| + | ==== reverse ==== | ||
| + | Take a list, and return it in reverse order. | ||
| + | |||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | ==== push ==== | ||
| + | Push item to end of list. Same as ''< | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | ==== pop ==== | ||
| + | Assign the last value of a list to the variable item and remove from it from the list. | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== unshift ==== | ||
| + | Insert an item to front of the list. Same as ''< | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | ==== shift ==== | ||
| + | Assign the first value from the list to item and remove it from the list. | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | ==== null ==== | ||
| + | Assign an empty list. If ''< | ||
| + | |||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Other ===== | ||
| + | |||
| + | ==== description ==== | ||
| + | Set the description of the scenario. If multiple descriptions are set in a scenario, the first one is taken. This also applies when the description is set via an API call. Once set, it is not changed. | ||
| + | |||
| + | < | ||
| + | Description This is a scenario | ||
| + | Description < | ||
| + | </ | ||
| + | |||
| + | ==== stop ==== | ||
| + | Quit scenario. The scenario will have failed and the job status will list <color red> | ||
| + | |||
| + | Make sure to read the [[menu: | ||
| + | |||
| + | Here the usage of " | ||
| + | ==== end ==== | ||
| + | |||
| + | Quit scenario, signal that the scenario succeeded. It may be used to halt a scenario at any point and set the job status to <color green> | ||
| + | |||
| + | ==== task ==== | ||
| + | It is currently possible to use a named scenario as a ' | ||
| + | |||
| + | < | ||
| + | task := another_scenario | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== comments ==== | ||
| + | Comments can be added to the scenario using the '#' | ||
| + | < | ||
| + | # some comment | ||
| + | if < | ||
| + | # it is expected to enter here. | ||
| + | do_something | ||
| + | endif | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Examples ===== | ||
| + | Several examples can be found on the [[guides: | ||