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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nub/commander.rb +31 -3
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 867a70047e9f70cfe52dcf96d22038a33b981f4d51596a9c770048f52ba7abbc
4
- data.tar.gz: 0e61b494698663f1abfd4b518ae1014466d925d398e8afe96fae71c1b5e19a74
3
+ metadata.gz: 943bbffc0b7d94c51011c6e97e3ef61a48ee24ebea5153bfb47e49b866d1742d
4
+ data.tar.gz: 1d6f16fdf680fc3e5d10acedd71103be355aef9ee3470c2e6dccda9cc8bc6ac9
5
5
  SHA512:
6
- metadata.gz: 432d3f0a1169a974eeb9d1adbd784f56cf63b4c594d09b3687df2f0b2c2795351952e2c3def0e742aa4b464b52ce051e62332c25ca57d5d266171deb7e7824f0
7
- data.tar.gz: 4340abc5d2100d98bf4e919500251ebfcff8dee1d2a592757e4bf2fdcb1a163baddd883d1282b028ab2cb06a769981cc3ed59324130ad4800316e9f3a690a729
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 is always set
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?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.45
4
+ version: 0.0.46
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Crummett