userinput ========= [![Gem Version](https://img.shields.io/gem/v/userinput.svg)](https://rubygems.org/gems/userinput) [![Dependency Status](https://img.shields.io/gemnasium/akerl/userinput.svg)](https://gemnasium.com/akerl/userinput) [![Code Climate](https://img.shields.io/codeclimate/github/akerl/userinput.svg)](https://codeclimate.com/github/akerl/userinput) [![Coverage Status](https://img.shields.io/coveralls/akerl/userinput.svg)](https://coveralls.io/r/akerl/userinput) [![Build Status](https://img.shields.io/travis/akerl/userinput.svg)](https://travis-ci.org/akerl/userinput) [![MIT Licensed](https://img.shields.io/badge/license-MIT-green.svg)](https://tldrlegal.com/license/mit-license) A simple user input library ## Usage User interaction is handled by the `UserInput::Prompt` object. As a convenience, this can be created using the `UserInput.new` method: ``` > require 'userinput' => true > prompt = UserInput.new(message: 'Username') => # > prompt.ask Username? leet_dude => "leet_dude" ``` You can optionally provide a default: ``` > require 'userinput' => true > prompt = UserInput.new(default: '_other') => # > prompt.ask ? [_other] answer => "answer" > prompt.ask ? [_other] => "_other" ``` If you provide a validation Regexp or a code block, the input will be validated using that: ``` > require 'userinput' => true > prompt = UserInput.new(validation: /[0-9]+/) => # > prompt.ask ? _str ? 23 => "23" > other_prompt = UserInput.new { |x| x == '_correct' } => #> > other_prompt.ask ? _wrong ? 23 ? _correct => "_correct" ``` Providing a number of attempts will cause an ArgumentError to be raised after that many failed inputs: ``` > require 'userinput' => true > prompt = UserInput.new(attempts: 3, validation: /[0-9]+/) => # > prompt.ask ? _str ? _other ? _attempt ArgumentError: No valid input provided ``` For sensitive input, pass the `secret` parameter, which will try to disable terminal printing of the user's input: ``` > require 'userinput' => true > prompt = UserInput.new(message: 'Password', secret: true) => # > prompt.ask Password? => "_password" ``` ## Installation gem install userinput ## License userinput is released under the MIT License. See the bundled LICENSE file for details.