cliutils 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +6 -0
- data/Gemfile.lock +1 -1
- data/HISTORY.md +11 -0
- data/README.md +82 -7
- data/cliutils.gemspec +1 -5
- data/lib/cliutils/ext/Hash+Extensions.rb +1 -1
- data/lib/cliutils/messenging.rb +3 -1
- data/lib/cliutils/prefs.rb +8 -1
- data/lib/cliutils/pretty-io.rb +11 -25
- data/lib/cliutils/version.rb +1 -1
- data/lib/cliutils.rb +1 -1
- data/res/readme-images/prefs-ask.png +0 -0
- data/test/messenging_test.rb +3 -4
- metadata +8 -21
- data/.yardoc/checksums +0 -11
- data/.yardoc/object_types +0 -0
- data/.yardoc/objects/root.dat +0 -0
- data/.yardoc/proxy_types +0 -0
- data/features/cli_manager.feature +0 -13
- data/features/step_definitions/cli_manager_steps.rb +0 -1
- data/features/support/env.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7d9fdba4ade5e475660b1e5403ba10c27b11d49
|
4
|
+
data.tar.gz: c433c12ec3c28b525a2ac4dd889592753c58473a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd0495d72585e2e6176e58312bbfa9390443bbfb3e4c3906c2aa12096b735d92e058bbc853bb1410990b2b0323a922985d43e44c0a8d391e12d2ad1f78022579
|
7
|
+
data.tar.gz: a88ae22aa1b2e936ff8986714e545e9a8f3e3b9dfaabd68e1999d31ea96df64caa228c7106bd663a3e459bf5e73f6a29231e8320375d818b21d04b2c071a94b9
|
data/.travis.yml
ADDED
data/Gemfile.lock
CHANGED
data/HISTORY.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# 1.0.4 (2014-03-29)
|
2
|
+
|
3
|
+
* Fixed several bugs
|
4
|
+
* Fixed backwards compatibility with Ruby 1.9.2
|
5
|
+
* Added more YARDdoc
|
6
|
+
* Cleaned out old directories
|
7
|
+
* Updated gemspec to reflect new gem versions
|
8
|
+
|
9
|
+
# 1.0.3 (2014-03-29)
|
10
|
+
|
11
|
+
* Initial release of CLIUtils
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
CLIUtils
|
2
2
|
====
|
3
|
+
[](https://travis-ci.org/bachya/cliutils)
|
4
|
+
[](http://badge.fury.io/rb/cliutils)
|
3
5
|
|
4
6
|
CLIUtils is a library of functionality designed to alleviate common tasks and headaches when developing command-line (CLI) apps in Ruby.
|
5
7
|
|
@@ -51,10 +53,10 @@ Note that although this README.md is extensive, it may not cover all methods. Ch
|
|
51
53
|
|
52
54
|
CLIUtils offers:
|
53
55
|
|
54
|
-
* [PrettyIO](
|
55
|
-
* [Messenging](
|
56
|
-
* [Configuration](
|
57
|
-
* [Prefs](
|
56
|
+
* [PrettyIO](#prettyio): nicer-looking CLI messages
|
57
|
+
* [Messenging](#messenging): a full-featured Logger
|
58
|
+
* [Configuration](#configuration): a app configuration manager
|
59
|
+
* [Prefs](#prefs): a preferences prompter and manager
|
58
60
|
|
59
61
|
## PrettyIO
|
60
62
|
|
@@ -146,13 +148,13 @@ messenger.info_block('Starting up...', 'Done!', multiline = false) { # do stuff
|
|
146
148
|
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:
|
147
149
|
|
148
150
|
```Ruby
|
149
|
-
CLIUtils::PrettyIO
|
151
|
+
CLIUtils::PrettyIO.wrap_char_limit = 50
|
150
152
|
messenger.info('This is a really long message, okay? It should wrap at some point. Seriously. Wrapping is nice.')
|
151
153
|
puts ''
|
152
|
-
CLIUtils::PrettyIO
|
154
|
+
CLIUtils::PrettyIO.wrap_char_limit = 20
|
153
155
|
messenger.info('This is a really long message, okay? It should wrap at some point. Seriously. Wrapping is nice.')
|
154
156
|
puts ''
|
155
|
-
CLIUtils::PrettyIO
|
157
|
+
CLIUtils::PrettyIO.wrap = false
|
156
158
|
messenger.info('This is a really long message, okay? It should wrap at some point. Seriously. Wrapping is nice.')
|
157
159
|
```
|
158
160
|

|
@@ -255,6 +257,79 @@ user_data:
|
|
255
257
|
username: bob
|
256
258
|
```
|
257
259
|
|
260
|
+
## Prefs
|
261
|
+
|
262
|
+
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.
|
263
|
+
|
264
|
+
`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:
|
265
|
+
|
266
|
+
* prompt (**required**): the string to prompt your user with
|
267
|
+
* default (*optional*): an optional default to offer
|
268
|
+
* key (**required**): the key that refers to this preference
|
269
|
+
* section (**required**): the Configuration section that this preference applies to
|
270
|
+
* options (*optional*): an optional array of values; the user's choice must be in this array
|
271
|
+
* requirements (*optional*): an optional list of key/value pairs that must exist for this preference to be displayed
|
272
|
+
|
273
|
+
Here's an example YAML preferences file.
|
274
|
+
|
275
|
+
```YAML
|
276
|
+
prompts:
|
277
|
+
- prompt: What is the hostname of your DD-WRT router?
|
278
|
+
default: 192.168.1.1
|
279
|
+
key: hostname
|
280
|
+
section: ssh_info
|
281
|
+
- prompt: What is the SSH username of your DD-WRT router?
|
282
|
+
default: root
|
283
|
+
key: username
|
284
|
+
section: ssh_info
|
285
|
+
- prompt: What SSH port does your DD-WRT router use?
|
286
|
+
default: 22
|
287
|
+
key: port
|
288
|
+
section: ssh_info
|
289
|
+
- prompt: Do you use password or key authentication?
|
290
|
+
default: password
|
291
|
+
key: auth_method
|
292
|
+
section: ssh_info
|
293
|
+
options: ['password', 'key']
|
294
|
+
- prompt: Where is your key located?
|
295
|
+
default: ~/.ssh
|
296
|
+
key: key_location
|
297
|
+
section: ssh_info
|
298
|
+
requirements:
|
299
|
+
- key: auth_method
|
300
|
+
value: key
|
301
|
+
- prompt: What is your password?
|
302
|
+
key: password
|
303
|
+
section: ssh_info
|
304
|
+
requirements:
|
305
|
+
- key: auth_method
|
306
|
+
value: password
|
307
|
+
```
|
308
|
+
|
309
|
+
Assuming the above, `Prefs` is instantiated like so:
|
310
|
+
|
311
|
+
```Ruby
|
312
|
+
prefs = CLIUtils::Prefs.new('path/to/yaml/file')
|
313
|
+
```
|
314
|
+
|
315
|
+
With valid preferences loaded, simply use `ask` to begin prompting your user:
|
316
|
+
|
317
|
+
```Ruby
|
318
|
+
prefs.ask
|
319
|
+
```
|
320
|
+

|
321
|
+
|
322
|
+
Once the user has answered all the preference prompts, you can fold those answers back into a Configurator using the `ingest` method:
|
323
|
+
|
324
|
+
```Ruby
|
325
|
+
configuration.ingest(prefs)
|
326
|
+
configuration.save
|
327
|
+
```
|
328
|
+
|
329
|
+
### Why a Prefs Class?
|
330
|
+
|
331
|
+
I've written apps that need to request user input at various times for multiple different things; as such, I thought it'd be easier to have those scenarios chunked up. You can always wrap `Prefs` into a module singleton if you wish.
|
332
|
+
|
258
333
|
# Known Issues
|
259
334
|
|
260
335
|
* LoggerDelegator doesn't currently know what to do with `messenger.prompt`, so you'll have to manually log a `debug` message if you want that information logged.
|
data/cliutils.gemspec
CHANGED
@@ -10,17 +10,13 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["bachya1208@googlemail.com"]
|
11
11
|
spec.summary = 'Sugary goodness for Ruby CLI apps.'
|
12
12
|
spec.description = 'A library of functionality designed to alleviate common tasks and headaches when developing command-line (CLI) apps in Ruby.'
|
13
|
-
spec.homepage = "
|
13
|
+
spec.homepage = "http://www.bachyaproductions.com/cliutils-ruby-library-cli-apps/"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
|
-
|
21
|
-
spec.has_rdoc = true
|
22
|
-
spec.extra_rdoc_files = ['README.md', 'doc']
|
23
|
-
spec.rdoc_options << '--title' << 'CLIUtils' << '--main' << 'README.md' << '-ri'
|
24
20
|
|
25
21
|
spec.add_development_dependency('bundler', '~> 1.5')
|
26
22
|
spec.add_development_dependency('pry', '~> 0.9')
|
@@ -86,7 +86,7 @@ class Hash
|
|
86
86
|
|
87
87
|
# Same as _deep_transform_keys_in_object, but
|
88
88
|
# destructively alters the original Object.
|
89
|
-
# @param [Object] The object to examine
|
89
|
+
# @param [Object] object The object to examine
|
90
90
|
# @yield &block
|
91
91
|
# @return [Object]
|
92
92
|
def _deep_transform_keys_in_object!(object, &block)
|
data/lib/cliutils/messenging.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
require 'cliutils/pretty-io'
|
2
|
+
|
1
3
|
module CLIUtils
|
2
4
|
# CLIMessenger Module
|
3
5
|
# Outputs coordinated messages to a variety of targets.
|
4
6
|
module Messenging
|
5
|
-
include
|
7
|
+
include PrettyIO
|
6
8
|
|
7
9
|
# Hook that triggers when this module is included.
|
8
10
|
# @param [Object] k The includer object
|
data/lib/cliutils/prefs.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'cliutils/pretty-io'
|
2
|
+
|
1
3
|
module CLIUtils
|
2
4
|
# Engine to derive preferences from a YAML file, deliver
|
3
5
|
# those to a user via a prompt, and collect the results.
|
@@ -6,7 +8,7 @@ module CLIUtils
|
|
6
8
|
attr_reader :answers, :config_path, :prompts
|
7
9
|
|
8
10
|
# Reads prompt data from and stores it.
|
9
|
-
# @param [<String, Array>] data
|
11
|
+
# @param [<String, Hash, Array>] data Filepath to YAML, Hash, or Array
|
10
12
|
# @return [void]
|
11
13
|
def initialize(data)
|
12
14
|
@answers = []
|
@@ -22,6 +24,11 @@ module CLIUtils
|
|
22
24
|
else
|
23
25
|
fail "Invalid configuration file: #{ yaml_path }"
|
24
26
|
end
|
27
|
+
when Hash
|
28
|
+
@config_path = nil
|
29
|
+
|
30
|
+
data = {:prompts => data} unless data.keys[0] == :prompts
|
31
|
+
@prompts.deep_merge!(data).deep_symbolize_keys!
|
25
32
|
when Array
|
26
33
|
@config_path = nil
|
27
34
|
|
data/lib/cliutils/pretty-io.rb
CHANGED
@@ -10,8 +10,14 @@ module CLIUtils
|
|
10
10
|
# CLIMessenger Module
|
11
11
|
# Outputs color-coordinated messages to a CLI
|
12
12
|
module PrettyIO
|
13
|
-
|
14
|
-
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_accessor :wrap
|
16
|
+
attr_accessor :wrap_char_limit
|
17
|
+
end
|
18
|
+
|
19
|
+
self.wrap = true
|
20
|
+
self.wrap_char_limit = 40
|
15
21
|
|
16
22
|
# Hook that triggers when this module is included.
|
17
23
|
# @param [Object] k The includer object
|
@@ -148,26 +154,6 @@ module CLIUtils
|
|
148
154
|
puts _word_wrap(m, '# ').yellow
|
149
155
|
end
|
150
156
|
|
151
|
-
# Toggles wrapping on or off
|
152
|
-
# @param [<True, False>] on
|
153
|
-
# @return [void]
|
154
|
-
def self.wrap(on)
|
155
|
-
@@wrap = on
|
156
|
-
end
|
157
|
-
|
158
|
-
# Returns the current character wrap amount
|
159
|
-
# @return [Integer]
|
160
|
-
def self.wrap_limit
|
161
|
-
@@wrap_char_limit
|
162
|
-
end
|
163
|
-
|
164
|
-
# Sets the number of characters at which to wrap
|
165
|
-
# @param [Integer] chars The number of chars to output before wrapping
|
166
|
-
# @return [void]
|
167
|
-
def self.wrap_at(chars)
|
168
|
-
@@wrap_char_limit = chars
|
169
|
-
end
|
170
|
-
|
171
157
|
private
|
172
158
|
|
173
159
|
# Outputs a wrapped string (where each line is limited
|
@@ -176,9 +162,9 @@ module CLIUtils
|
|
176
162
|
# @param [String] prefix_str The prefix for each line
|
177
163
|
# @return [String]
|
178
164
|
def _word_wrap(text, prefix_str)
|
179
|
-
if
|
180
|
-
return text if
|
181
|
-
text.gsub(/\n/, ' ').gsub(/(.{1,#{
|
165
|
+
if PrettyIO.wrap
|
166
|
+
return text if PrettyIO.wrap_char_limit <= 0
|
167
|
+
text.gsub(/\n/, ' ').gsub(/(.{1,#{PrettyIO.wrap_char_limit - prefix_str.length}})(\s+|$)/, "#{ prefix_str }\\1\n").strip
|
182
168
|
else
|
183
169
|
text
|
184
170
|
end
|
data/lib/cliutils/version.rb
CHANGED
data/lib/cliutils.rb
CHANGED
Binary file
|
data/test/messenging_test.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'fileutils'
|
2
1
|
require 'logger'
|
3
2
|
require 'test/unit'
|
4
3
|
|
@@ -27,10 +26,10 @@ class TestMessenging < Test::Unit::TestCase
|
|
27
26
|
end
|
28
27
|
|
29
28
|
def test_wrapping
|
30
|
-
CLIUtils::PrettyIO.
|
29
|
+
CLIUtils::PrettyIO.wrap_char_limit = 35
|
31
30
|
|
32
31
|
long_str = 'This is a really long string that should wrap itself at some point, okay?'
|
33
|
-
expected_str = long_str.gsub(/\n/, ' ').gsub(/(.{1,#{CLIUtils::PrettyIO.
|
32
|
+
expected_str = long_str.gsub(/\n/, ' ').gsub(/(.{1,#{CLIUtils::PrettyIO.wrap_char_limit - 2}})(\s+|$)/, "# \\1\n").strip
|
34
33
|
assert_output(expected_str.blue + "\n") { messenger.send(:info, long_str) }
|
35
34
|
end
|
36
35
|
|
@@ -47,7 +46,7 @@ class TestMessenging < Test::Unit::TestCase
|
|
47
46
|
messenger.send(:warn, 'Warn test')
|
48
47
|
|
49
48
|
File.open(@file1path, 'r') do |f|
|
50
|
-
assert_output("INFO: Info test\nERROR: Error test\n") { puts f.read.lines[1..-1].join }
|
49
|
+
assert_output("INFO: Info test\nERROR: Error test\n") { puts f.read.lines.to_a[1..-1].join }
|
51
50
|
end
|
52
51
|
end
|
53
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cliutils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Bach
|
@@ -72,23 +72,17 @@ email:
|
|
72
72
|
- bachya1208@googlemail.com
|
73
73
|
executables: []
|
74
74
|
extensions: []
|
75
|
-
extra_rdoc_files:
|
76
|
-
- README.md
|
75
|
+
extra_rdoc_files: []
|
77
76
|
files:
|
78
77
|
- ".gitignore"
|
79
|
-
- ".
|
80
|
-
- ".yardoc/object_types"
|
81
|
-
- ".yardoc/objects/root.dat"
|
82
|
-
- ".yardoc/proxy_types"
|
78
|
+
- ".travis.yml"
|
83
79
|
- Gemfile
|
84
80
|
- Gemfile.lock
|
81
|
+
- HISTORY.md
|
85
82
|
- LICENSE.txt
|
86
83
|
- README.md
|
87
84
|
- Rakefile
|
88
85
|
- cliutils.gemspec
|
89
|
-
- features/cli_manager.feature
|
90
|
-
- features/step_definitions/cli_manager_steps.rb
|
91
|
-
- features/support/env.rb
|
92
86
|
- lib/.DS_Store
|
93
87
|
- lib/cliutils.rb
|
94
88
|
- lib/cliutils/configuration.rb
|
@@ -104,6 +98,7 @@ files:
|
|
104
98
|
- res/readme-images/messenger-types-1.png
|
105
99
|
- res/readme-images/messenger-warn.png
|
106
100
|
- res/readme-images/multi-logger.png
|
101
|
+
- res/readme-images/prefs-ask.png
|
107
102
|
- res/readme-images/prettyio-color-chart.png
|
108
103
|
- res/readme-images/prettyio-gnarly-text.png
|
109
104
|
- res/readme-images/prettyio-red-text.png
|
@@ -116,17 +111,12 @@ files:
|
|
116
111
|
- test/prefs_test.rb
|
117
112
|
- test/string_extesions_test.rb
|
118
113
|
- test/test_files/prefstest.yaml
|
119
|
-
homepage:
|
114
|
+
homepage: http://www.bachyaproductions.com/cliutils-ruby-library-cli-apps/
|
120
115
|
licenses:
|
121
116
|
- MIT
|
122
117
|
metadata: {}
|
123
118
|
post_install_message:
|
124
|
-
rdoc_options:
|
125
|
-
- "--title"
|
126
|
-
- CLIUtils
|
127
|
-
- "--main"
|
128
|
-
- README.md
|
129
|
-
- "-ri"
|
119
|
+
rdoc_options: []
|
130
120
|
require_paths:
|
131
121
|
- lib
|
132
122
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -146,9 +136,6 @@ signing_key:
|
|
146
136
|
specification_version: 4
|
147
137
|
summary: Sugary goodness for Ruby CLI apps.
|
148
138
|
test_files:
|
149
|
-
- features/cli_manager.feature
|
150
|
-
- features/step_definitions/cli_manager_steps.rb
|
151
|
-
- features/support/env.rb
|
152
139
|
- test/configurator_test.rb
|
153
140
|
- test/hash_extensions_test.rb
|
154
141
|
- test/logger_extensions_test.rb
|
@@ -156,4 +143,4 @@ test_files:
|
|
156
143
|
- test/prefs_test.rb
|
157
144
|
- test/string_extesions_test.rb
|
158
145
|
- test/test_files/prefstest.yaml
|
159
|
-
has_rdoc:
|
146
|
+
has_rdoc:
|
data/.yardoc/checksums
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
lib/cliutils.rb 4f397f5444c33ebcebd8384e5b7b498186785d0d
|
2
|
-
lib/cliutils/prefs.rb 66f1c006d76b5334ad4b2b427141796d35065c27
|
3
|
-
lib/cliutils/version.rb 70a1622cf96c5c986b312e8b7f5751131090b30e
|
4
|
-
lib/cliutils/pretty-io.rb fba4516ec1e2561d1b5e613e4f61b04f03e8abc0
|
5
|
-
lib/cliutils/messenging.rb c0362bcb8e1090d83966d2b3596532c7f1576570
|
6
|
-
lib/cliutils/configurator.rb c202ac417ea4891c990da586d1edf6333f0fa8d6
|
7
|
-
lib/cliutils/configuration.rb 02752324c57b7c7054d9a8158f70641cd73682bc
|
8
|
-
lib/cliutils/logger-delegator.rb 507b215088c1d019d42059832ef8280f746ee26b
|
9
|
-
lib/cliutils/ext/Hash+Extensions.rb e51f7475afccd3ad38a4a62ddcf87216f2dcc974
|
10
|
-
lib/cliutils/ext/Logger+Extensions.rb 946bf8385446f369264877a2fe6bb415f24dbcd8
|
11
|
-
lib/cliutils/ext/String+Extensions.rb a0a39b16d1709e7f5c5c4eff0d6d8258e691582d
|
data/.yardoc/object_types
DELETED
Binary file
|
data/.yardoc/objects/root.dat
DELETED
Binary file
|
data/.yardoc/proxy_types
DELETED
Binary file
|
@@ -1,13 +0,0 @@
|
|
1
|
-
Feature: My bootstrapped app kinda works
|
2
|
-
In order to get going on coding my awesome app
|
3
|
-
I want to have aruba and cucumber setup
|
4
|
-
So I don't have to do it myself
|
5
|
-
|
6
|
-
Scenario: App just runs
|
7
|
-
When I get help for "cliutils"
|
8
|
-
Then the exit status should be 0
|
9
|
-
And the banner should be present
|
10
|
-
And the banner should document that this app takes options
|
11
|
-
And the following options should be documented:
|
12
|
-
|--version|
|
13
|
-
And the banner should document that this app takes no arguments
|
@@ -1 +0,0 @@
|
|
1
|
-
# Put your step definitions here
|
data/features/support/env.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'aruba/cucumber'
|
2
|
-
require 'methadone/cucumber'
|
3
|
-
|
4
|
-
ENV['PATH'] = "#{File.expand_path(File.dirname(__FILE__) + '/../../bin')}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
|
5
|
-
LIB_DIR = File.join(File.expand_path(File.dirname(__FILE__)),'..','..','lib')
|
6
|
-
|
7
|
-
Before do
|
8
|
-
# Using "announce" causes massive warnings on 1.9.2
|
9
|
-
@puts = true
|
10
|
-
@original_rubylib = ENV['RUBYLIB']
|
11
|
-
ENV['RUBYLIB'] = LIB_DIR + File::PATH_SEPARATOR + ENV['RUBYLIB'].to_s
|
12
|
-
end
|
13
|
-
|
14
|
-
After do
|
15
|
-
ENV['RUBYLIB'] = @original_rubylib
|
16
|
-
end
|