In Avoidance of Errors: Using TTY-Prompt for my First Command Line Interface

Lydia Reitzel
3 min readMar 5, 2021

My first task as a new programmer was to create a Command Line Interface(CLI) project that would pull information from an online resource. We had the choice to import data from an API or scrape data from a website, and I chose to use an API. To be specific, I chose the National Parks Service API which gives lots of great information on the United States national parks and monuments. I was inspired to help people find parks because 2020 prevented us from gathering indoors and got many people out and about in nature nearby. I focused on North Carolina because, well, I live here and I’m selfish.

TTY-Prompt:

My idea for my program was that I would show users a list of all the national parks in North Carolina, then allow them to select a state and get the details. I originally set up my program to list the options in a numbered list, such as:

“#{i}. #{park.name}”

Then the user would input a number, and get all the information about the corresponding park. But I also realized that I would need to account for incorrect inputs, like letters or symbols, and also create error messages. I was hoping to find a way around the human error that comes along with user input. In my research I found a fantastic trickster of a gem that would allow me to put out a list that my user could simply scroll through to select their desired park, maybe my new favorite gem ever: TTY-Prompt.

This gem, TTY-Prompt is part of large set of TTY or Teletype gems, but so far I have only had the pleasure of exploring Prompt. Essentially, you install the gem, and create a new instance of TTY-Prompt. I put mine inside a method called prompt, where I set the new instance of TTY-Prompt to a variable I also called “prompt”. The body of my “prompt” method looks like so:

prompt = TTY::Prompt.new

Then, inside any of the methods you use to create lists, you use the “select” method on the “prompt” variable and add in parentheses the string that you would like the list to output at the top of your prompt. Using select requires a block of code that tells select what you want it to return. In this case, I used the example in given in the gem documentation, which suggests that you use “do |menu|” followed by however many lines you need of “menu.choice” followed by the string you would like to be on that line of your list. So, for my main menu I put the beginning options, which were not objects, but strings that I was hard-coding with “menu.choice”.

This is where I thought I would hit a wall! You see, object-oriented programming emphasizes exactly what you think it would: objects. It encourages abstraction, not hard-coding! And hard-coding does not involve object creation. After delving a little further into this marvelous little gem, I discovered the gem already had a solution. With TTY-Prompt, you can pass in an array and the method will create a list of choices for you! So, using the objects I created in my different classes, I was able to pass in arrays, one of all parks (or Park.all) and one of all activities (or Activity.all) from methods that output only the names of these objects, not the objects in their entirety.

Small note: there is also the option to add in “cycle: true” to the argument you pass to select. This will allow the user to scroll back up the beginning of the list instead of having to travel back to the top themselves. I did not include this option in my larger lists because I wanted users to be aware they had reached the end of the list.

So now my program will show my users a list of all the National Parks in North Carolina, and it will show a list of the many activities available in those parks! All the while, I was able to save time and hassle because I don’t have to worry about an improper input from my user. No error messages needed, isn’t that awesome?

--

--

Lydia Reitzel

Former server, current student of software engineering. Figuring things out as I go!