diff options
author | Abinoam Praxedes Marques Jr <abinoam@gmail.com> | 2015-10-08 00:12:49 -0300 |
---|---|---|
committer | Abinoam Praxedes Marques Jr <abinoam@gmail.com> | 2015-10-09 23:33:48 -0300 |
commit | c95224e2d5cb72d8d791967a38e8b3f5898f74f5 (patch) | |
tree | 0a1d04613ca79e7634ff9ee1145874373e704786 /README.md | |
parent | 8f8eb73bb873e4b91e04fd82d99935884dbb6f8f (diff) | |
download | highline-c95224e2d5cb72d8d791967a38e8b3f5898f74f5.tar.gz |
Change the recommended way of using HighLine on README.md
Until now we have recommended
require 'highline/import'
But this was causing some name collisions as method names like #say and #ask,
are common in other libraries.
So we should adopt the more verbose and safer way from 2.0.0 on
require 'highline'
cli = HighLine.new
cli.ask "What do you think?"
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 43 |
1 files changed, 28 insertions, 15 deletions
@@ -27,52 +27,65 @@ Documentation See {HighLine} and {HighLine::Question} for documentation. -Start hacking in your code with HighLine with: +Usage +----- ```ruby -require 'highline/import' -``` -Examples --------- +require 'highline' + +# Basic usage -Basic usage: +cli = HighLine.new +answer = cli.ask "What do you think?" +puts "You have answered: #{answer}" -ask("Company? ") { |q| q.default = "none" } + +# Default answer + +cli.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/ } +cli.ask("Age? ", Integer) { |q| q.in = 0..105 } +cli.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*/) }) +cli.ask("Birthday? ", Date) +cli.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" } +cli.ask("Enter your password: ") { |q| q.echo = false } +cli.ask("Enter your password: ") { |q| q.echo = "x" } # ERb based output (with HighLine's ANSI color tools): -say("This should be <%= color('bold', BOLD) %>!") +cli.say("This should be <%= color('bold', BOLD) %>!") # Menus: -choose do |menu| +cli.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 ``` +If you want to save up some characteres, you can inject/import HighLine methods on Kernel by doing the following. Be aware to avoid name collisions at the main namespace. + +```ruby +require 'highline/import' + +say "Now you can use #say directly" +``` + For more examples see the examples/ directory of this project. Requirements |