Making a Good Lookin’ CLI
The following is a guest post by Tiffany Peon and originally appeared on her blog. Tiffany is currently in the Ruby-003 class at The Flatiron School. You can follow her on Twitter here. Ever since starting at Flatiron my friends have been asking me on a regular basis whether or not I can “make cool stuff” yet. So […]
Ever since starting at Flatiron my friends have been asking me on a regular basis whether or not I can “make cool stuff” yet. So when I made my first CLI three weeks ago I was overly excited to show it to them. I came home with a “jukebox” CLI that didn’t really play music, but simply said “I am playing your track” with the specific track name once they typed it in.
They weren’t too impressed, but I was still excited. Making a CLI is a quantifiable measure of my learning. It’s a way for me to see exactly how much stuff I can make the computer do just by interacting with my terminal.
That being said, after making about three different jukebox CLIs, and using that same format to make several other CLIs, it’s starting to feel like I’m having to type a ton just to get some pretty simple commands –
**Disclaimer – the Spanish_if method isn’t due out until Ruby v 9
After Avi mentioned the other day that a lot of our coding projects would soon be made easier by allowing us to use gems for certain tasks, I started to wonder what was out there in terms of command line resources. Sure enough, I found HighLine within seconds of opening up Ruby Toolbox.
I went back to my first jukebox program to take a look at where I could remove some of the messier bits of code. First off, I made sure to require ‘highline/import’ at the beginning of my program.
I found that my code was getting and validating my commands in a bunch of different places (upon launch, in the help directory, at the end of each case statement within my while loop) and I felt that this could become a single point of failure in a future, larger program.
Using HighLine’s menu module, I was able to create clear code to distinguish my choices and their commands in the code below.
Another quick fix I found was adding an index number to my song list. The first time around, I had to write a pretty confusing loop that adjusted the automatic index number of my song from the array I was setting up in the program. I had to create a new array, push each song into that array and interpolate my string.
Womp womp. No fun at all.
Here’s the solution I found using HighLine:
This is all well and good and will save me some time and headaches, but as my friends would ask – “what about the cool stuff?”
Well, here are some things that got me pretty excited.
SWEET VALIDATION –
Remember my absurd gets example from above? Here’s some sample validation from the Highline documentation –
As you move into inputs that require more validation than matching strings (eg. date of birth, zip code, password validation), HighLine provides clear methods for checking those inputs.
SICK COLORS –
Format away, the options are endless –
With the help of HighLine, I’m confident that I can quickly make a good looking, effective CLI.
Disclaimer: The information in this blog is current as of 17 October 2013. Current policies, offerings, procedures, and programs may differ. For up-to-date information visit FlatironSchool.com.
Posted by Flatiron School / October 17, 2013
Learn to Code Python: Free Lesson for Beginners
Flatiron School Welcomes Peter Barth as CEO
“As we navigate a dynamic and complex tech talent landscape, Flatiron School’s mission – to enable the pursuit of a better life through education – is more vital than ever.”