cliutils 1.2.6 → 1.2.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +4 -0
- data/README.md +41 -19
- data/lib/cliutils/constants.rb +1 -1
- data/lib/cliutils/prefs/pref.rb +9 -3
- data/lib/cliutils/prefs/pref_behavior.rb +47 -3
- data/res/readme-images/prefs-ask-behaviors.png +0 -0
- data/test/test_files/prefstest.yaml +9 -27
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac9423ddd995d1468da91eff3b3abf630b7c5aa1
|
4
|
+
data.tar.gz: 6e430afb3d34414ee453b4fa70be21878f9a8c2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d96e9df275ed3a6272ae23c85023c82671b6af7e1168867a1f675904b50369e0567bd47b6e4c1183f471f928b56c20c4ba21883b82af35d5386e2fc240791218
|
7
|
+
data.tar.gz: 28cb0e9a8af04179edefaba915f7ecb6f3fcddc8d33e460323c28cb11924ebb5ae476578fcc74e023389246d09851c25e58d21cce12bc5ec7478a7a253b5a255
|
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -498,7 +498,20 @@ prefs.ask
|
|
498
498
|
|
499
499
|
### Validators
|
500
500
|
|
501
|
-
"But," you say, "I want to ensure that my user gives answers that conform to certain specifications!" Not a problem, dear user
|
501
|
+
"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:
|
502
|
+
|
503
|
+
```YAML
|
504
|
+
validators:
|
505
|
+
- numeric # Must be a number
|
506
|
+
- alphabetic # Must be made up of letters and spaces
|
507
|
+
- alphanumeric # Must be made up of letters, numbers, and spaces
|
508
|
+
- date # Must be a parsable date (e.g., 2014-04-03)
|
509
|
+
- non_nil # Must be a non-nil value
|
510
|
+
- numeric # Must be made up of numbers
|
511
|
+
- url # Must be a fully-qualified URL
|
512
|
+
```
|
513
|
+
|
514
|
+
An example:
|
502
515
|
|
503
516
|
```YAML
|
504
517
|
prompts:
|
@@ -519,33 +532,44 @@ prefs.ask
|
|
519
532
|
```
|
520
533
|
![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/prefs-ask-validators.png "Validators")
|
521
534
|
|
522
|
-
`
|
523
|
-
|
524
|
-
* `alphabetic`: must be made up of letters and spaces
|
525
|
-
* `alphanumeric`: must be made up of letters, numbers, and spaces
|
526
|
-
* `date`: must be a parsable date (e.g., 2014-04-03)
|
527
|
-
* `non_nil`: must be a non-nil value
|
528
|
-
* `numeric`: must be made up of numbers
|
529
|
-
* `url`: must be a fully-qualified URL
|
535
|
+
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.
|
530
536
|
|
531
537
|
### Behaviors
|
532
538
|
|
533
|
-
Finally, a common desire might be to modify the user's answer in some way:
|
539
|
+
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:
|
540
|
+
|
541
|
+
```YAML
|
542
|
+
validators:
|
543
|
+
- capitalize # Turns "answer" into "Answer"
|
544
|
+
- local_filepath # Runs File.expand_path on the answer
|
545
|
+
- lowercase # Turns "AnSwEr" into "answer"
|
546
|
+
- prefix: 'test ' # Prepends 'test ' to the answer
|
547
|
+
- suffix: 'test ' # Appends 'test ' to the answer
|
548
|
+
- titlecase # Turns "the answer" into "The Answer"
|
549
|
+
- uppercase # Turns "answer" to "ANSWER"
|
550
|
+
```
|
551
|
+
|
552
|
+
An example:
|
534
553
|
|
535
554
|
```YAML
|
536
555
|
prompts:
|
537
|
-
- prompt:
|
538
|
-
config_key:
|
556
|
+
- prompt: What is your favorite food?
|
557
|
+
config_key: food
|
539
558
|
config_section: personal_info
|
559
|
+
validators:
|
560
|
+
- non_nil
|
540
561
|
behaviors:
|
541
|
-
-
|
562
|
+
- uppercase
|
563
|
+
- prefix: 'My favorite food: '
|
564
|
+
- suffix: ' (soooo good!)'
|
542
565
|
```
|
543
566
|
|
544
|
-
|
545
|
-
|
546
|
-
|
567
|
+
```Ruby
|
568
|
+
prefs.ask
|
569
|
+
```
|
570
|
+
![alt text](https://raw.githubusercontent.com/bachya/cli-utils/master/res/readme-images/prefs-ask-behaviors.png "Behaviors")
|
547
571
|
|
548
|
-
|
572
|
+
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).
|
549
573
|
|
550
574
|
### Adding Pref Responses to a Configurator
|
551
575
|
|
@@ -604,5 +628,3 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
604
628
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
605
629
|
|
606
630
|
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.
|
607
|
-
|
608
|
-
|
data/lib/cliutils/constants.rb
CHANGED
data/lib/cliutils/prefs/pref.rb
CHANGED
@@ -70,9 +70,15 @@ module CLIUtils
|
|
70
70
|
def evaluate_behaviors(text)
|
71
71
|
if @behaviors
|
72
72
|
modified_text = text
|
73
|
-
@behaviors.each do |
|
74
|
-
if
|
75
|
-
|
73
|
+
@behaviors.each do |method|
|
74
|
+
if method.is_a?(Hash)
|
75
|
+
parameter = method.values[0]
|
76
|
+
method = method.keys[0]
|
77
|
+
end
|
78
|
+
|
79
|
+
args = [modified_text, parameter]
|
80
|
+
if PrefBehavior.respond_to?(method)
|
81
|
+
modified_text = PrefBehavior.send(method, *args)
|
76
82
|
else
|
77
83
|
messenger.warn("Skipping undefined Pref behavior: #{ b }")
|
78
84
|
end
|
@@ -3,12 +3,56 @@ module CLIUtils
|
|
3
3
|
# Behaviors that should be applied to a Pref's
|
4
4
|
# final value
|
5
5
|
module PrefBehavior
|
6
|
+
# Capitalizes the first word of the passed text.
|
7
|
+
# @param [String] args[0] The text to evaluate
|
8
|
+
# @return [String]
|
9
|
+
def self.capitalize(*args)
|
10
|
+
args[0].capitalize
|
11
|
+
end
|
12
|
+
|
6
13
|
# Expands the passed text (assumes it
|
7
14
|
# is a filepath).
|
8
|
-
# @param [String]
|
15
|
+
# @param [String] args[0] The text to evaluate
|
16
|
+
# @return [String]
|
17
|
+
def self.local_filepath(*args)
|
18
|
+
File.expand_path(args[0])
|
19
|
+
end
|
20
|
+
|
21
|
+
# Lowercases all characters in the passed text.
|
22
|
+
# @param [String] args[0] The text to evaluate
|
23
|
+
# @return [String]
|
24
|
+
def self.lowercase(*args)
|
25
|
+
args[0].downcase
|
26
|
+
end
|
27
|
+
|
28
|
+
# Adds a prefix to the passed text.
|
29
|
+
# @param [String] args[0] The text to evaluate
|
30
|
+
# @param [String] args[1] The prefix to add
|
31
|
+
# @return [String]
|
32
|
+
def self.prefix(*args)
|
33
|
+
args[1] + args[0]
|
34
|
+
end
|
35
|
+
|
36
|
+
# Adds a suffix to the passed text.
|
37
|
+
# @param [String] args[0] The text to evaluate
|
38
|
+
# @param [String] args[1] The suffix to add
|
39
|
+
# @return [String]
|
40
|
+
def self.suffix(*args)
|
41
|
+
args[0] + args[1]
|
42
|
+
end
|
43
|
+
|
44
|
+
# Capitalizes each word in the passed text.
|
45
|
+
# @param [String] args[0] The text to evaluate
|
46
|
+
# @return [String]
|
47
|
+
def self.titlecase(*args)
|
48
|
+
args[0].split.map(&:capitalize).join(' ')
|
49
|
+
end
|
50
|
+
|
51
|
+
# Uppercases all characters in the passed text.
|
52
|
+
# @param [String] args[0] The text to evaluate
|
9
53
|
# @return [String]
|
10
|
-
def self.
|
11
|
-
|
54
|
+
def self.uppercase(*args)
|
55
|
+
args[0].upcase
|
12
56
|
end
|
13
57
|
end
|
14
58
|
end
|
Binary file
|
@@ -1,28 +1,10 @@
|
|
1
1
|
prompts:
|
2
|
-
- prompt:
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
prereqs:
|
12
|
-
- config_key: superhero
|
13
|
-
config_value: Batman
|
14
|
-
- prompt: Why do you prefer Superman?!
|
15
|
-
default: No clue
|
16
|
-
config_key: superman_answer
|
17
|
-
config_section: personal_info
|
18
|
-
prereqs:
|
19
|
-
- config_key: superhero
|
20
|
-
config_value: Superman
|
21
|
-
- prompt: Why don't you have a clue?
|
22
|
-
config_key: no_clue
|
23
|
-
config_section: personal_info
|
24
|
-
prereqs:
|
25
|
-
- config_key: superhero
|
26
|
-
config_value: Superman
|
27
|
-
- config_key: superman_answer
|
28
|
-
config_value: No clue
|
2
|
+
- prompt: What is your favorite food?
|
3
|
+
config_key: food
|
4
|
+
config_section: personal_info
|
5
|
+
validators:
|
6
|
+
- non_nil
|
7
|
+
behaviors:
|
8
|
+
- uppercase
|
9
|
+
- prefix: 'My favorite food: '
|
10
|
+
- suffix: ' (Italian food FTW!)'
|