nub 0.0.39 → 0.0.40
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nub/commander.rb +12 -10
- data/lib/nub/sys.rb +19 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15cb45b36e04783ecc0f79034f6e724fc453931766812ad33c620c7dc3710bfc
|
4
|
+
data.tar.gz: 00207fa6c60fa53c84cbc26604875be289217c0c1cc09ec73dc7fcbad9cd3519
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4fceb5e69d5574bb681d83a98bcae3cc298e1f4bdbe96fa7d2db966b5ac600285c083dae8d1c807cad1b6a5a711bf7fba5f18aa0a1edbc6a62b6256edbabde0
|
7
|
+
data.tar.gz: f4842193383f214356e52528adfcfa6b4605a2086d1b31ea728ad068cdc4914170757caf10d463b4e0688a35cadc1b68f00e4fc25546cc8f5c071466d559fbde
|
data/lib/nub/commander.rb
CHANGED
@@ -95,13 +95,11 @@ class Commander
|
|
95
95
|
# @param app [String] application name e.g. reduce
|
96
96
|
# @param version [String] version of the application e.g. 1.0.0
|
97
97
|
# @param examples [String] optional examples to list after the title before usage
|
98
|
-
def initialize(app, version, examples:nil)
|
98
|
+
def initialize(app:nil, version:nil, examples:nil)
|
99
99
|
@app = app
|
100
|
+
@app_default = Sys.caller_filename
|
100
101
|
@version = version
|
101
102
|
@examples = examples
|
102
|
-
!puts("Error: app name and version are required".colorize(:red)) and
|
103
|
-
exit if @app.nil? or @app.empty? or @version.nil? or @version.empty?
|
104
|
-
|
105
103
|
@help_opt = Option.new('-h|--help', 'Print command/options help')
|
106
104
|
@just = 40
|
107
105
|
|
@@ -127,7 +125,9 @@ class Commander
|
|
127
125
|
exit if cmd =~ /[^a-z]/
|
128
126
|
|
129
127
|
# Build help for command
|
130
|
-
|
128
|
+
app = @app || @app_default
|
129
|
+
help = "#{desc}\n\nUsage: ./#{app} #{cmd} [options]\n"
|
130
|
+
help = "#{banner}\n#{help}" if @app
|
131
131
|
options << @help_opt
|
132
132
|
|
133
133
|
# Add positional options first
|
@@ -150,23 +150,25 @@ class Commander
|
|
150
150
|
# Returns banner string
|
151
151
|
# @returns [String] the app's banner
|
152
152
|
def banner
|
153
|
-
|
153
|
+
version = @version.nil? ? "" : "_v#{@version}"
|
154
|
+
banner = "#{@app}#{version}\n#{'-' * 80}".colorize(:light_yellow)
|
154
155
|
return banner
|
155
156
|
end
|
156
157
|
|
157
158
|
# Return the app's help string
|
158
159
|
# @returns [String] the app's help string
|
159
160
|
def help
|
160
|
-
help = "#{banner}\n"
|
161
|
+
help = @app.nil? ? "" : "#{banner}\n"
|
161
162
|
if !@examples.nil? && !@examples.empty?
|
162
163
|
newline = Sys.strip_colorize(@examples)[-1] != "\n" ? "\n" : ""
|
163
164
|
help += "Examples:\n#{@examples}\n#{newline}"
|
164
165
|
end
|
165
|
-
|
166
|
+
app = @app || @app_default
|
167
|
+
help += "Usage: ./#{app} [commands] [options]\n"
|
166
168
|
help += " #{'-h|--help'.ljust(@just)}Print command/options help: Flag\n"
|
167
169
|
help += "COMMANDS:\n"
|
168
170
|
@config.each{|x| help += " #{x.name.ljust(@just)}#{x.desc}\n" }
|
169
|
-
help += "\nsee './#{
|
171
|
+
help += "\nsee './#{app} COMMAND --help' for specific command help\n"
|
170
172
|
|
171
173
|
return help
|
172
174
|
end
|
@@ -277,7 +279,7 @@ class Commander
|
|
277
279
|
!puts("Error: invalid options #{ARGV}".colorize(:red)) and exit if ARGV.any?
|
278
280
|
|
279
281
|
# Print banner on success
|
280
|
-
puts(banner)
|
282
|
+
puts(banner) if @app
|
281
283
|
end
|
282
284
|
end
|
283
285
|
|
data/lib/nub/sys.rb
CHANGED
@@ -38,6 +38,25 @@ ColorMap = {
|
|
38
38
|
|
39
39
|
module Sys
|
40
40
|
|
41
|
+
# Wait for any key to be pressed
|
42
|
+
def self.any_key?
|
43
|
+
begin
|
44
|
+
state = `stty -g`
|
45
|
+
`stty raw -echo -icanon isig`
|
46
|
+
STDIN.getc.chr
|
47
|
+
ensure
|
48
|
+
`stty #{state}`
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Get the caller's filename for the caller of the function this call is nested in
|
53
|
+
# not the function this call is called in
|
54
|
+
# @returns [String] the caller's filename
|
55
|
+
def self.caller_filename
|
56
|
+
path = caller_locations(2, 1).first.path
|
57
|
+
return File.basename(path)
|
58
|
+
end
|
59
|
+
|
41
60
|
# Capture STDOUT to a string
|
42
61
|
# @returns [String] the redirected output
|
43
62
|
def self.capture(&block)
|
@@ -51,17 +70,6 @@ module Sys
|
|
51
70
|
return OpenStruct.new(result: result, stdout: stdout.string, stderr: stderr.string)
|
52
71
|
end
|
53
72
|
|
54
|
-
# Wait for any key to be pressed
|
55
|
-
def self.any_key?
|
56
|
-
begin
|
57
|
-
state = `stty -g`
|
58
|
-
`stty raw -echo -icanon isig`
|
59
|
-
STDIN.getc.chr
|
60
|
-
ensure
|
61
|
-
`stty #{state}`
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
73
|
# Strip the ansi color codes from the given string
|
66
74
|
# @param str [String] string with ansi color codes
|
67
75
|
# @returns [String] string without any ansi codes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.40
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Crummett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|