The Accidental Rubyist

invalid byte sequence in UTF-8

Archive for July 2010

User friendly command-line input with ruby readline

leave a comment »

While getting user input for various fields, you’d like some values to appear so the user can choose them via arrow keys, or Control-R. Readline makes this very simple.

Readline::HISTORY.push(*values) unless values.empty?

Readline::HISTORY is like an array. You can clear it, push values to it, print it using “to_a”.

To get user input, instead of “gets” we can use:

str = Readline::readline('>', boolean)

If boolean is true, entries will be automatically pushed into HISTORY.

When a user enters multiple records, its easy to save entries in a file based on column name, and add them to HISTORY before prompting him. So the user gets column-specific history. I am doing this in bugzyrb (a command-line bug tracker). Its on github in a file named cmdapp.rb history_read and history_save.

I might mention that Highline, also allows a readline flag in ask and question. However, from what I tried, it does not push valid values into HISTORY. It defines a completion_proc so you can tab complete the given options.

Written by totalrecall

July 10, 2010 at 8:32 pm

Posted in ruby

ruby gems that waste our time

with one comment

I searched through and find a gem named “cri“. It says:

“Cri is a library for building easy-to-use commandline tools.”. There’s no webpage or any further information on the web but there are some 1850 downloads so it must be good, right? So i install it, and find absolutely no documentation, no examples, no usage of any kind. Going through the source reveals its just another command line parser which perhaps prints a nice help. So i delete “cri”.
Should it not be basic ettiquette for developers to release gems only with some minimal documentation and samples. Should we have to download and browse source to know what a gem does and even then not be sure if it works ?

Written by totalrecall

July 10, 2010 at 8:05 pm

Posted in ruby