App 2: Handling Help

Another relatively simple case we can knock out is the “Help” use case. This is when the user types the letter "h" to see our help text.

Because we implemented the showHelp function in the IOHelper object earlier, and because that function returns Try[Unit], all we have to do is call it in our second case statement:

def handleUserInput(input: String): Try[Unit] = input match
    case "q" => 
        Try(System.exit(0))
    case "h" => 
        IOHelper.showHelp()     // <== THE NEW CODE

That function prints our “help text” to STDOUT using println statements — and because that function follows our rule that any time we reach into the outside world, we return Try — that function “just works” here.