bogo-cli 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97c776598127786a35b12a9d95a6d8453c3606aca1551b5acbb374a99606061d
4
- data.tar.gz: e97a5d3ad04f9e7573f11146c46e3b24e4536dfc1c758cbf56802723275adfd4
3
+ metadata.gz: c3d995cac00e17670f4b08c185c248f79a5bc4da9dfd99f5cd3ee38740c37045
4
+ data.tar.gz: c381604451c5b15fc8d2d521ed4dee69e52b73684895f8c94827d3f30db71ca1
5
5
  SHA512:
6
- metadata.gz: b651506c2871283b831264a32a8cadd8414a1dc1743b1a57fd54ba44cf7756c899752307237d4efc77f2d3d0dc86291b62a183ee43f48bd57427f472644b6e1d
7
- data.tar.gz: 6a99913415692998685c3e60e803a2ac5df8dfd299f8b9e94b6dfe89c105d2beacf67339f4b1ea99a342e13d1ac603d9c66cda07d9483f295b62092443547d7f
6
+ metadata.gz: b27998591ca5eeef1eacb675d53232028328b11d28164eb93563c0fe37ec1464333d3ad002a13a8304e602b02e3d6eecf5a3e463ae0232f7744e780af5158293
7
+ data.tar.gz: 1c2faafec7efb8f6cc1f17f188b903ee2ebca11ba30ce040244e1c292cc0dc07474cf5a53d9b794cc078c4d6685a0d383282614c95b6dfdfcbc0713eed2f0350
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # v0.3.1
2
+ * Add context for option parsing (#15)
3
+
1
4
  # v0.3.0
2
5
  * Remove dependency on slop library (#14)
3
6
 
@@ -31,17 +31,19 @@ module Bogo
31
31
  #
32
32
  # @return [self]
33
33
  def initialize(cli_opts, args)
34
- @options = Smash.new
35
34
  @defaults = Smash.new
35
+ @options = Smash.new
36
36
  case cli_opts
37
- when OpenStruct
37
+ when Bogo::Cli::Parser::OptionValues
38
38
  process_cli_options(cli_opts)
39
+ when Bogo::Cli::Parser::Command
40
+ @options = cli_opts.parse(args).first
39
41
  else
40
- @options = cli_opts.to_hash.to_smash(:snake)
41
- [@options, *@options.values].compact.each do |hsh|
42
- next unless hsh.is_a?(Hash)
43
- hsh.delete_if { |k, v| v.nil? }
44
- end
42
+ @options = cli_opts.to_h.to_smash(:snake)
43
+ end
44
+ [@options, *@options.values].compact.each do |hsh|
45
+ next unless hsh.is_a?(Hash)
46
+ hsh.delete_if { |k, v| v.nil? }
45
47
  end
46
48
  @arguments = validate_arguments!(args)
47
49
  load_config!
@@ -193,19 +195,14 @@ module Bogo
193
195
  # @param cli_opts [Slop]
194
196
  # @return [NilClass]
195
197
  def process_cli_options(cli_opts)
196
- unless cli_opts.is_a?(OpenStruct)
197
- raise TypeError,
198
- "Expecting `OpenStruct' but received `#{cli_opts.class}'"
199
- end
200
198
  @options = Smash.new
201
199
  @defaults = Smash.new
202
- cli_opts.each do |cli_opt|
203
- unless cli_opt.value.nil?
204
- opt_key = Bogo::Utility.snake(cli_opt.key)
205
- if cli_opt.default?
206
- @defaults[opt_key] = cli_opt.value
200
+ cli_opts.each_pair do |key, value|
201
+ unless value.nil?
202
+ if cli_opts.default?(key)
203
+ @defaults[key] = value
207
204
  else
208
- @options[opt_key] = cli_opt.value
205
+ @options[key] = value
209
206
  end
210
207
  end
211
208
  end
@@ -1,3 +1,4 @@
1
+ require 'forwardable'
1
2
  require 'optparse'
2
3
  require 'ostruct'
3
4
 
@@ -8,6 +9,72 @@ module Bogo
8
9
  # @return [Symbol] represent unset value
9
10
  UNSET = :__unset__
10
11
 
12
+ class OptionValues
13
+ extend Forwardable
14
+
15
+ NONFORWARDABLE = [:is_a?, :respond_to?, :object_id, :inspect, :keys, :to_s, :[]=].freeze
16
+
17
+ Smash.public_instance_methods.each do |ifunc|
18
+ next if ifunc.to_s.start_with?("_") || NONFORWARDABLE.include?(ifunc)
19
+ def_delegator :composite, ifunc
20
+ end
21
+
22
+ def initialize
23
+ @defaults = Smash.new
24
+ @sets = Smash.new
25
+ end
26
+
27
+ def []=(key, value)
28
+ assign(key, value)
29
+ end
30
+
31
+ def assign(key, value)
32
+ @sets[key] = value
33
+ self
34
+ end
35
+
36
+ def set_default(key, value)
37
+ @defaults[key] = value
38
+ self
39
+ end
40
+
41
+ def keys
42
+ @sets.keys | @defaults.keys
43
+ end
44
+
45
+ def default?(key)
46
+ unless keys.include?(key)
47
+ raise KeyError,
48
+ "Unknown option key '#{key}'"
49
+ end
50
+ !@sets.keys.include?(key)
51
+ end
52
+
53
+ def to_s
54
+ "<OptionValues: #{composite.inspect}>"
55
+ end
56
+
57
+ def inspect
58
+ "<OptionValues: #{@sets.inspect} | #{@defaults.inspect}>"
59
+ end
60
+
61
+ def is_a?(const)
62
+ super || composite.is_a?(const)
63
+ end
64
+
65
+ def respond_to?(method)
66
+ super || composite.respond_to?(method)
67
+ end
68
+
69
+ def composite
70
+ Smash.new.tap do |c|
71
+ keys.each do |k|
72
+ c[k] = @sets.fetch(k, @defaults[k])
73
+ end
74
+ end
75
+ end
76
+ end
77
+
11
78
  # Modified option parser to include
12
79
  # subcommand information
13
80
  class OptionParser < ::OptionParser
@@ -47,7 +114,7 @@ module Bogo
47
114
  @commands = []
48
115
  @flags = []
49
116
  @callable = nil
50
- @options = OpenStruct.new
117
+ @options = OptionValues.new
51
118
  end
52
119
 
53
120
  # Add a new flag
@@ -118,13 +185,15 @@ module Bogo
118
185
  def parse(arguments)
119
186
  raise "Must call #generate before #parse" if
120
187
  parser.nil?
121
- @options = OpenStruct.new.tap do |opts|
122
- flags.each do |f|
123
- next if f.default.nil?
124
- opts[f.option_name] = f.default
125
- end
188
+ flags.each do |f|
189
+ next if f.default.nil?
190
+ options.set_default(f.option_name, f.default)
191
+ end
192
+ init = OpenStruct.new
193
+ parser.parse!(arguments, into: init)
194
+ init.each_pair do |k, v|
195
+ options.assign(k, v)
126
196
  end
127
- parser.parse!(arguments, into: options)
128
197
  [options, arguments]
129
198
  end
130
199
 
@@ -1,6 +1,6 @@
1
1
  module Bogo
2
2
  module Cli
3
3
  # Current library version
4
- VERSION = Gem::Version.new("0.3.0")
4
+ VERSION = Gem::Version.new("0.3.1")
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bogo-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Roberts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-15 00:00:00.000000000 Z
11
+ date: 2022-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bogo