Are you making a CLI app? Use TTY::Prompt!

Alexandra Hernandez
Jul 2, 2018 · 5 min read

TTY::Prompt is a great interactive prompt for the command line. Using it helps in gathering inputs and displaying information beautifully. I first came across it when working on my own CLI application project at the Flatiron School, with a partner. I was so overwhelmed with how to go about building the project that I wasn’t sure if I would have time to read the documentation on how to use this tool. However, my worries were for not; simply taking 5–10 minutes in skimming the documentation, you’ll find how simple is is to implement it in your own project.

Before going over some of its’ great features, you need to install the TTY::Prompt gem. You will first need add gem ‘tty-prompt’ to your Gemfile, and then execute bundle in your bash. Or you can install it via bash by typing gem install tty-prompt. The inside your ruby file, where you will be using your prompts you will need to add this line of code: prompt = TTY::Prompt.new. As this will create prompt needed to get the inputs from the user. Now to go over a few of the TTY::Prompt’s methods I had used in my CLI application.

prompt.ask & validation

via: https://github.com/piotrmurach/tty-prompt

By using prompt.ask you are prompting the user for a simple input on something that pertains to the question asked (which is taken in as an argument). Of course, if you want to make sure the input of the user is something specific that must contain certain characters, you can add a validation to your prompt.ask line; which will be done through the use of a block. As shown:

project.pic1a

By default, TTY::Prompt comes with predefined error messages for options relating to validate. By writing q.validate(/\A\w+@\w+\.\w+\Z/), you are making sure that the user’s input matches the expression written inside the parentheses. The second line of code inside the block determines what the error message will be if the user input is invalid.

poject.pic_1b
project.pic_1c
project.pic_1d

In project.pic_1b you can see that by simply writing ‘someone’, the error message of “Invalid email address” is returned. It doesn’t meet the expression in the q.validate line.

In project.pic_1c, it still doesn’t meet the regex we defined.

The project.pic_1d finally shows us that it accepts the user input as a valid email. The only thing is that the valid email is not in the database of where the user is logging in.

Or you can use TTY::Prompt’s built-in validations by typing this line of code: prompt.ask(‘What is your email?’) { |q| q.validate :email }.

prompt.mask & decoration

Using prompt.mask is great when prompting the user for their password, since it “masks” what the user is typing. The question is, once again, the argument that will be taken in.

When the line of code shown in project.pic_2a is run, the user will see what is shown in project.pic_2b. As you can see, the password that was typed in by the user was masked using bullet points.

project.pic_2a
project.pic_2b

However, you can change the bullet points to anything you’d like. In the code snippet shown in project.pic_3a, I changed the “mask” to an emoji of a pizza. So when this code is run, the user will see pizza slices masking the password they’re inputing.

project.pic_3a
project.pic_3b

The only problem with using the decorate method for the masking prompt is that it may be seen as unprofessional. So must companies will tend to use the original mask of bullet points.

prompt.select & multi-select

To provide a select menu for the user is done very easily. When using prompt.select, you will need to input two arguments: the question, and an array with elements being the options the user will select from. In the images shown below, I am prompting the user to select whether they are a student or a Teacher.

project.pic_4a
project.pic_4b

I set the user’s selection to a variable (ie $user_type)in order to use the user’s input elsewhere in the CLI application.

TTY::Prompt also allows a user to select multiple things, by using the multi_select method:

via: https://github.com/piotrmurach/tty-prompt

You can shovel the selections the user made into an array, and perform any action you need to.

prompt.collect

If you wanted to collect more than one input from the user, you would use TTY::Prompt’s collect method. In this manner you are creating a hash, with each input the user makes set as a value for a key that describes the what the input is.

In the CLI application I requested that the user input an “Announcement title:” using TTY::Prompt’s ask method, and set that input as a value for the key [:title]. And the same was done for the description of an announcement.

project.pic_5a
project.pic_5b

That title and description are then collected into a hash, and I was able to use the keys of the hash as arguments on other methods in my code.

Just Do It!

Add TTY::Prompt to your CLI application! It will help make life easier for you by removing any unnecessary what if cases.

___________________________________________________________________

Also, if you haven’t been able to tell, the CLI application created was a school application that allowed a student user to view course materials, and allowed a teacher user to modify the course materials.

For more info on TTY::Prompt go to: https://github.com/piotrmurach/tty-prompt .

Alexandra Hernandez

Written by

An avid developer and gamer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade