cliutils 1.2.8 → 1.2.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +8 -0
- data/README.md +41 -25
- data/lib/cliutils/constants.rb +1 -1
- data/lib/cliutils/logger_delegator.rb +1 -1
- data/lib/cliutils/prefs/pref_behavior.rb +1 -1
- data/lib/cliutils/prefs/pref_validation.rb +33 -3
- data/res/readme-images/prefs-ask-behaviors.png +0 -0
- data/test/test_files/prefstest.yaml +1 -1
- metadata +2 -3
- data/lib/.DS_Store +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97a26794fd111748a7122e364fd21179f7d1f082
|
4
|
+
data.tar.gz: 78a1352fde30e33340c46340164ddc88a9bf526f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c92816990f22349112121b23694c85ff0d85b04b8420999ec1047fb6727acc688073b3eb321ef68bb94082972c0a69150b1292a1d5254014c05a7cd76ad671c7
|
7
|
+
data.tar.gz: c9d7ccaaee1a0416cd24030588c936ec285f1b7ea8ed56cc89e9c2c935631b20ab035443da84139a76c426368e33b8f7f540284091eab26b9325567b74722a7f
|
data/HISTORY.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
# 1.2.9 (2014-04-09)
|
2
|
+
|
3
|
+
* Added time Pref validator
|
4
|
+
* Added datetime Pref validator
|
5
|
+
* Added filepath_exists Pref validator
|
6
|
+
* Pref Validator change: numeric => number
|
7
|
+
* Pref Behavior change: local_filepath => expand_filepath
|
8
|
+
|
1
9
|
# 1.2.8 (2014-04-08)
|
2
10
|
|
3
11
|
* Global name changes
|
data/README.md
CHANGED
@@ -122,7 +122,7 @@ messenger.warn('Hey pal, you need to be careful.')
|
|
122
122
|
* `messenger.info`: used to show a formatted-blue infomational message.
|
123
123
|
* `messenger.section`: used to show a formatted-purple sectional message.
|
124
124
|
* `messenger.success`: used to show a formatted-green success message.
|
125
|
-
* `messenger.
|
125
|
+
* `messenger.warn`: used to show a formatted-yellow warning message.
|
126
126
|
|
127
127
|
Let's see an example that uses them all:
|
128
128
|
|
@@ -138,24 +138,37 @@ messenger.success('But Luke still blew up the Death Star!')
|
|
138
138
|
`messenger` also includes two "block" methods that allow you to wrap program execution in messages that are "longer-term".
|
139
139
|
|
140
140
|
```Ruby
|
141
|
-
|
142
|
-
|
141
|
+
# Outputs 'Starting up...', runs the code in
|
142
|
+
# `# do stuff here`, and once complete,
|
143
|
+
# outputs 'Done!' on the same line.
|
144
|
+
messenger.info_block('Starting up...', 'Done!', multiline = false) {
|
145
|
+
# ...do stuff here...
|
146
|
+
}
|
143
147
|
|
144
|
-
|
148
|
+
# Outputs 'MY SECTION' and then runs
|
149
|
+
# the code in `# do stuff here` before
|
150
|
+
# proceeding.
|
151
|
+
section_block('MY SECTION', multiline = true) {
|
152
|
+
# ...do stuff here...
|
153
|
+
}
|
154
|
+
```
|
145
155
|
|
146
156
|
### Message Wrapping
|
147
157
|
|
148
158
|
`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:
|
149
159
|
|
150
160
|
```Ruby
|
161
|
+
long_string = 'This is a really long message, okay? ' \
|
162
|
+
'It should wrap at some point. Seriously. Wrapping is nice.'
|
163
|
+
|
151
164
|
CLIUtils::PrettyIO.wrap_char_limit = 50
|
152
|
-
messenger.info(
|
165
|
+
messenger.info(long_string)
|
153
166
|
puts ''
|
154
167
|
CLIUtils::PrettyIO.wrap_char_limit = 20
|
155
|
-
messenger.info(
|
168
|
+
messenger.info(long_string)
|
156
169
|
puts ''
|
157
170
|
CLIUtils::PrettyIO.wrap = false
|
158
|
-
messenger.info(
|
171
|
+
messenger.info(long_string)
|
159
172
|
```
|
160
173
|
![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/wrapping.png "Text Wrapping")
|
161
174
|
|
@@ -208,7 +221,7 @@ E, [2014-03-29T15:14:34.844553 #4497] ERROR -- : This error should appear in STD
|
|
208
221
|
D, [2014-03-29T15:14:34.844609 #4497] DEBUG -- : This debug message should only appear in file.txt
|
209
222
|
```
|
210
223
|
|
211
|
-
Since you
|
224
|
+
Since you are attaching Logger objects, each can have it's own format and severity level. Cool!
|
212
225
|
|
213
226
|
## Configuration
|
214
227
|
|
@@ -242,9 +255,9 @@ configuration.delete_section(:program_data)
|
|
242
255
|
There are two ways data can be managed in `configurator`: via its `@data` property or via some magic methods; your call:
|
243
256
|
|
244
257
|
```Ruby
|
245
|
-
configuration.data[:user_data].merge!(username: 'bob')
|
258
|
+
configuration.data[:user_data].merge!({ username: 'bob', age: 45 })
|
246
259
|
# OR
|
247
|
-
configuration.user_data.merge!(username: 'bob')
|
260
|
+
configuration.user_data.merge!({ username: 'bob', age: 45 })
|
248
261
|
```
|
249
262
|
|
250
263
|
### Saving to a File
|
@@ -262,6 +275,7 @@ Note that all your keys are converted to strings before saving (and, likewise, a
|
|
262
275
|
app_data:
|
263
276
|
user_data:
|
264
277
|
username: bob
|
278
|
+
age: 45
|
265
279
|
```
|
266
280
|
|
267
281
|
### Checking Configuration Versions
|
@@ -292,8 +306,7 @@ app_data:
|
|
292
306
|
configuration.cur_version_key = :APP_VERSION
|
293
307
|
|
294
308
|
# Tell your configurator the name of the key that
|
295
|
-
# stores the last version that needed a configuration
|
296
|
-
# change.
|
309
|
+
# stores the last version that needed a configuration change.
|
297
310
|
# NOTE that you don't have to specify the section.
|
298
311
|
configuration.last_version_key = :NEWEST_CONFIG_VERSION
|
299
312
|
|
@@ -316,7 +329,7 @@ Many times, CLI apps need to ask their users some questions, collect the feedbac
|
|
316
329
|
|
317
330
|
### Basic Schema
|
318
331
|
|
319
|
-
`Prefs` can load preferences information from either a YAML file (via a filepath) or from an
|
332
|
+
`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:
|
320
333
|
|
321
334
|
* `prompt` (**required**): the string to prompt your user with
|
322
335
|
* `default` (*optional*): an optional default to offer
|
@@ -347,7 +360,7 @@ Assuming the above, `Prefs` is instantiated like so:
|
|
347
360
|
prefs = CLIUtils::Prefs.new('path/to/yaml/file')
|
348
361
|
```
|
349
362
|
|
350
|
-
`Prefs` can also be instantiated via a Hash or an
|
363
|
+
`Prefs` can also be instantiated via a Hash or an Array of prompts; the overall schema remains the same:
|
351
364
|
|
352
365
|
```Ruby
|
353
366
|
# Instantiation through a Hash
|
@@ -375,9 +388,9 @@ h = {
|
|
375
388
|
}
|
376
389
|
|
377
390
|
prefs = CLIUtils::Prefs.new(h)
|
378
|
-
|
391
|
+
```
|
392
|
+
```Ruby
|
379
393
|
# Instantiation through an Array
|
380
|
-
|
381
394
|
a = [
|
382
395
|
{
|
383
396
|
prompt: 'What is your name?',
|
@@ -502,12 +515,15 @@ prefs.ask
|
|
502
515
|
|
503
516
|
```YAML
|
504
517
|
validators:
|
505
|
-
- alphabetic
|
506
|
-
- alphanumeric
|
507
|
-
- date
|
508
|
-
-
|
509
|
-
-
|
510
|
-
-
|
518
|
+
- alphabetic # Must be made up of letters and spaces
|
519
|
+
- alphanumeric # Must be made up of letters, numbers, and spaces
|
520
|
+
- date # Must be a parsable date (e.g., 2014-04-03)
|
521
|
+
- datetime # Must be a parsable datetime (e.g., 2014-04-03 9:34am)
|
522
|
+
- non_nil # Must be a non-nil value
|
523
|
+
- number # Must be made up of numbers
|
524
|
+
- filepath_exists # Must be a local filepath that exists (e.g., `/tmp/`)
|
525
|
+
- time # Must be a parsable time (e.g., 12:45pm or 21:08)
|
526
|
+
- url # Must be a fully-qualified URL
|
511
527
|
```
|
512
528
|
|
513
529
|
An example:
|
@@ -523,7 +539,7 @@ prompts:
|
|
523
539
|
config_key: age
|
524
540
|
config_section: personal_info
|
525
541
|
validators:
|
526
|
-
-
|
542
|
+
- number
|
527
543
|
```
|
528
544
|
|
529
545
|
```Ruby
|
@@ -540,7 +556,7 @@ Finally, a common desire might be to modify the user's answer in some way before
|
|
540
556
|
```YAML
|
541
557
|
validators:
|
542
558
|
- capitalize # Turns "answer" into "Answer"
|
543
|
-
-
|
559
|
+
- expand_filepath # Runs File.expand_path on the answer
|
544
560
|
- lowercase # Turns "AnSwEr" into "answer"
|
545
561
|
- prefix: 'test ' # Prepends 'test ' to the answer
|
546
562
|
- suffix: 'test ' # Appends 'test ' to the answer
|
@@ -568,7 +584,7 @@ prefs.ask
|
|
568
584
|
```
|
569
585
|
![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/prefs-ask-behaviors.png "Behaviors")
|
570
586
|
|
571
|
-
Note that behaviors are executed in order, which might give you different results than you're expecting.
|
587
|
+
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).
|
572
588
|
|
573
589
|
### Adding Pref Responses to a Configurator
|
574
590
|
|
data/lib/cliutils/constants.rb
CHANGED
@@ -16,7 +16,7 @@ module CLIUtils
|
|
16
16
|
end
|
17
17
|
|
18
18
|
# Attaches a new target to delegate to.
|
19
|
-
# @param [
|
19
|
+
# @param [Hash] target A hash describing a reference key and a Logger
|
20
20
|
# @return [void]
|
21
21
|
def attach(target)
|
22
22
|
fail "Cannot add invalid target: #{ target }" unless target.is_a?(Hash)
|
@@ -8,7 +8,8 @@ module CLIUtils
|
|
8
8
|
# and a result message.
|
9
9
|
Validator = Struct.new(:code, :message)
|
10
10
|
|
11
|
-
# Validates that a value is only letters
|
11
|
+
# Validates that a value is only letters
|
12
|
+
# and spaces.
|
12
13
|
# @param [String] text The text to inspect
|
13
14
|
# @return [Boolean]
|
14
15
|
def self.alphabetic(text)
|
@@ -17,7 +18,8 @@ module CLIUtils
|
|
17
18
|
Validator.new(c, m)
|
18
19
|
end
|
19
20
|
|
20
|
-
# Validates that a value is only letters
|
21
|
+
# Validates that a value is only letters, numbers
|
22
|
+
# and spaces.
|
21
23
|
# @param [String] text The text to inspect
|
22
24
|
# @return [Boolean]
|
23
25
|
def self.alphanumeric(text)
|
@@ -35,6 +37,15 @@ module CLIUtils
|
|
35
37
|
Validator.new(c, m)
|
36
38
|
end
|
37
39
|
|
40
|
+
# Validates that a value is a datetime.
|
41
|
+
# @param [String] text The text to inspect
|
42
|
+
# @return [Boolean]
|
43
|
+
def self.datetime(text)
|
44
|
+
m = "Response is not a datetime: #{ text }"
|
45
|
+
c = !(DateTime.parse(text) rescue nil).nil?
|
46
|
+
Validator.new(c, m)
|
47
|
+
end
|
48
|
+
|
38
49
|
# Validates that a value is passed and is not
|
39
50
|
# empty.
|
40
51
|
# @param [String] text The text to inspect
|
@@ -48,12 +59,31 @@ module CLIUtils
|
|
48
59
|
# Validates that a value is some sort of number.
|
49
60
|
# @param [String] text The text to inspect
|
50
61
|
# @return [Boolean]
|
51
|
-
def self.
|
62
|
+
def self.number(text)
|
52
63
|
m = "Response is not a number: #{ text }"
|
53
64
|
c = text.to_s =~ /\A[-+]?\d*\.?\d+\z/
|
54
65
|
Validator.new(c, m)
|
55
66
|
end
|
56
67
|
|
68
|
+
# Validates that a filepath exists on the
|
69
|
+
# local filesystem.
|
70
|
+
# @param [String] text The text to inspect
|
71
|
+
# @return [Boolean]
|
72
|
+
def self.filepath_exists(text)
|
73
|
+
m = "Path does not exist locally: #{ text }"
|
74
|
+
c = Pathname.new(text).exist?
|
75
|
+
Validator.new(c, m)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Validates that a value is a time.
|
79
|
+
# @param [String] text The text to inspect
|
80
|
+
# @return [Boolean]
|
81
|
+
def self.time(text)
|
82
|
+
m = "Response is not a time: #{ text }"
|
83
|
+
c = !(Time.parse(text) rescue nil).nil?
|
84
|
+
Validator.new(c, m)
|
85
|
+
end
|
86
|
+
|
57
87
|
# Validates that passed value is a URL.
|
58
88
|
# @param [String] text The text to inspect
|
59
89
|
# @return [Boolean]
|
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cliutils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Bach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -82,7 +82,6 @@ files:
|
|
82
82
|
- README.md
|
83
83
|
- Rakefile
|
84
84
|
- cliutils.gemspec
|
85
|
-
- lib/.DS_Store
|
86
85
|
- lib/cliutils.rb
|
87
86
|
- lib/cliutils/configuration.rb
|
88
87
|
- lib/cliutils/configurator.rb
|
data/lib/.DS_Store
DELETED
Binary file
|