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 +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/bogo-cli/command.rb +14 -17
- data/lib/bogo-cli/parser.rb +76 -7
- data/lib/bogo-cli/version.rb +1 -1
- 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: c3d995cac00e17670f4b08c185c248f79a5bc4da9dfd99f5cd3ee38740c37045
|
4
|
+
data.tar.gz: c381604451c5b15fc8d2d521ed4dee69e52b73684895f8c94827d3f30db71ca1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b27998591ca5eeef1eacb675d53232028328b11d28164eb93563c0fe37ec1464333d3ad002a13a8304e602b02e3d6eecf5a3e463ae0232f7744e780af5158293
|
7
|
+
data.tar.gz: 1c2faafec7efb8f6cc1f17f188b903ee2ebca11ba30ce040244e1c292cc0dc07474cf5a53d9b794cc078c4d6685a0d383282614c95b6dfdfcbc0713eed2f0350
|
data/CHANGELOG.md
CHANGED
data/lib/bogo-cli/command.rb
CHANGED
@@ -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
|
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.
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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.
|
203
|
-
unless
|
204
|
-
|
205
|
-
|
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[
|
205
|
+
@options[key] = value
|
209
206
|
end
|
210
207
|
end
|
211
208
|
end
|
data/lib/bogo-cli/parser.rb
CHANGED
@@ -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 =
|
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
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
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
|
|
data/lib/bogo-cli/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2022-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bogo
|