Ruby Highline – Examples
August 1, 2009
Examples
Basic usage:
ask("Company? ") { |q| q.default = "none" }
Validation:
ask("Age? ", Integer) { |q| q.in = 0..105 }
ask("Name? (last, first) ")
{ |q| q.validate = /\A\w+, ?\w+\Z/ }
Type conversion for answers:
ask("Birthday? ", Date)
ask("Interests? (comma sep list) ",
lambda { |str| str.split(/,\s*/) })
Reading passwords:
ask("Enter your password: ") { |q| q.echo = false }
ask("Enter your password: ") { |q| q.echo = "x" }
ERb based output (with HighLine‘s ANSI color tools):
say("This should be <%= color('bold', BOLD) %>!")
Menus:
choose do |menu|
menu.prompt = "Please choose your favorite
programming language? "
menu.choice(:ruby) { say("Good choice!") }
menu.choices(:python, :perl)
{ say("Not from around here, are you?") }
end
Reference --> http://highline.rubyforge.org/doc/
Highline – Ruby Gem
July 5, 2008
HighLine is about…
Saving time.
Command line interfaces are meant to be easy. So why shouldn’t building them be easy, too? HighLine provides a solid toolset to help you get the job done cleanly so you can focus on the real task at hand, your task.
Clean and intuitive design.
Want to get a taste for how HighLine is used? Take a look at this simple example, which asks a user for a zip code, automatically does validation, and returns the result:
Code:
require “highline/import”
zipcode = ask(“Zip? “) { |zip| zip.validate = /\A\d{5}(?:-?\d{4})?\Z/ }
Hassle-free Installation.
Installation is easy via RubyGems. Simply enter the command:
linux : sudo gem install highline
Windows: gem install highline
and you’ll be on your way! Of course, manual installation is an option, too.
For more details http://highline.rubyforge.org/