Command Line Interface Application

Programming Command Line Interface applications (CLI applications) is one of the most entertaining tasks a developer may do. So let’s have some fun building our first CLI application in Crystal.

There are two main topics when building a CLI application:

Input

This topic covers all things related to:

Options

It is a very common practice to pass options to the application. For example, we may run crystal -v and Crystal will display:

  1. $ crystal -v
  2. --8<-- "crystal-version.txt"

and if we run: crystal -h, then Crystal will show all the accepted options and how to use them.

So now the question would be: do we need to implement an options parser? No need to, Crystal got us covered with the class OptionParser. Let’s build an application using this parser!

At the start our CLI application has two options:

  • -v / --version: it will display the application version.
  • -h / --help: it will display the application help.

!!! example “help.cr”

  1. ```crystal
  2. require "option_parser"
  3. OptionParser.parse do |parser|
  4. parser.banner = "Welcome to The Beatles App!"
  5. parser.on "-v", "--version", "Show version" do
  6. puts "version 1.0"
  7. exit
  8. end
  9. parser.on "-h", "--help", "Show help" do
  10. puts parser
  11. exit
  12. end
  13. end
  14. ```

So, how does all this work? Well … magic! No, it’s not really magic! Just Crystal making our life easy. When our application starts, the block passed to OptionParser#parse gets executed. In that block we define all the options. After the block is executed, the parser will start consuming the arguments passed to the application, trying to match each one with the options defined by us. If an option matches then the block passed to parser#on gets executed!

We can read all about OptionParser in the official API documentation. And from there we are one click away from the source code … the actual proof that it is not magic!

Now, let’s run our application. We have two ways using the compiler:

  1. Build the application and then run it.
  2. Compile and run the application, all in one command.

We are going to use the second way:

  1. $ crystal run ./help.cr -- -h
  2. Welcome to The Beatles App!
  3. -v, --version Show version
  4. -h, --help Show help

Let’s build another fabulous application with the following feature:

By default (i.e. no options given) the application will display the names of the Fab Four. But, if we pass the option -t / --twist it will display the names in uppercase:

!!! example “twist_and_shout.cr”

  1. ```crystal
  2. require "option_parser"
  3. the_beatles = [
  4. "John Lennon",
  5. "Paul McCartney",
  6. "George Harrison",
  7. "Ringo Starr",
  8. ]
  9. shout = false
  10. option_parser = OptionParser.parse do |parser|
  11. parser.banner = "Welcome to The Beatles App!"
  12. parser.on "-v", "--version", "Show version" do
  13. puts "version 1.0"
  14. exit
  15. end
  16. parser.on "-h", "--help", "Show help" do
  17. puts parser
  18. exit
  19. end
  20. parser.on "-t", "--twist", "Twist and SHOUT" do
  21. shout = true
  22. end
  23. end
  24. members = the_beatles
  25. members = the_beatles.map &.upcase if shout
  26. puts ""
  27. puts "Group members:"
  28. puts "=============="
  29. members.each do |member|
  30. puts member
  31. end
  32. ```
  33. Running the application with the `-t` option will output:
  34. ```console
  35. $ crystal run ./twist_and_shout.cr -- -t
  36. Group members:
  37. ==============
  38. JOHN LENNON
  39. PAUL MCCARTNEY
  40. GEORGE HARRISON
  41. RINGO STARR
  42. ```

Parameterized options

Let’s create another application: when passing the option -g / --goodbye_hello, the application will say hello to a given name passed as a parameter to the option.

!!! example “hello_goodbye.cr”

  1. ```crystal
  2. require "option_parser"
  3. the_beatles = [
  4. "John Lennon",
  5. "Paul McCartney",
  6. "George Harrison",
  7. "Ringo Starr",
  8. ]
  9. say_hi_to = ""
  10. option_parser = OptionParser.parse do |parser|
  11. parser.banner = "Welcome to The Beatles App!"
  12. parser.on "-v", "--version", "Show version" do
  13. puts "version 1.0"
  14. exit
  15. end
  16. parser.on "-h", "--help", "Show help" do
  17. puts parser
  18. exit
  19. end
  20. parser.on "-g NAME", "--goodbye_hello=NAME", "Say hello to whoever you want" do |name|
  21. say_hi_to = name
  22. end
  23. end
  24. unless say_hi_to.empty?
  25. puts ""
  26. puts "You say goodbye, and #{the_beatles.sample} says hello to #{say_hi_to}!"
  27. end
  28. ```

In this case, the block receives a parameter that represents the parameter passed to the option.

Let’s try it!

  1. $ crystal run ./hello_goodbye.cr -- -g "Penny Lane"
  2. You say goodbye, and Ringo Starr says hello to Penny Lane!

Great! These applications look awesome! But, what happens when we pass an option that is not declared? For example -n

  1. $ crystal run ./hello_goodbye.cr -- -n
  2. Unhandled exception: Invalid option: -n (OptionParser::InvalidOption)
  3. from ...

Oh no! It’s broken: we need to handle invalid options and invalid parameters given to an option! For these two situations, the OptionParser class has two methods: #invalid_option and #missing_option

So, let’s add this option handler and merge all these CLI applications into one fabulous CLI application!

All My CLI: The complete application!

Here’s the final result, with invalid/missing options handling, plus other new options:

!!! example “all_my_cli.cr”

  1. ```crystal
  2. require "option_parser"
  3. the_beatles = [
  4. "John Lennon",
  5. "Paul McCartney",
  6. "George Harrison",
  7. "Ringo Starr",
  8. ]
  9. shout = false
  10. say_hi_to = ""
  11. strawberry = false
  12. option_parser = OptionParser.parse do |parser|
  13. parser.banner = "Welcome to The Beatles App!"
  14. parser.on "-v", "--version", "Show version" do
  15. puts "version 1.0"
  16. exit
  17. end
  18. parser.on "-h", "--help", "Show help" do
  19. puts parser
  20. exit
  21. end
  22. parser.on "-t", "--twist", "Twist and SHOUT" do
  23. shout = true
  24. end
  25. parser.on "-g NAME", "--goodbye_hello=NAME", "Say hello to whoever you want" do |name|
  26. say_hi_to = name
  27. end
  28. parser.on "-r", "--random_goodbye_hello", "Say hello to one random member" do
  29. say_hi_to = the_beatles.sample
  30. end
  31. parser.on "-s", "--strawberry", "Strawberry fields forever mode ON" do
  32. strawberry = true
  33. end
  34. parser.missing_option do |option_flag|
  35. STDERR.puts "ERROR: #{option_flag} is missing something."
  36. STDERR.puts ""
  37. STDERR.puts parser
  38. exit(1)
  39. end
  40. parser.invalid_option do |option_flag|
  41. STDERR.puts "ERROR: #{option_flag} is not a valid option."
  42. STDERR.puts parser
  43. exit(1)
  44. end
  45. end
  46. members = the_beatles
  47. members = the_beatles.map &.upcase if shout
  48. puts "Strawberry fields forever mode ON" if strawberry
  49. puts ""
  50. puts "Group members:"
  51. puts "=============="
  52. members.each do |member|
  53. puts "#{strawberry ? "🍓" : "-"} #{member}"
  54. end
  55. unless say_hi_to.empty?
  56. puts ""
  57. puts "You say goodbye, and I say hello to #{say_hi_to}!"
  58. end
  59. ```

Request for user input

Sometimes, we may need the user to input a value. How do we read that value? Easy, peasy! Let’s create a new application: the Fab Four will sing with us any phrase we want. When running the application, it will request a phrase to the user and the magic will happen!

!!! example “let_it_cli.cr”

  1. ```crystal
  2. puts "Welcome to The Beatles Sing-Along version 1.0!"
  3. puts "Enter a phrase you want The Beatles to sing"
  4. print "> "
  5. user_input = gets
  6. puts "The Beatles are singing: 🎵#{user_input}🎶🎸🥁"
  7. ```

The method gets will pause the execution of the application until the user finishes entering the input (pressing the Enter key). When the user presses Enter, then the execution will continue and user_input will have the user value.

But what happens if the user doesn’t enter any value? In that case, we would get an empty string (if the user only presses Enter) or maybe a Nil value (if the input stream is closed, e.g. by pressing Ctrl+D). To illustrate the problem let’s try the following: we want the input entered by the user to be sung loudly:

!!! example “let_it_cli.cr”

  1. ```crystal
  2. puts "Welcome to The Beatles Sing-Along version 1.0!"
  3. puts "Enter a phrase you want The Beatles to sing"
  4. print "> "
  5. user_input = gets
  6. puts "The Beatles are singing: 🎵#{user_input.upcase}🎶🎸🥁"
  7. ```

When running the example, Crystal will reply:

  1. $ crystal run ./let_it_cli.cr
  2. Showing last frame. Use --error-trace for full trace.
  3. In let_it_cli.cr:5:46
  4. 5 | puts "The Beatles are singing: 🎵#{user_input.upper_case}
  5. ^---------
  6. Error: undefined method 'upper_case' for Nil (compile-time type is (String | Nil))

Ah! We should have known better: the type of the user input is the union type String | Nil. So, we have to test for Nil and for empty and act naturally for each case:

!!! example “let_it_cli.cr”

  1. ```crystal
  2. puts "Welcome to The Beatles Sing-Along version 1.0!"
  3. puts "Enter a phrase you want The Beatles to sing"
  4. print "> "
  5. user_input = gets
  6. exit if user_input.nil? # Ctrl+D
  7. default_lyrics = "Na, na, na, na-na-na na" \
  8. " / " \
  9. "Na-na-na na, hey Jude"
  10. lyrics = user_input.presence || default_lyrics
  11. puts "The Beatles are singing: 🎵#{lyrics.upcase}🎶🎸🥁"
  12. ```

Output

Now, we will focus on the second main topic: our application’s output. For starters, our applications already display information but (I think) we could do better. Let’s add more life (i.e. colors!) to the outputs.

And to accomplish this, we will be using the Colorize module.

Let’s build a really simple application that shows a string with colors! We will use a yellow font on a black background:

!!! example “yellow_cli.cr”

  1. ```crystal
  2. require "colorize"
  3. puts "#{"The Beatles".colorize(:yellow).on(:black)} App"
  4. ```

Great! That was easy! Now imagine using this string as the banner for our All My CLI application, it’s easy if you try:

  1. parser.banner = "#{"The Beatles".colorize(:yellow).on(:black)} App"

For our second application, we will add a text decoration (blinkin this case):

!!! example “let_it_cli.cr”

  1. ```crystal
  2. require "colorize"
  3. puts "Welcome to The Beatles Sing-Along version 1.0!"
  4. puts "Enter a phrase you want The Beatles to sing"
  5. print "> "
  6. user_input = gets
  7. exit if user_input.nil? # Ctrl+D
  8. default_lyrics = "Na, na, na, na-na-na na" \
  9. " / " \
  10. "Na-na-na na, hey Jude"
  11. lyrics = user_input.presence || default_lyrics
  12. puts "The Beatles are singing: #{"🎵#{lyrics}🎶🎸🥁".colorize.mode(:blink)}"
  13. ```

Let’s try the renewed application … and hear the difference!! Now we have two fabulous apps!!

You may find a list of available colors and text decorations in the API documentation.

Testing

As with any other application, at some point, we would like to write tests for the different features.

Right now the code containing the logic of each of the applications always gets executed with the OptionParser, i.e. there is no way to include that file without running the whole application. So first we would need to refactor the code, separating the code necessary for parsing options from the logic. Once the refactoring is done, we could start testing the logic and including the file with the logic in the testing files we need. We leave this as an exercise for the reader.

Using Readline and NCurses

In case we want to build richer CLI applications, there are libraries that can help us. Here we will name two well-known libraries: Readline and NCurses.

As stated in the documentation for the GNU Readline Library, Readline is a library that provides a set of functions for use by applications that allow users to edit command lines as they are typed in. Readline has some great features: filename autocompletion out of the box; custom auto-completion method; keybinding, just to mention a few. If we want to try it then the crystal-lang/crystal-readline shard will give us an easy API to use Readline.

On the other hand, we have NCurses(New Curses). This library allows developers to create graphical user interfaces in the terminal. As its name implies, it is an improved version of the library named Curses, which was developed to support a text-based dungeon-crawling adventure game called Rogue! As you can imagine, there are already a couple of shards in the ecosystem that will allow us to use NCurses in Crystal!

And so we have reached The End 😎🎶