JavaXT
|
|
Console UtilsThe Console class provides various command line utilities used to print debug messages and collect user inputs.console.logOne of the most useful features in the Console class is the log function. Similar to the JavaScript console.log, it prints a message to the standard output stream, along with the line number and class that is calling the log function. You can even pass multiple args to the log function. Example:package com.example; import java.util.*; import static javaxt.utils.Console.console; public class Main { public static void main(String[] arguments) { console.log("Hello"); console.log("Date: ", new Date()); console.log(1>0, 20/5, new int[]{1,2,3}); } }This simple command line app will output the cfollowing to the standard output stream: [Main:8] Hello [Main:9] Date: Sat Mar 22 08:56:01 EDT 2025 [Main:10] true 4 [1,2,3]As you can see, the class name and line number appear on the left, followed by whatever you want to print. This makes the console.log incredibly handy for debugging apps. parseArgs and getValueThe parseArgs and getValue methods are used to parse command line arguments, typically from a main() function. The parseArgs method converts command line args (an array of strings) into a HashMap. Example:java -jar demo.jar -test database -list tables -comment "I love Java!" In this example, everything to the right of the jar file would be passed to the main function. If the main class in demo.jar looked something like this: package com.example; import java.util.*; import static javaxt.utils.Console.*; public class Main { public static void main(String[] arguments) { //Parse args HashMap The output would look something like this: ----------------------- - All args: ----------------------- -list = tables -comment = I love Java! -test = database ----------------------- - Test ----------------------- Test database, list tables As you can see, the parseArgs separates strings by identifying anything that starts with a "-" as key (e.g. "-test") and the following string as a value ("database"). The getValue method can be used in conjunction with parseArgs to search for multiple keywords. In the following example, getValue will return the value for either "--port" or "-p" using the HashMap generated by parseArgs. package com.example; import java.util.*; import static javaxt.utils.Console.*; public class Main { public static void main(String[] arguments) { var args = parseArgs(arguments); Integer port = getValue(args, "--port", "-p").toInteger(); if (port==null) port = 8080; WebServer.start(port); } } getInputThe getInput method is used to collect user inputs via a command line prompt. Example:package com.example; import javaxt.sql.Value; import static javaxt.utils.Console.*; public class Main { public static void main(String[] arguments) { Boolean answer = new Value(getInput( "\nDo you want to load timezones? [Y/n] ")).toBoolean(); if (answer==null || answer==false) return; System.out.println("Loading timezones..."); } } |