Using %Library.Prompt in IRIS for terminal interaction
Interacting with Users in the Terminal: A Guide to Using %Library.Prompt in IRIS
Have you ever wondered how commands like ^DATABASE engage users in the terminal? Or perhaps you're writing an automation routine and want ways to specify options directly from the terminal. Thankfully, the %Library.Prompt class in IRIS offers a straightforward way to do so!
String Input
For basic input, such as asking the user to provide a filepath or namespace, use the following code:
set status = ##class(%Library.Prompt).GetString("Input filepath:", .path)This code prompts the user to input a string, which could be anything from file paths to namespaces. The resulting response will be stored in the path variable.
Numeric Input
To request a numeric input, like the number of cores, you can use:
set status = ##class(%Library.Prompt).GetNumber("Input number of cores:",.number)%Library.Prompt automatically ensure that the input is numeric. We can also specify a min and max number to use
set status = ##class(%Library.Prompt).GetNumber("Input number of cores (1-8):",.number,0,8)Default Options
You can set default options as well. For example, to default an answer to 'yes' we just set the response variable beforehand:
set answer = 1
set status = ##class(%Library.Prompt).GetYesNo("Accept default settings?", .answer)Here, answer defaults to 1 (yes) if the user doesn't provide input.
Menus
For more complex inputs, such as selecting from a menu, you can use methods like GetArray() or GetMenu():
#include %syPrompt
set options(1) = "Option 1"
set options(2) = "Option 2"
do ##class(%Library.Prompt).GetArray("Select deployment type:", .result, .options, , , , $$$InitialDisplayMask)This approach presents a list of options to the user. The $$$InitialDisplayMask flag displays the menu before selection.
These command-line tools significantly simplify writing and interacting with more complex routines. For further information check out the IRIS documentation.
Comments
@Elijah Tamarchenko - well done! I have indeed found this library to be very helpful to my in constructing interactive user prompts in ObjectScript!
Thanks for the article. We need to create a complex process to update information in a production and it will be a good idea for the developers.