cliutils 1.4.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/HISTORY.md +9 -0
  4. data/README.md +15 -639
  5. data/lib/cliutils/constants.rb +1 -1
  6. data/lib/cliutils/messaging.rb +112 -0
  7. data/lib/cliutils/prefs/pref.rb +113 -66
  8. data/lib/cliutils/prefs/pref_actions/open_url_action.rb +6 -2
  9. data/lib/cliutils/prefs/pref_actions/pref_action.rb +18 -9
  10. data/lib/cliutils/prefs/pref_behaviors/capitalize_behavior.rb +11 -0
  11. data/lib/cliutils/prefs/pref_behaviors/expand_filepath_behavior.rb +12 -0
  12. data/lib/cliutils/prefs/pref_behaviors/lowercase_behavior.rb +11 -0
  13. data/lib/cliutils/prefs/pref_behaviors/pref_behavior.rb +26 -0
  14. data/lib/cliutils/prefs/pref_behaviors/prefix_behavior.rb +11 -0
  15. data/lib/cliutils/prefs/pref_behaviors/suffix_behavior.rb +11 -0
  16. data/lib/cliutils/prefs/pref_behaviors/titlecase_behavior.rb +11 -0
  17. data/lib/cliutils/prefs/pref_behaviors/uppercase_behavior.rb +11 -0
  18. data/lib/cliutils/prefs/pref_validators/alphabetic_validator.rb +13 -0
  19. data/lib/cliutils/prefs/pref_validators/alphanumeric_validator.rb +14 -0
  20. data/lib/cliutils/prefs/pref_validators/date_validator.rb +13 -0
  21. data/lib/cliutils/prefs/pref_validators/datetime_validator.rb +13 -0
  22. data/lib/cliutils/prefs/pref_validators/filepath_exists_validator.rb +13 -0
  23. data/lib/cliutils/prefs/pref_validators/non_nil_validator.rb +13 -0
  24. data/lib/cliutils/prefs/pref_validators/number_validator.rb +13 -0
  25. data/lib/cliutils/prefs/pref_validators/pref_validator.rb +31 -0
  26. data/lib/cliutils/prefs/pref_validators/time_validator.rb +13 -0
  27. data/lib/cliutils/prefs/pref_validators/url_validator.rb +15 -0
  28. data/lib/cliutils/pretty_io.rb +0 -112
  29. data/lib/cliutils.rb +1 -0
  30. data/test/configurator_test.rb +1 -1
  31. data/test/hash_extensions_test.rb +1 -1
  32. data/test/logger_extensions_test.rb +1 -1
  33. data/test/messenging_test.rb +1 -1
  34. data/test/prefs_test.rb +1 -1
  35. data/test/string_extesions_test.rb +1 -1
  36. data/test/test_files/prefstest.yaml +29 -9
  37. data/test/test_helper.rb +6 -0
  38. metadata +22 -4
  39. data/lib/cliutils/prefs/pref_behavior.rb +0 -58
  40. data/lib/cliutils/prefs/pref_validation.rb +0 -96
data/README.md CHANGED
@@ -13,9 +13,19 @@ It's fairly simple:
13
13
  2. I found myself copy/pasting common code from one to another.
14
14
  3. I decided to do something about it.
15
15
 
16
- # Can I use it with...?
16
+ # Libraries
17
17
 
18
- I often get asked how nicely CLIUtils plays with other Ruby-based CLI libraries (like Dave Copeland's excellent [Methadone](https://github.com/davetron5000/methadone "Methadone") and [GLI](https://github.com/davetron5000/gli "GLI")). Answer: they play very nicely. I use CLIUtils in [Sifttter Redux](https://github.com/bachya/Sifttter-Redux "Sifttter Redux") (which is built on GLI) and [ExpandSync](https://github.com/bachya/ExpandSync "ExpandSync") (which is built on Methadone).
18
+ CLIUtils offers:
19
+
20
+ * **PrettyIO:** nicer-looking CLI messages
21
+ * **Messaging:** a system to display nicely-formatted messages to one or more tagets
22
+ * **Configuration:** a app configuration manager
23
+ * **Prefs:** a preferences prompter and manager
24
+
25
+ ...along with plenty of documentation:
26
+
27
+ * [Wiki](https://github.com/bachya/cliutils/wiki "CLIUtils Wiki")
28
+ * [YARD documentation](http://rubydoc.info/github/bachya/cliutils/master/frames "YARD Documentation")
19
29
 
20
30
  # Installation
21
31
 
@@ -39,7 +49,7 @@ $ gem install cliutils
39
49
 
40
50
  # Usage
41
51
 
42
- ```rub
52
+ ```Ruby
43
53
  require 'cliutils'
44
54
  ```
45
55
 
@@ -49,643 +59,9 @@ If you want to mix in everything that CLIUtils has to offer:
49
59
  include CLIUtils
50
60
  ```
51
61
 
52
- Alternatively, as described below, mix in only the libraries that you want.
53
-
54
- Note that although this README.md is extensive, it may not cover all methods. Check out the [YARD documentation](http://rubydoc.info/github/bachya/cliutils/master/frames) and the [tests](https://github.com/bachya/cliutils/tree/master/test) to see more examples.
55
-
56
- # Libraries
57
-
58
- CLIUtils offers:
59
-
60
- * [PrettyIO](#prettyio): nicer-looking CLI messages
61
- * [Messaging](#messaging): a system to display nicely-formatted messages to one or more tagets
62
- * [Configuration](#configuration): a app configuration manager
63
- * [Prefs](#prefs): a preferences prompter and manager
64
-
65
- # PrettyIO
66
-
67
- First stop on our journey is better client IO via `PrettyIO`. To activate, simply mix into your project:
68
-
69
- ```ruby
70
- include CLIUtils::PrettyIO
71
- ```
72
-
73
- ## Colored Strings
74
-
75
- To start, `PrettyIO` affords you colorized strings:
76
-
77
- ```ruby
78
- puts 'A sample string'.red
79
- ```
80
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/prettyio-red-text.png "Colored Text via PrettyIO")
81
-
82
- You get a stable of utility methods for the common ANSI color codes:
83
-
84
- ```ruby
85
- String.blue
86
- String.cyan
87
- String.green
88
- String.purple
89
- String.red
90
- String.white
91
- String.yellow
92
- ```
93
-
94
- You also get the `colorize` method, which allows you to define more complex color combinations. For example, to get some nice purple text on a gnarly green background:
95
-
96
- ```ruby
97
- puts 'A sample string'.colorize('35;42')
98
- ```
99
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/prettyio-gnarly-text.png "Complex Colored Text via PrettyIO")
100
-
101
- Naturally, memorizing the ANSI color scheme is a pain, so `PrettyIO` gives you a convenient method to look up these color combinations:
102
-
103
- ```ruby
104
- color_chart
105
- ```
106
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/prettyio-color-chart.png "PrettyIO Color Chart")
107
-
108
- # Messaging
109
-
110
- Throughout the life of your application, you will most likely want to send several messages to your user (warnings, errors, info, etc.). `Messaging` makes this a snap. It, too, is a mixin:
111
-
112
- ```ruby
113
- include CLIUtils::Messaging
114
- ```
115
-
116
- Once mixed in, you get access to `messenger`, a type of Logger that uses `PrettyIO` to send nicely-formatted messages to your user. For example, if you'd like to warn your user:
117
-
118
- ```ruby
119
- messenger.warn('Hey pal, you need to be careful.')
120
- ```
121
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/messenger-warn.png "A Warning from Messenger")
122
-
123
- ## Messaging Methods
124
-
125
- `messenger` gives you access to several basic "read-only" methods:
126
-
127
- * `messenger.error`: used to show a formatted-red error message.
128
- * `messenger.info`: used to show a formatted-blue infomational message.
129
- * `messenger.section`: used to show a formatted-purple sectional message.
130
- * `messenger.success`: used to show a formatted-green success message.
131
- * `messenger.warn`: used to show a formatted-yellow warning message.
132
-
133
- Let's see an example that uses them all:
134
-
135
- ```Ruby
136
- messenger.section('STARTING ATTACK RUN...')
137
- messenger.info('Beginning strafing run...')
138
- messenger.warn('WARNING: Tie Fighters approaching!')
139
- messenger.error('Porkins died :(')
140
- messenger.success('But Luke still blew up the Death Star!')
141
- ```
142
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/messenger-types-1.png "Basic Messenger Types")
143
-
144
- `messenger` also includes two "block" methods that allow you to wrap program execution in messages that are "longer-term".
145
-
146
- ```Ruby
147
- # Outputs 'Starting up...', runs the code in
148
- # `# do stuff here`, and once complete,
149
- # outputs 'Done!' on the same line.
150
- messenger.info_block('Starting up...', 'Done!', multiline = false) {
151
- # ...do stuff here...
152
- }
153
-
154
- # Outputs 'MY SECTION' and then runs
155
- # the code in `# do stuff here` before
156
- # proceeding.
157
- section_block('MY SECTION', multiline = true) {
158
- # ...do stuff here...
159
- }
160
- ```
161
-
162
- ## Message Wrapping
163
-
164
- `PrettyIO` also gives `messenger` the ability to wrap your messages so that they don't span off into infinity. You can even control what the wrap limit (in characters) is:
165
-
166
- ```Ruby
167
- long_string = 'This is a really long message, okay? ' \
168
- 'It should wrap at some point. Seriously. Wrapping is nice.'
169
-
170
- CLIUtils::PrettyIO.wrap_char_limit = 50
171
- messenger.info(long_string)
172
- puts ''
173
- CLIUtils::PrettyIO.wrap_char_limit = 20
174
- messenger.info(long_string)
175
- puts ''
176
- CLIUtils::PrettyIO.wrap = false
177
- messenger.info(long_string)
178
- ```
179
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/wrapping.png "Text Wrapping")
180
-
181
- ## Prompting
182
-
183
- `messenger` also carries a convenient method to prompt your users to give input (including an optional default). It makes use of `readline`, so you can do cool things like text expansion of paths.
184
-
185
- ```Ruby
186
- p = messenger.prompt('Are you a fan of Battlestar Galactica?', default = 'Y')
187
- messenger.info("You answered: #{ p }")
188
- ```
189
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/prompting.png "Prompting")
190
-
191
- When you pass a default to `messaging.prompt`, hitting `Enter` (i.e., leaving the prompt blank) will return the value of the default.
192
-
193
- ## Logging
194
-
195
- Often, it's desirable to log messages as they appear to your user. `Messaging` makes this a breeze by allowing you to attach and detach Logger instances at will.
196
-
197
- For instance, let's say you wanted to log a few messages to both your user's STDOUT and to `file.txt`:
198
-
199
- ```Ruby
200
- # By default, messenger only outputs to STDOUT.
201
- messenger.info('This should only appear in STDOUT.')
202
-
203
- # messenger.attach takes a Hash of string/symbol keys
204
- # and Logger values (so you can refer to them later on).
205
- messenger.attach(MY_FILE_LOGGER: Logger.new('file.txt'))
206
-
207
- messenger.warn('This warning should appear in STDOUT and file.txt')
208
- messenger.error('This error should appear in STDOUT and file.txt')
209
- messenger.debug('This debug message should only appear in file.txt')
210
-
211
- # messenger.detach takes the string/symbol key
212
- # defined earlier.
213
- messenger.detach(:MY_FILE_LOGGER)
214
-
215
- messenger.section('This section message should appear only in STDOUT')
216
- ```
217
-
218
- In STDOUT:
219
-
220
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/multi-logger.png "Multi-logging")
221
-
222
- ...and in `file.txt`:
223
-
224
- ```
225
- W, [2014-03-29T15:14:34.844406 #4497] WARN -- : This warning should appear in STDOUT and file.txt
226
- E, [2014-03-29T15:14:34.844553 #4497] ERROR -- : This error should appear in STDOUT and file.txt
227
- D, [2014-03-29T15:14:34.844609 #4497] DEBUG -- : This debug message should only appear in file.txt
228
- ```
229
-
230
- Since you are attaching Logger objects, each can have it's own format and severity level. Cool!
231
-
232
- # Configuration
233
-
234
- CLIUtils offers a `Configurator` class and a `Configuration` module (which provides access to a shared instance of `Configurator`) that make managing a user's configuration parameters easy. Although the examples in this README use that shared instance, you should note that you can always use your own `Configurator`.
235
-
236
- To use, mix it in!
237
-
238
- ```Ruby
239
- include CLIUtils::Configuration
240
- ```
241
-
242
- ## Loading a Configuration File
243
-
244
- ```Ruby
245
- load_configuration('~/.my-app-config')
246
- ```
247
-
248
- If there's data in there, it will be consumed into the `Configurator`'s `data` property.
249
-
250
- ## Adding/Removing Sections
251
-
252
- Sections are top levels of the configuration file and are managed via the `Configurator` object:
253
-
254
- ```Ruby
255
- configuration.add_section(:app_data)
256
- configuration.add_section(:user_data)
257
- configuration.add_section(:program_data)
258
- configuration.delete_section(:program_data)
259
- ```
260
-
261
- ## Adding Data to Sections
262
-
263
- There are two ways data can be managed in the `Configurator`: via its `@data` property or via some magic methods; your call:
264
-
265
- ```Ruby
266
- configuration.data[:user_data].merge!({ username: 'bob', age: 45 })
267
- # OR
268
- configuration.user_data.merge!({ username: 'bob', age: 45 })
269
- ```
270
-
271
- ## Saving to a File
272
-
273
- When you're ready to save your configuration data to a YAML file:
274
-
275
- ```Ruby
276
- configuration.save
277
- ```
278
-
279
- Note that all your keys are converted to strings before saving (and, likewise, are converted to symbols, when loading). Assuming we used the commands above, we could expect this to be the contents of `~/.my-app-config`:
280
-
281
- ```YAML
282
- ---
283
- app_data:
284
- user_data:
285
- username: bob
286
- age: 45
287
- ```
288
-
289
- ## Checking Configuration Versions
290
-
291
- Often, you'll want to check the user's current version of your app against the last version that required some sort of configuration change; moreover, you'll want to run some "re-configuration" steps if the user's version is older than the last version that required a configuration update.
292
-
293
- The `Configurator` allows for this via its `compare_version` method.
294
-
295
- Assume you have a config file that looks like this:
296
-
297
- ```YAML
298
- ---
299
- app_data:
300
- # The current version of the app
301
- APP_VERSION: 0.8.8
302
-
303
- # ...other keys...
304
- ```
305
-
306
- ...and that, somewhere in your app, you store a constant that contains the config version that requires a re-configuration:
307
-
308
- ```Ruby
309
- LATEST_CONFIG_VERSION = 0.9.5
310
- ```
311
-
312
- ...this will initiate a version check (and give you the option to do something with that information):
313
-
314
- ```Ruby
315
- # Store the current version of your app in a
316
- # property of the Configurator.
317
- configuration.current_version = configuration.app_data['APP_VERSION']
318
-
319
- # Store the last version of your app that required
320
- # a re-configuration in a property of the Configurator.
321
- configuration.last_version = LATEST_CONFIG_VERSION
322
-
323
- # Run the check. If the current version is older than
324
- # the "last" version, use a block to tell the Configurator
325
- # what to do.
326
- configuration.compare_version do |c, l|
327
- puts "We need to update from #{c} to #{l}..."
328
- # ...do stuff...
329
- end
330
- ```
331
-
332
- Two items to note:
333
-
334
- 1. If the `current_version` parameter is `nil`, the Configurator will assume that it the app needs to be updated when `compare_version` is run.
335
- 2. Note that if the current version is *later* than the last version that required re-configuration, the whole block is skipped over (allowing your app to get on with its day).
336
-
337
- # Prefs
338
-
339
- Many times, CLI apps need to ask their users some questions, collect the feedback, validate it, and store it. CLIUtils makes this a breeze via the `Prefs` class.
340
-
341
- ## Basic Schema
342
-
343
- `Prefs` can load preferences information from either a YAML file (via a filepath) or from an Array of preferences. In either case, the schema is the same; each prompt includes the following:
344
-
345
- * `prompt_text` (**required**): the string to prompt your user with
346
- * `default` (*optional*): an optional default to offer
347
- * `config_key` (**required**): the Configurator key that this preference will use
348
- * `config_section` (**required**): the Configurator section that this preference applies to
349
-
350
- Here's an example YAML preferences file.
351
-
352
- ```YAML
353
- prompts:
354
- - prompt_text: What is your name?
355
- default: Bob Cobb
356
- config_key: name
357
- config_section: personal_info
358
- - prompt_text: What is your age?
359
- default: 45
360
- config_key: age
361
- config_section: personal_info
362
- - prompt_text: Batman or Superman?
363
- default: Batman
364
- config_key: superhero
365
- config_section: personal_info
366
- ```
367
-
368
- Assuming the above, `Prefs` is instantiated like so:
369
-
370
- ```Ruby
371
- prefs = CLIUtils::Prefs.new('path/to/yaml/file')
372
- ```
373
-
374
- `Prefs` can also be instantiated via a Hash or an Array of prompts; the overall schema remains the same:
375
-
376
- ```Ruby
377
- # Instantiation through a Hash
378
- h = {
379
- prompts: [
380
- {
381
- prompt_text: 'What is your name?',
382
- default: 'Bob Cobb',
383
- config_key: :name,
384
- config_section: :personal_info
385
- },
386
- {
387
- prompt_text: 'What is your age?',
388
- default: '45',
389
- config_key: :age,
390
- config_section: :personal_info
391
- },
392
- {
393
- prompt_text: 'Batman or Superman?',
394
- default: 'Batman',
395
- config_key: :superhero,
396
- config_section: :personal_info
397
- }
398
- ]
399
- }
400
-
401
- prefs = CLIUtils::Prefs.new(h)
402
- ```
403
- ```Ruby
404
- # Instantiation through an Array
405
- a = [
406
- {
407
- prompt_text: 'What is your name?',
408
- default: 'Bob Cobb',
409
- config_key: :name,
410
- config_section: :personal_info
411
- },
412
- {
413
- prompt_text: 'What is your age?',
414
- default: '45',
415
- config_key: :age,
416
- config_section: :personal_info
417
- },
418
- {
419
- prompt_text: 'Batman or Superman?',
420
- default: 'Batman',
421
- config_key: :superhero,
422
- config_section: :personal_info
423
- }
424
- ]
425
-
426
- prefs = CLIUtils::Prefs.new(a)
427
- ```
428
-
429
- ## Prompting the User
430
-
431
- With valid preferences loaded, simply use `ask` to begin prompting your user:
432
-
433
- ```Ruby
434
- prefs.ask
435
- ```
436
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/prefs-ask.png "Prefs.ask")
437
-
438
- ## Prerequisites
439
-
440
- Sometimes, you need to answer certain prompts before others become relevant. `Prefs` allows this via a `prereqs` key, which can contain multiple already-answered key/value pairs to check for. For instance, imagine we want to drill into a user's superhero preference a bit further; using our previously-used YAML prefs file:
441
-
442
- ```YAML
443
- prompts:
444
- - prompt_text: Batman or Superman?
445
- default: Batman
446
- config_key: superhero
447
- config_section: personal_info
448
- - prompt_text: Do you feel smart for preferring Batman?
449
- default: Y
450
- config_key: batman_answer
451
- config_section: personal_info
452
- prereqs:
453
- - config_key: superhero
454
- config_value: Batman
455
- - prompt_text: Why do you prefer Superman?!
456
- default: No clue
457
- config_key: superman_answer
458
- config_section: personal_info
459
- prereqs:
460
- - config_key: superhero
461
- config_value: Superman
462
- - prompt_text: Why don't you have a clue?
463
- config_key: no_clue
464
- config_section: personal_info
465
- prereqs:
466
- - config_key: superhero
467
- config_value: Superman
468
- - config_key: superman_answer
469
- config_value: No clue
470
- ```
471
-
472
- `prereqs` checks for already-answered preferences (based on a Configurator key and value); assuming everything checks out, the subsequent preferences are collected:
473
-
474
- ```Ruby
475
- prefs.ask
476
- ```
477
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/prefs-ask-prereqs.png "Prerequisities")
478
-
479
- Be careful that you don't define any circular prerequisities (e.g., A requires B and B requires A). In that case, both preferences will be ignored by `Prefs.ask`.
480
-
481
- ## Options
482
-
483
- What if you want to limit a preference to a certain set of options? Easy! Imagine we want to expand the previous example and force the user to choose either "Batman" or "Superman":
484
-
485
- ```YAML
486
- prompts:
487
- - prompt_text: Batman or Superman?
488
- default: Batman
489
- config_key: superhero
490
- config_section: personal_info
491
- options: ['Batman', 'Superman']
492
- - prompt_text: Do you feel smart for preferring Batman?
493
- default: Y
494
- config_key: batman_answer
495
- config_section: personal_info
496
- prereqs:
497
- - config_key: superhero
498
- config_value: Batman
499
- - prompt_text: Why do you prefer Superman?!
500
- default: No clue
501
- config_key: superman_answer
502
- config_section: personal_info
503
- prereqs:
504
- - config_key: superhero
505
- config_value: Superman
506
- - prompt_text: Why don't you have a clue?
507
- config_key: no_clue
508
- config_section: personal_info
509
- prereqs:
510
- - config_key: superhero
511
- config_value: Superman
512
- - config_key: superman_answer
513
- config_value: No clue
514
- ```
515
-
516
- Once in place:
517
-
518
- ```Ruby
519
- prefs.ask
520
- ```
521
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/prefs-ask-options.png "Options")
522
-
523
- ## Validators
524
-
525
- "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:
526
-
527
- ```YAML
528
- validators:
529
- - alphabetic # Must be made up of letters and spaces
530
- - alphanumeric # Must be made up of letters, numbers, and spaces
531
- - date # Must be a parsable date (e.g., 2014-04-03)
532
- - datetime # Must be a parsable datetime (e.g., 2014-04-03 9:34am)
533
- - non_nil # Must be a non-nil value
534
- - number # Must be made up of numbers
535
- - filepath_exists # Must be a local filepath that exists (e.g., `/tmp/`)
536
- - time # Must be a parsable time (e.g., 12:45pm or 21:08)
537
- - url # Must be a fully-qualified URL
538
- ```
539
-
540
- An example:
541
-
542
- ```YAML
543
- prompts:
544
- - prompt_text: What is your name?
545
- config_key: name
546
- config_section: personal_info
547
- validators:
548
- - alphabetic
549
- - prompt_text: How old are you?
550
- config_key: age
551
- config_section: personal_info
552
- validators:
553
- - number
554
- ```
555
-
556
- ```Ruby
557
- prefs.ask
558
- ```
559
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/prefs-ask-validators.png "Validators")
560
-
561
- 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.
562
-
563
- ## Behaviors
564
-
565
- 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:
566
-
567
- ```YAML
568
- validators:
569
- - capitalize # Turns "answer" into "Answer"
570
- - expand_filepath # Runs File.expand_path on the answer
571
- - lowercase # Turns "AnSwEr" into "answer"
572
- - prefix: 'test ' # Prepends 'test ' to the answer
573
- - suffix: 'test ' # Appends 'test ' to the answer
574
- - titlecase # Turns "the answer" into "The Answer"
575
- - uppercase # Turns "answer" to "ANSWER"
576
- ```
577
-
578
- An example:
579
-
580
- ```YAML
581
- prompts:
582
- - prompt_text: What is your favorite food?
583
- config_key: food
584
- config_section: personal_info
585
- validators:
586
- - non_nil
587
- behaviors:
588
- - uppercase
589
- - prefix: 'My favorite food: '
590
- - suffix: ' (soooo good!)'
591
- ```
592
-
593
- ```Ruby
594
- prefs.ask
595
- ```
596
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/prefs-ask-behaviors.png "Behaviors")
597
-
598
- Note that behaviors are executed in order, which might give you different results than you're expecting. Using the YAML above, for example, placing the `uppercase` behavior last in the list will uppercase *the entire string* (including prefix and suffix).
599
-
600
- ## Pre- and Post-Messages/Actions
601
-
602
- `Prefs` allows you to define messages and "action" plugins that can be executed before and after the prompt is given.
603
-
604
- For example, imagine that before delivering a prompt, we want to open the user's default browser (notfying the user first, of course) so that they can collect some information before coming back. Once the user puts that information into the prompt, we want to provide a close-out message. We can accomplish this by writing a set of pre- and post-messages/actions on the prompt.
605
-
606
- ### Defining the Schema
607
-
608
- ```YAML
609
- prompts:
610
- - prompt_text: What is the top story on espn.com?
611
- config_key: top_story
612
- config_section: app_data
613
- pre:
614
- message: 'I will now open espn.com in your default browser.'
615
- action: ~/.cliutils_plugins/open_url.rb
616
- action_parameters:
617
- - url: http://www.espn.com
618
- post:
619
- message: 'Thanks for inputting!'
620
- validators:
621
- - non_nil
622
- ```
623
-
624
- Both `pre` and `post` keys can contain up to 3 child keys:
625
-
626
- * `message` (**required**): the message to display in the pre or post stage
627
- * `action` (*optional*): the name of the action to execute in the pre or post stage
628
- * `action_parameters` (*optional*): an array of key/value pairs to be used in the action
629
-
630
- ### Creating the Action
631
-
632
- `Prefs` actions are simply Ruby classes. A simple `open_url` action might be look like this:
633
-
634
- ```Ruby
635
- module CLIUtils
636
- class OpenUrlAction < PrefAction
637
- def run(parameters)
638
- `open #{ parameters[:url] }`
639
- end
640
- end
641
- end
642
- ```
643
-
644
- Several items to note:
645
-
646
- 1. The action class needs to be wrapped in the CLIUtils module. In addition to "locking in" the action, this wrapper module gives easier access to PrettyIO, Messaging, etc.
647
- 2. The class name needs to be the camel-case version of the `action` key in the YAML.
648
- 3. The class name needs to end with "Action".
649
- 4. The class needs to inherit from the PrefAction class. This allows the action to have access to:
650
- * `@pref`: the Pref that implemented this action
651
- 5. The class needs to implement one method: `run(parameters = {})`
652
-
653
- ### Testing
654
-
655
- Let's run it!
656
-
657
- ```Ruby
658
- prefs.ask
659
- ```
660
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/actions-1.png "Pre-action")
661
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/actions-2.png "Action")
662
- ![alt text](https://raw.githubusercontent.com/bachya/cliutils/master/res/readme-images/actions-3.png "Post-action")
663
-
664
- ## Adding Pref Responses to a Configurator
665
-
666
- Once the user has answered all the preference prompts, you can fold those answers back into a Configurator using the `ingest` method:
667
-
668
- ```Ruby
669
- # Ingest the Prefs answers into our
670
- # Configurator.
671
- configuration.ingest_prefs(prefs)
672
-
673
- # Save it to the filesystem.
674
- configuration.save
675
- ```
676
-
677
- ## Using Configurator Values as Defaults
678
-
679
- Note that you can also initialize a `Prefs` object with a Configurator:
680
-
681
- ```Ruby
682
- # Scans my_configurator for values to the
683
- # questions posed by prefs; if found, use
684
- # them as defaults.
685
- prefs = CLIUtils::Prefs.new('path/to/yaml/file', my_configurator)
686
- ```
62
+ # Can I use it with...?
687
63
 
688
- In this case, `Prefs` will look to see if any values already exist for a specific prompt; if so, that value will be used as the default, rather than the default specified in the YAML.
64
+ I often get asked how nicely CLIUtils plays with other Ruby-based CLI libraries (like Dave Copeland's excellent [Methadone](https://github.com/davetron5000/methadone "Methadone") and [GLI](https://github.com/davetron5000/gli "GLI")). Answer: they play very nicely. I use CLIUtils in [Sifttter Redux](https://github.com/bachya/Sifttter-Redux "Sifttter Redux") (which is built on GLI) and [ExpandSync](https://github.com/bachya/ExpandSync "ExpandSync") (which is built on Methadone).
689
65
 
690
66
  # Known Issues
691
67
 
@@ -1,5 +1,5 @@
1
1
  # Stores constants to use.
2
2
  module CLIUtils
3
3
  # The current version of the gem
4
- VERSION = '1.4.2'
4
+ VERSION = '2.0.0'
5
5
  end