nub 0.0.45 → 0.0.46
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/lib/nub/commander.rb +31 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 943bbffc0b7d94c51011c6e97e3ef61a48ee24ebea5153bfb47e49b866d1742d
|
4
|
+
data.tar.gz: 1d6f16fdf680fc3e5d10acedd71103be355aef9ee3470c2e6dccda9cc8bc6ac9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 437709def18c3c5981d7549481d2b2f46c9e82eb2447710f9bea33d879c4919c4fbf50a1d24468c3524ef8993e0200d200720082d485b05f78f6a5eeb413b73f
|
7
|
+
data.tar.gz: 25b13784617df550221bbf74d11c14a9d6de668ca7dafa5c2f8edf7db2ec766b0bb2815e2014634107619e2718edfb9a8b27e12a4673fb0ae863c1d9ec2f866a
|
data/lib/nub/commander.rb
CHANGED
@@ -34,6 +34,7 @@ class Option
|
|
34
34
|
attr_reader(:type)
|
35
35
|
attr_reader(:allowed)
|
36
36
|
attr_reader(:required)
|
37
|
+
attr_accessor(:shared)
|
37
38
|
|
38
39
|
# Create a new option instance
|
39
40
|
# @param key [String] option short hand, long hand and hint e.g. -s|--skip=COMPONENTS
|
@@ -46,6 +47,7 @@ class Option
|
|
46
47
|
@long = nil
|
47
48
|
@short = nil
|
48
49
|
@desc = desc
|
50
|
+
@shared = false
|
49
51
|
@allowed = allowed || []
|
50
52
|
@required = required || false
|
51
53
|
|
@@ -83,9 +85,9 @@ end
|
|
83
85
|
# An implementation of git like command syntax for ruby applications:
|
84
86
|
# see https://github.com/phR0ze/ruby-nub
|
85
87
|
class Commander
|
86
|
-
attr_accessor(:cmds)
|
87
88
|
attr_reader(:config)
|
88
89
|
attr_reader(:banner)
|
90
|
+
attr_accessor(:cmds)
|
89
91
|
|
90
92
|
Command = Struct.new(:name, :desc, :opts, :help)
|
91
93
|
|
@@ -112,6 +114,9 @@ class Commander
|
|
112
114
|
# Configuration - ordered list of commands
|
113
115
|
@config = []
|
114
116
|
|
117
|
+
# List of options that will be added to all commands
|
118
|
+
@shared = []
|
119
|
+
|
115
120
|
# Configure default global options
|
116
121
|
add_global(Option.new('-h|--help', 'Print command/options help'))
|
117
122
|
end
|
@@ -132,13 +137,18 @@ class Commander
|
|
132
137
|
# @param opts [List] list of command options
|
133
138
|
def add(cmd, desc, options:[])
|
134
139
|
Log.die("'global' is a reserved command name") if cmd == 'global'
|
140
|
+
Log.die("'shared' is a reserved command name") if cmd == 'shared'
|
141
|
+
Log.die("'#{cmd}' already exists") if @config.any?{|x| x.name == cmd}
|
135
142
|
Log.die("'help' is a reserved option name") if options.any?{|x| !x.key.nil? && x.key.include?('help')}
|
136
143
|
|
144
|
+
# Add shared options
|
145
|
+
@shared.each{|x| options.unshift(x)}
|
146
|
+
|
137
147
|
cmd = add_cmd(cmd, desc, options)
|
138
148
|
@config << cmd
|
139
149
|
end
|
140
150
|
|
141
|
-
# Add global options
|
151
|
+
# Add global options (any option coming before all commands)
|
142
152
|
# @param option/s [Array/Option] array or single option/s
|
143
153
|
def add_global(options)
|
144
154
|
options = [options] if options.class == Option
|
@@ -152,6 +162,18 @@ class Commander
|
|
152
162
|
@config << add_cmd('global', 'Global options:', options)
|
153
163
|
end
|
154
164
|
|
165
|
+
# Add shared option (options that are added to all commands)
|
166
|
+
# @param option/s [Array/Option] array or single option/s
|
167
|
+
def add_shared(options)
|
168
|
+
options = [options] if options.class == Option
|
169
|
+
options.each{|x|
|
170
|
+
Log.die("duplicate shared option '#{x.desc}' given") if @shared
|
171
|
+
.any?{|y| y.key == x.key && y.desc == x.desc && y.type == x.type}
|
172
|
+
x.shared = true
|
173
|
+
@shared << x
|
174
|
+
}
|
175
|
+
end
|
176
|
+
|
155
177
|
# Returns banner string
|
156
178
|
# @returns [String] the app's banner
|
157
179
|
def banner
|
@@ -315,12 +337,18 @@ class Commander
|
|
315
337
|
# --------------------------------------------------------------------
|
316
338
|
!puts("Error: unknown named option '#{opt}' given!".colorize(:red)) && !puts(cmd.help) and exit if !sym
|
317
339
|
@cmds[cmd.name.to_sym][sym] = value
|
340
|
+
if cmd_opt.shared
|
341
|
+
sym = "shared#{pos}".to_sym if cmd_opt.key.nil?
|
342
|
+
@cmds[:shared] = {} if !@cmds.key?(:shared)
|
343
|
+
@cmds[:shared][sym] = value
|
344
|
+
end
|
318
345
|
}
|
319
346
|
end
|
320
347
|
}
|
321
348
|
|
322
|
-
# Ensure global
|
349
|
+
# Ensure specials (global, shared) are always set
|
323
350
|
@cmds[:global] = {} if !@cmds[:global]
|
351
|
+
@cmds[:shared] = {} if !@cmds[:shared]
|
324
352
|
|
325
353
|
# Ensure all options were consumed
|
326
354
|
Log.die("invalid options #{ARGV}") if ARGV.any?
|