github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/structs/function_doc.yaml (about) 1 - DocumentID: alias 2 Title: >+ 3 `alias` 4 CategoryID: commands 5 Summary: >- 6 Create an alias for a command 7 Description: |- 8 `alias` allows you to create a shortcut or abbreviation for a longer command. 9 10 IMPORTANT: aliases in Murex are not macros and are therefore different than 11 other shells. if the shortcut requires any dynamics such as `piping`, 12 `command sequencing`, `variable evaluations` or `scripting`... 13 Prefer the **`function`** builtin. 14 Usage: |- 15 ``` 16 alias alias=command parameter parameter 17 18 !alias command 19 ``` 20 Examples: |- 21 Because aliases are parsed into an array of parameters, you cannot put the 22 entire alias within quotes. For example: 23 24 ``` 25 # bad :( 26 » alias hw="out Hello, World!" 27 » hw 28 exec "out\\ Hello,\\ World!": executable file not found in $PATH 29 30 # good :) 31 » alias hw=out "Hello, World!" 32 » hw 33 Hello, World! 34 ``` 35 36 Notice how only the command `out "Hello, World!"` is quoted in `alias` the 37 same way you would have done if you'd run that command "naked" in the command 38 line? This is how `alias` expects it's parameters and where `alias` on Murex 39 differs from `alias` in POSIX shells. 40 41 To materialize those differences, pay attention to the examples below: 42 43 ``` 44 # bad : the following statements generate errors, 45 # prefer function builtin to implent them 46 47 » alias myalias=out "Hello, World!" | wc 48 » alias myalias=out $myvariable | wc 49 » alias myalias=out ${vmstat} | wc 50 » alias myalias=out "hello" && out "world" 51 » alias myalias=out "hello" ; out "world" 52 » alias myalias="out hello; out world" 53 ``` 54 55 In some ways this makes `alias` a little less flexible than it might 56 otherwise be. However the design of this is to keep `alias` focused on it's 57 core objective. To implement the above aliasing, you can use `function` 58 instead. 59 Flags: 60 Detail: |- 61 ### Allowed characters 62 63 Alias names can only include alpha-numeric characters, hyphen and underscore. 64 The following regex is used to validate the `alias`'s parameters: 65 `^([-_a-zA-Z0-9]+)=(.*?)$` 66 67 ### Undefining an alias 68 69 Like all other definable states in Murex, you can delete an alias with the 70 bang prefix: 71 72 ``` 73 » alias hw=out "Hello, World!" 74 » hw 75 Hello, World! 76 77 » !alias hw 78 » hw 79 exec "hw": executable file not found in $PATH 80 ``` 81 82 ### Order of preference 83 84 {{ include "gen/includes/order-of-precedence.inc.md" }} 85 Synonyms: 86 - alias 87 - "!alias" 88 Related: 89 - function 90 - private 91 - source 92 - g 93 - let 94 - set 95 - global 96 - export 97 - exec 98 - fexec 99 - method 100 101 - DocumentID: function 102 Title: >+ 103 `function` 104 CategoryID: commands 105 Summary: >- 106 Define a function block 107 Description: |- 108 `function` defines a block of code as a function 109 Usage: |- 110 Define a function: 111 112 ``` 113 function name { code-block } 114 ``` 115 116 Define a function with variable names defined (**default value** and 117 **description** are optional parameters): 118 119 ``` 120 function name ( 121 variable1: data-type [default-value] "description", 122 variable2: data-type [default-value] "description" 123 ) { 124 code-block 125 } 126 ``` 127 128 Undefine a function: 129 130 ``` 131 !function command 132 ``` 133 Examples: |- 134 ``` 135 » function hw { out "Hello, World!" } 136 » hw 137 Hello, World! 138 139 » !function hw 140 » hw 141 exec "hw": executable file not found in $PATH 142 ``` 143 Flags: 144 Detail: |- 145 ### Allowed characters 146 147 Function names can only include any characters apart from dollar (`$`). 148 This is to prevent functions from overwriting variables (see the order of 149 preference below). 150 151 ### Undefining a function 152 153 Like all other definable states in Murex, you can delete a function with 154 the bang prefix `!function` (see the example above). 155 156 ### Using parameterized variable names 157 158 By default, if you wanted to query the parameters passed to a Murex function 159 you would have to use either: 160 161 * the Bash syntax where of `$2` style numbered reserved variables, 162 163 * and/or the Murex convention of `$PARAM` / `$ARGS` arrays (see **reserved-vars** 164 document below), 165 166 * and/or the older Murex convention of the `args` builtin for any flags. 167 168 Starting from Murex `2.7.x` it's been possible to declare parameters from 169 within the function declaration: 170 171 ``` 172 function name ( 173 variable1: data-type [default-value] "description", 174 variable2: data-type [default-value] "description" 175 ) { 176 code-block 177 } 178 ``` 179 180 #### Syntax 181 182 First off, the syntax doesn't have to follow exactly as above: 183 184 * **Variables** shouldn't be prefixed with a dollar (`$`). This is a little like 185 declaring variables via `set`, etc. However it should be followed by a colon 186 (`:`) or comma (`,`). Normal rules apply with regards to allowed characters 187 in variable names: limited to ASCII letters (upper and lower case), numbers, 188 underscore (`_`), and hyphen (`-`). Unicode characters as variable names are 189 not currently supported. 190 191 * **data-type** is the Murex data type. This is an optional field in version 192 `2.8.x` (defaults to `str`) but is required in `2.7.x`. 193 194 * The **default value** must be inside square brackets (`[...]`). Any value is 195 allowed (including Unicode) _except_ for carriage returns / new lines (`\r`, 196 `\n`) and a closing square bracket (`]`) as the latter would indicate the end 197 of this field. You cannot escape these characters either. 198 199 This field is optional. 200 201 * The **description** must sit inside double quotes (`"..."`). Any value is allowed 202 (including Unicode) _except_ for carriage returns / new lines (`\r`, `\n`) 203 and double quotes (`"`) as the latter would indicate the end of this field. 204 You cannot escape these characters either. 205 206 This field is optional. 207 208 * You do not need a new line between each parameter, however you do need to 209 separate them with a comma (like with JSON, there should not be a trailing 210 comma at the end of the parameters). Thus the following is valid: 211 `variable1: data-type, variable2: data-type`. 212 213 #### Variables 214 215 Any variable name you declare in your function declaration will be exposed in 216 your function body as a local variable. For example: 217 218 ``` 219 function hello (name: str) { 220 out "Hello $name, pleased to meet you." 221 } 222 ``` 223 224 If the function isn't called with the complete list of parameters and it is 225 running in the foreground (ie not part of `autocomplete`, `event`, `bg`, etc) 226 then you will be prompted for it's value. That could look something like this: 227 228 ``` 229 » function hello (name: str) { 230 » out "Hello $name, pleased to meet you." 231 » } 232 233 » hello 234 Please enter a value for 'name': Bob 235 Hello Bob, pleased to meet you. 236 ``` 237 238 (in this example you typed `Bob` when prompted) 239 240 #### Data-Types 241 242 This is the Murex data type of the variable. From version `2.8.x` this field 243 is optional and will default to `str` when omitted. 244 245 The advantage of setting this field is that values are type checked and the 246 function will fail early if an incorrect value is presented. For example: 247 248 ``` 249 » function age (age: int) { out "$age is a great age." } 250 251 » age 252 Please enter a value for 'age': ten 253 Error in `age` ( 2,1): cannot convert parameter 1 'ten' to data type 'int' 254 255 » age ten 256 Error in `age` ( 2,1): cannot convert parameter 1 'ten' to data type 'int' 257 ``` 258 259 However it will try to automatically convert values if it can: 260 261 ``` 262 » age 1.2 263 1 is a great age. 264 ``` 265 266 #### Default values 267 268 Default values are only relevant when functions are run interactively. It 269 allows the user to press enter without inputting a value: 270 271 ``` 272 » function hello (name: str [John]) { out "Hello $name, pleased to meet you." } 273 274 » hello 275 Please enter a value for 'name' [John]: 276 Hello John, pleased to meet you. 277 ``` 278 279 Here no value was entered so `$name` defaulted to `John`. 280 281 Default values will not auto-populate when the function is run in the 282 background. For example: 283 284 ``` 285 » bg {hello} 286 Error in `hello` ( 2,2): cannot prompt for parameters when a function is run in the background: too few parameters 287 ``` 288 289 #### Description 290 291 Descriptions are only relevant when functions are run interactively. It allows 292 you to define a more useful prompt should that function be called without 293 sufficient parameters. For example: 294 295 ``` 296 » function hello (name: str "What is your name?") { out "Hello $name" } 297 298 » hello 299 What is your name?: Sally 300 Hello Sally 301 ``` 302 303 ### Order of precedence 304 305 {{ include "gen/includes/order-of-precedence.inc.md" }} 306 Synonyms: 307 - function 308 - "!function" 309 Related: 310 - args 311 - alias 312 - private 313 - source 314 - g 315 - let 316 - set 317 - global 318 - export 319 - exec 320 - fexec 321 - method 322 - reserved-vars 323 - version 324 - break 325 326 - DocumentID: private 327 Title: >+ 328 `private` 329 CategoryID: commands 330 Summary: >- 331 Define a private function block 332 Description: |- 333 `private` defines a function who's scope is limited to that module or source 334 file. 335 336 Privates cannot be called from one module to another (unless they're wrapped 337 around a global `function`) and nor can they be called from the interactive 338 command line. The purpose of a `private` is to reduce repeated code inside 339 a module or source file without cluttering up the global namespace. 340 Usage: |- 341 ``` 342 private name { code-block } 343 ``` 344 Examples: |- 345 ``` 346 # The following cannot be entered via the command line. You need to write 347 # it to a file and execute it from there. 348 349 private hw { 350 out "Hello, World!" 351 } 352 353 function tom { 354 hw 355 out "My name is Tom." 356 } 357 358 function dick { 359 hw 360 out "My name is Dick." 361 } 362 363 function harry { 364 hw 365 out "My name is Harry." 366 } 367 ``` 368 Flags: 369 Detail: |- 370 ### Allowed characters 371 372 Private names can only include any characters apart from dollar (`$`). 373 This is to prevent functions from overwriting variables (see the order of 374 preference below). 375 376 ### Undefining a private 377 378 Because private functions are fixed to the source file that declares them, 379 there isn't much point in undefining them. Thus at this point in time, it 380 is not possible to do so. 381 382 ### Order of preference 383 384 {{ include "gen/includes/order-of-precedence.inc.md" }} 385 Synonyms: 386 Related: 387 - function 388 - alias 389 - source 390 - g 391 - let 392 - set 393 - global 394 - export 395 - exec 396 - fexec 397 - method 398 - break 399 400 - DocumentID: method 401 Title: >+ 402 `method` 403 CategoryID: commands 404 Summary: >- 405 Define a methods supported data-types 406 Description: |- 407 `method` defines what the typical data type would be for a function's STDIN 408 and STDOUT. 409 Usage: |- 410 ``` 411 method: define name { json } 412 ``` 413 Examples: |- 414 ``` 415 method: define name { 416 "Stdin": "@Any", 417 "Stdout": "json" 418 } 419 ``` 420 Flags: 421 Detail: |- 422 ### Type Groups 423 424 You can define a Murex data type or use a type group. The following type 425 groups are available to use: 426 427 ```go 428 {{ include "lang/types/groups.go" }} 429 ``` 430 Synonyms: 431 Related: 432 - function 433 - alias 434 - private 435 - autocomplete 436 - runtime 437 - pipe-arrow 438 - interactive-shell