README.md in cliutils-1.2.6 vs README.md in cliutils-1.2.7

- old
+ new

@@ -496,13 +496,26 @@ ``` ![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/prefs-ask-options.png "Options") ### Validators -"But," you say, "I want to ensure that my user gives answers that conform to certain specifications!" Not a problem, dear user, `Prefs` has you covered: +"But," you say, "I want to ensure that my user gives answers that conform to certain specifications!" Not a problem, dear user: `Prefs` gives you Validators. Currently supported Validators are: ```YAML +validators: + - numeric # Must be a number + - alphabetic # Must be made up of letters and spaces + - alphanumeric # Must be made up of letters, numbers, and spaces + - date # Must be a parsable date (e.g., 2014-04-03) + - non_nil # Must be a non-nil value + - numeric # Must be made up of numbers + - url # Must be a fully-qualified URL +``` + +An example: + +```YAML prompts: - prompt: What is your name? config_key: name config_section: personal_info validators: @@ -517,38 +530,49 @@ ```Ruby prefs.ask ``` ![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/prefs-ask-validators.png "Validators") -`Prefs` currently supports these validators: +Note that validators are evaluated in order, from top to bottom. If any validator fails, `messenger` will display an error and prompt the user to try again. -* `alphabetic`: must be made up of letters and spaces -* `alphanumeric`: must be made up of letters, numbers, and spaces -* `date`: must be a parsable date (e.g., 2014-04-03) -* `non_nil`: must be a non-nil value -* `numeric`: must be made up of numbers -* `url`: must be a fully-qualified URL - ### Behaviors -Finally, a common desire might be to modify the user's answer in some way: +Finally, a common desire might be to modify the user's answer in some way before storing it. `Prefs` accomplishes this via Behaviors. Currently supported Behaviors are: ```YAML +validators: + - capitalize # Turns "answer" into "Answer" + - local_filepath # Runs File.expand_path on the answer + - lowercase # Turns "AnSwEr" into "answer" + - prefix: 'test ' # Prepends 'test ' to the answer + - suffix: 'test ' # Appends 'test ' to the answer + - titlecase # Turns "the answer" into "The Answer" + - uppercase # Turns "answer" to "ANSWER" +``` + +An example: + +```YAML prompts: - - prompt: Where is your SSH public key located? - config_key: pub_key + - prompt: What is your favorite food? + config_key: food config_section: personal_info + validators: + - non_nil behaviors: - - local_filepath + - uppercase + - prefix: 'My favorite food: ' + - suffix: ' (soooo good!)' ``` -The `local_filepath` behavior will expand the user's answer as a filepath (e.g., `~/.ssh` will get saved as `/Users/bob/.ssh/`). +```Ruby +prefs.ask +``` +![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/prefs-ask-behaviors.png "Behaviors") -`Prefs` currently supports these behaviors: +Note that behaviors are executed in order, which might give you different results than you're expecting. In the example above, for example, placing the `uppercase` behavior last in the list will uppercase *the entire string* (including prefix and suffix). -* `local_filepath`: runs File.expand_path on the answer - ### Adding Pref Responses to a Configurator Once the user has answered all the preference prompts, you can fold those answers back into a Configurator using the `ingest` method: ```Ruby @@ -602,7 +626,5 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -